这是工厂方法模式的创建? [英] Is this Factory Method creation pattern?
问题描述
我一直在使用工厂方法模式创造了一段时间了。我最近刚告诉记者,这样的:
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".
摘要摘要:一个类的实例化延迟到子类在我看来这片段是不是的正确的实施工厂法OO设计模式,因为它不符合虽然,个人我想自由地参考这个解决方案是工厂法。
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".
要使它的真正的工厂方法模式,你需要允许方法被子类覆盖。即工厂类(ScheduleTypeFactory)需要可扩展的(即非静态)和GetScheduleItem需要是虚的。
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.
这篇关于这是工厂方法模式的创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!