I have created a polymorphic system, but I don't know whether this is the correct way of doing it. Am I abusing Polymorphism here? Here is the code:
class WriteObj
{
public string Obj1 { get; set; }
public string Obj2 { get; set; }
public string Obj3 { get; set; }
}
The above code is a data object that I am passing around in the methods, since I would be using a List of these objects.
abstract class BaseWriter
{
public abstract void Write(List<WriteObj> writeObjList);
}
class ConsoleWriter : BaseWriter
{
public override void Write(List<WriteObj> writeObjList)
{
for (int i = 0; i < writeObjList.Count; i++)
{
Console.Writeline("I am in Console Writer, parameter: " + writeObjList[i].Obj1);
}
}
}
class FileWriter : BaseWriter
{
public override void Write(List<WriteObj> writeObjList)
{
for (int i = 0; i < writeObjList.Count; i++)
{
Console.Writeline("I write in file, parameter: " + writeObjList[i].Obj1);
}
}
}
class DatabaseWriter : BaseWriter
{
public override void Write(List<WriteObj> writeObjList)
{
for (int i = 0; i < writeObjList.Count; i++)
{
Console.Writeline("I write in database, parameter: " + writeObjList[i].Obj2);
}
}
}
In my main method I call them like:
static void main()
{
List<WriteObj> col = new List<WriteObj>();
col.AddRange(new WriteObj[2] { new WriteObj { Obj1 = "this is obj1 iteration 1", Obj2 = "This is obj2 iteration 1" },
new WriteObj { Obj1 = "this is obj1 iteration 2", Obj2 = "This is obj2 iteration 2" } });
//some factory will generate these concrete types,
//but the sake of simplicity I am instantiating it like that.
BaseWriter a = new ConsoleWriter();
a.Write(col);
BaseWriter b = new FileWriter();
b.Write(col);
BaseWriter c = new DatabaseWriter();
c.Write(col);
}
Is it Ok to pass List of WriteObj in the Write method of the respective concrete implementations?
Update : I have used abstract class because it will be having some methods in it. I haven't mentioned it here for the sake of simplicity.