Factory Method Implementation

Posted by cedar715 on Stack Overflow See other posts from Stack Overflow or by cedar715
Published on 2010-04-17T19:25:59Z Indexed on 2010/04/17 19:33 UTC
Read the original article Hit count: 369

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

© Stack Overflow or respective owner

Related posts about factory-method

Related posts about factory-pattern