I was going through the 'Factory method' pages in SO and had come across this link. And this comment. The example looked as a variant and thought to implement in its original way: to defer instantiation to subclasses...
Here is my attempt. Does the following code implements the Factory pattern of the example specified in the link? Please validate and suggest if this has to undergo any re-factoring.
public class ScheduleTypeFactoryImpl implements ScheduleTypeFactory {
@Override
public IScheduleItem createLinearScheduleItem() {
return new LinearScheduleItem();
}
@Override
public IScheduleItem createVODScheduleItem() {
return new VODScheduleItem();
}
}
public class UseScheduleTypeFactory {
public enum ScheduleTypeEnum {
CableOnDemandScheduleTypeID,
BroadbandScheduleTypeID,
LinearCableScheduleTypeID,
MobileLinearScheduleTypeID
}
public static IScheduleItem getScheduleItem(ScheduleTypeEnum scheduleType) {
IScheduleItem scheduleItem = null;
ScheduleTypeFactory scheduleTypeFactory = new ScheduleTypeFactoryImpl();
switch (scheduleType) {
case CableOnDemandScheduleTypeID:
scheduleItem = scheduleTypeFactory.createVODScheduleItem();
break;
case BroadbandScheduleTypeID:
scheduleItem = scheduleTypeFactory.createVODScheduleItem();
break;
case LinearCableScheduleTypeID:
scheduleItem = scheduleTypeFactory.createLinearScheduleItem();
break;
case MobileLinearScheduleTypeID:
scheduleItem = scheduleTypeFactory.createLinearScheduleItem();
break;
default:
break;
}
return scheduleItem;
}
}