I have to implement switchboard class which can have devices like Fan, AC, Bulb etc. My switch board class looks like below.
Which one is more object oriented?
1.
class SwitchBoard
{
public static int totalDevices=0;
public List<ElectricDevice> Devices { get; set; }
public SwitchBoard(int noOfFans, int noOfACs, int noOfBulbs)
{
Devices = new List<ElectricDevice>();
//int deviceCount = 0;
for (int i = 0; i < noOfACs + noOfBulbs + noOfFans; i++)
{
if (i < noOfFans)
{
Devices.Add(new Fan("Fan " + (i + 1),totalDevices));
}
else if (i >= noOfFans && i < noOfFans + noOfACs)
{
Devices.Add(new AC("AC " + (i - noOfFans + 1), totalDevices));
}
else
{
Devices.Add(new Bulb("Bulb " + (i - noOfFans - noOfACs + 1), totalDevices));
}
totalDevices++;
}
}
}
2.
class SwitchBoard
{
public static int totalDevices=0;
public List<ElectricDevice> Devices { get; set; }
public SwitchBoard(int noOfFans, int noOfACs, int noOfBulbs)
{
Devices = new List<ElectricDevice>();
CreateDevice(Devices, "Fan", noOfFans);
CreateDevice(Devices, "AC", noOfACs);
CreateDevice(Devices, "Bulb", noOfBulbs);
}
I am feeling like first one is best approach because in second method we are using method which intstantiate the class outside class by taking property outside the class and intializing outside it. Like taking out switch out of the swtichboard and connecting it to fan and placing it back into switch board.
I think it has something to do with encapsulation.
Pseudo code for CreateDevice
function CreateDevice(List<EelectricDevice> Devices, string type, int noOfObjects )
{
for(int i=Devices.Length; i<noOfDevices+Devices.Length;i++)
{
if(type=="Fan")
Device[i]=new Fan("Fan"+i);
else if(type=="AC")
Device[i]=new AC("AC"+i);
else if(type=="Bulb")
Device[i]=new Bulb("Bulb"+i);
}
}