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();
}
}