问题描述
public static class ScheduleTypeFactory { public static IScheduleItem GetScheduleItem(ScheduleTypeEnum scheduleType) { IScheduleItem scheduleItem = null; switch (scheduleType) { case ScheduleTypeEnum.CableOnDemandScheduleTypeID: { scheduleItem = new VODScheduleItem(); break; } case ScheduleTypeEnum.BroadbandScheduleTypeID: { scheduleItem = new VODScheduleItem(); break; } case ScheduleTypeEnum.LinearCableScheduleTypeID: { scheduleItem = new LinearScheduleItem(); break; } case ScheduleTypeEnum.MobileLinearScheduleTypeID: { scheduleItem = new LinearScheduleItem(); break; } } return scheduleItem; } }
不是我的"技术"负责人在没有告诉我原因或给我她的解释的情况下创建的工厂方法创建模式.我恳请解释,她告诉我她没有时间.我被告知只需重命名它.如果我错了,那么我无疑会接受我多年来一直错误地实施这一点.这是您将如何实现工厂方法创建模式的方式吗?提前致谢.
推荐答案
我同意将这个方法称为"工厂方法",尽管设计并不是严格意义上的"工厂方法模式".
这是一个关键点(来自维基百科):
...Factory 方法允许类将实例化推迟到子类."
由于您的类是静态的并且方法是静态的(因此是非虚拟的),因此不可能有"延迟".
从概念上讲,还要注意,此实现虽然提供了封装,但不会解耦/延迟任何决策.
话虽如此,同一篇 Wikipedia 文章确实将此架构呈现为"工厂方法模式"的变体.
总结总结:在我看来,这个片段不是"工厂方法 OO 设计模式"的正确实现,因为它不满足"类延迟实例化到子类".不过,我个人会将此解决方案自由地称为"工厂方法".
要使其成为真正的工厂方法模式,您需要允许方法被子类覆盖.IE.工厂类 (ScheduleTypeFactory) 需要是可扩展的(即非静态的),GetScheduleItem 需要是虚拟的.
问题描述
I have been using factory method creation pattern for awhile now. I was just recently told that this:
public static class ScheduleTypeFactory { public static IScheduleItem GetScheduleItem(ScheduleTypeEnum scheduleType) { IScheduleItem scheduleItem = null; switch (scheduleType) { case ScheduleTypeEnum.CableOnDemandScheduleTypeID: { scheduleItem = new VODScheduleItem(); break; } case ScheduleTypeEnum.BroadbandScheduleTypeID: { scheduleItem = new VODScheduleItem(); break; } case ScheduleTypeEnum.LinearCableScheduleTypeID: { scheduleItem = new LinearScheduleItem(); break; } case ScheduleTypeEnum.MobileLinearScheduleTypeID: { scheduleItem = new LinearScheduleItem(); break; } } return scheduleItem; } }
is not a factory method creation pattern by my "Tech" lead without telling me why or giving me her interpretation. I kindly asked for an explanation and she told me she didn't have time. I was told to just rename it. If I am wrong, then I will no doubt accept that I have implemented this incorrectly for years. Is this how YOU would implement the factory method creation pattern? Thanks in advance.
推荐答案
I would agree to call the method a "Factory Method", though the design is not strictly a "Factory Method Pattern".
Here is a key point (from Wikipedia):
...The Factory method lets a class defer instantiation to subclasses."
Since your class is static and method static (hence non-virtual), there is no "deferring" possible.
Conceptually, notice also, that this implementation, though provides encapsulation, does not decouple/delay any decision.
Having said that, same Wikipedia article does present this schema as a variant of the "Factory Method Pattern".
Summary of the Summary: In my opinion this snippet is not a proper implementation of the "Factory Method OO Design Pattern", since it does not satisfy "a class defer instantiation to subclasses." Though, personally I would freely refer to this solution as "factory method".
To make it real factory method pattern, you need to allow the method to be overridden by subclasses. I.e. factory class (ScheduleTypeFactory) needs to be extensible (i.e. non-static), and GetScheduleItem needs to be virtual.