0

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);
}
}
7
  • 1
    Its difficult to comment without seeing the code for CreateDevice Commented Feb 16, 2017 at 6:27
  • @Mathew Edited the question Commented Feb 16, 2017 at 6:34
  • neither. you could pass in a List<ElectricDevice> to initialize it with that collection. if you need the object to create the ElectricDevice objects I would have a method to do that. those methods could take an optional int parameter to say how many to add. then if you still want to create the devices in your constructor you can call those methods from there. Commented Feb 16, 2017 at 6:34
  • also, your second example doesn't do the same thing as your first one. it needs separate loops for each type of device. Commented Feb 16, 2017 at 6:34
  • Thanks @MikeD. But we are moving logic related to switchboard out of switchboard class. Correct me if i am wrong. I am new to OOP. Commented Feb 16, 2017 at 6:45

2 Answers 2

1

I would refactor that loop into a generic method to which you send a number (which represent the number of devices, and pass it function which returns an Electrical device:

public delegate ElectricDevice Func<ElectricDevice>()
private addElectricalDevices(int number, Func<int, ElectricDevice> createDeviceFunc)
{
   for(int i = 0; i < number; i++)
   {
      Devices.Add(createDeviceFunc(i));
   }
}

Example usage:

addElectricalDevices(10, (i) => new Fan("Fan " + (i + 1)));

Sorry, it's been long since i wrote C# code so this might not be perfect.

Sign up to request clarification or add additional context in comments.

1 Comment

but still we are instantiating Device outside the class. I want to know whether it is right approach or not
0

Designing of a system is something that is difficult to suggest as there are many things that needs to be considered.

But keeping it minimum and sticking to your question, there are a few things that needs to be considered -

Suggest to have an interface like ISwitchable, to be implemented by ElectricDevice which are switchable. This is important conceptually, as not all ElectricDevices are meant to be switchable -

public interface ISwitchable
{
}

Now you can implement this in you device something like this -

public class Fan : ElectricDevice, ISwitchable
{
    //Implementation of fan
}

For the total number of devices, it should be implemented as a read only property, which is calculated as a sum of all the devices, other there is a possibility of this value get tampered. There is no need to calculate this value with each iteration.

At the end this is how you SwitchBoard class might look like -

public class SwitchBoard
{
    public List<ISwitchable> switchableDevices { get; private set; }

    public int TotalSwichableDevices
    {
        get { return numberOfFans + numberOfACs + numberOfBulbs; }
    }
    private readonly int numberOfFans;
    private readonly int numberOfACs;
    private readonly int numberOfBulbs;

    public SwitchBoard(int noOfFans, int noOfACs, int noOfBulbs)
    {
        this.numberOfFans = noOfFans;
        this.numberOfACs = noOfACs;
        this.numberOfBulbs = noOfBulbs;

        switchableDevices = new List<ISwitchable>();
        switchableDevices.AddRange(this.AddFans());

        //TODO: Add other devices
    }

    private IEnumerable<ISwitchable> AddFans()
    {
        List<ISwitchable> fans = new List<ISwitchable>();
        for (int i = 0; i < numberOfFans; i++)
        {
            fans.Add(new Fan());
        }
        return fans;
    }
}

3 Comments

In C# >= 6 you don't need to create explicit read-only fields, a read-only auto-property is enough public string Text { get; } and you can set it in the constructor or directly on the property public string Text { get; } = "hey";. Also, in C# this isn't required unless you need to disambiguate variable/class field identifiers
@Yogi Thanks for the help! I also came up with same solution after thinking a lot but yours seems like better one with extra features like interfaces.
The reason why I think this answer correct is in practical switchboard scenario. Switches are manufactured(or created) outside. latter we will add that to switch board. Exactly like what we have done in our code. Correct me if anything wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.