Question

Say you have a factory that returns instances of ILightBulb. Two ways (there may be more) of implementing the factory are as follows:

Option 1 - Passing in an enum type

enum LightBulbType
{
    Incandescent,
    Halogen,
    Led,
}

class ILightBulbFactory
{
    public ILightBulb Create(LightBulbType type)
    {
        switch (type)
        {
            case LightBulbType.Incandescent:
                return new IncandescentBulb();
            case LightBulbType.Halogen:
                return new HalogenBulb();
            case LightBulbType.Led:
                return new LedBulb();
        }
    }
}

Option 2 - Explicit method names

class ILightBulbFactory
{
    public ILightBulb CreateIncandescent()
    {
        return new IncandescentBulb();
    }

    public ILightBulb CreateHalogen()
    {
        return new HalogenBulb();
    }

    public ILightBulb CreateLed()
    {
        return new LedBulb();
    }
}

Which method is most preferable, and why?

Thanks.

Was it helpful?

Solution

In java, you can solve it in the enum, thus making sure that if you add new enums, all your code will remain working, instead of forgetting to add statements to your case.

enum LightBulbType
{
    Incandescent{

        @Override
        public ILightBulb getInstance() {
            return new IncandescentBulb();
        }

    },
    Halogen{

        @Override
        public ILightBulb getInstance() {
            return new HalogenBulb();
        }

    },
    Led{

        @Override
        public ILightBulb getInstance() {
            return new LedBulb();
        }

    };

    public abstract ILightBulb getInstance();
}

class ILightBulbFactory
{
    public ILightBulb Create(LightBulbType type)
    {
       return type.getInstance();
    }
}

OTHER TIPS

The equivalent for c# would be

public sealed class LightBulbType
{
    public static readonly LightBulbType Incandescent = new 
           LightBulbType("Incandescent", new IncandescentBulb());
    public static readonly LightBulbType Halogen = new 
           LightBulbType("Halogen", new HalogenBulb());
    public static readonly LightBulbType LedBulb = new LightBulbType("Led", 
           new LedBulb());

    public static IEnumerable<LightBulbType> Values
    {
        get
        {
            yield return Incandescent;
            yield return Halogen;
            yield return LedBulb;
        }
    }

    private string Name { get; set; }

    private ILightBulb Instance { get;}


    private LightBulbType(string name, ILightBulb instance)
    {
        this.Name = name;
        this.Instance = instance;
    }

    public override string ToString() => Name;

    public ILightBulb GetInstance()
    {
        return Instance;
    }
}

public interface ILightBulb
{
}

public class IncandescentBulb : ILightBulb
{
}

public class HalogenBulb : ILightBulb
{
}

public class LedBulb : ILightBulb
{
}

public static class LightBulbFactory
{
    public static ILightBulb Create(LightBulbType type)
    {
        return type.GetInstance();
    }
}

For call

ILightBulb halogenBulb = new HalogenBulb();
ILightBulb incandescentBulb = new IncandescentBulb();
ILightBulb ledBulb = new LedBulb();

or 

ILightBulb halo = LightBulbFactory.Create(LightBulbType.Halogen);
ILightBulb inca = LightBulbFactory.Create(LightBulbType.Incandescent);
ILightBulb led = LightBulbFactory.Create(LightBulbType.LedBulb);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top