Is this Factory Method creation pattern?

前端 未结 20 819
再見小時候
再見小時候 2020-12-12 17:12

I have been using factory method creation pattern for awhile now. I was just recently told that this:

public static class ScheduleTypeFactory
{
    public st         


        
20条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 17:59

    Your Tech is right on renaming the method:

    public static IScheduleItem GetScheduleItem(ScheduleTypeEnum scheduleType)
    

    The action of the method is not to get something, is to create something. How do you decide which scheduleType should be created? Seems that logic should be encapsulated not the switch of the type.

    Also why the static on the class? Where are you using it from?

    public class ScheduleTypeFactory
    {
        public static IScheduleItem createScheduleFrom(ScheduleTypeEnum scheduleType)
        {
    
            switch (scheduleType)
            {
                case ScheduleTypeEnum.CableOnDemandScheduleTypeID:
                case ScheduleTypeEnum.BroadbandScheduleTypeID:
                     return new VODScheduleItem();
                 case ScheduleTypeEnum.LinearCableScheduleTypeID:
                 case ScheduleTypeEnum.MobileLinearScheduleTypeID:
                        return new LinearScheduleItem();
            }
    
           raise InvalidSchedule;
        }
    }
    

提交回复
热议问题