For example I have this classes:
public abstract class Device
{
public Device()
{
}
public Device(XmlDevice xmlDevice)
{
// creating device by xmlDevice
}
}
public class Modem : Device
{
public Modem()
{
}
public Modem(XmlModem modem)
{
// creating modem by xmlModem
}
}
public class Plc : Device
{
public Plc()
{
}
public Plc(XmlPlc plc)
{
// creating plc by xmlPlc
}
}
public abstract class XmlDevice
{
public XmlDevice()
{
}
}
public class XmlModem : XmlDevice
{
public XmlModem()
{
}
}
public class XmlPlc : XmlDevice
{
public XmlPlc()
{
}
}
I have a list with XmlDevice objects - how can I create Device objects based on its type? I can do this like this:
foreach( xmlDevice in xmlDevicesList )
{
if( xmlDevice is XmlModem )
devicesList.Add( new Modem((XmlModem)xmlDevice) );
else if( xmlDevice is XmlPlc )
devicesList.Add( new Plc((XmlPlc)xmlDevice) );
}
But I want to avoid "is" statement and casting objects. Is it more clean way to do this?