简体   繁体   English

工厂模式实施

[英]Factory pattern implementation

I need to create one of a number of different objects based on some value and I was advised to look into the Factory patterns. 我需要根据某个值创建许多不同的对象之一,并建议我研究一下Factory模式。 I do not want my client having to decide which object to create or to require hard-coded class names, so after doing some reading I came up with the following (simplified) example: 我不希望我的客户决定要创建哪个对象或要求使用硬编码的类名,因此在阅读后,我想到了以下(简化的)示例:

public class ObjectA : IObject 
{
}

public class ObjectA : IObject 
{
}

public interface IObjectFactory
{
    IObject CreateObject(ObjectCreationParameters p);
}

public abstract ObjectFactory : IObjectFactory
{
    abstract IObject CreateObject(ObjectCreationParameters p);
}

public ConcreteObjectFactory : ObjectFactory
{
    public IObject CreateObject(ObjectCreationParameters p)
    {
        IObject obj;
        switch (p.Value)
        {
            case A:
              obj = new ObjectA();
              break;
            case A:
              obj = new ObjectB()
              break;
        }
        return obj;
    }
}

The above works but I am a little confused as to whether my implementation is correct or not. 上面的方法有效,但是我对自己的实现是否正确感到有些困惑。

I would prefer not to have an ObjectAFactory and an ObjectBFactory as in a Factory Method pattern if it can be avoided, however, my object hierarchy does not seem to follow the same object hierarchy as in example of the Abstract Factory pattern. 如果可以避免,我宁愿不要像Factory Method模式那样具有ObjectAFactoryObjectBFactory ,但是,我的对象层次结构似乎不遵循与Abstract Factory模式示例相同的对象层次结构。 I do not have an ObjectA2 or ObjectB2 that would ever be created via a ConcreteObject2Factory . 我没有通过ConcreteObject2Factory创建的ObjectA2ObjectB2

Is my implementation correct or am I doing something wrong and if so, what? 我的实现是正确的还是我做错了什么?

What you really want to look at is Inversion of Control (IoC); 您真正要看的是控制反转(IoC); that is essentially the factory pattern but taken to its natural conclusion. 这本质上是工厂模式,但可以自然得出结论。

Simply put you register all of your types with an IoC container and then delegate the task of creating them over to that. 只需将您的所有类型注册到IoC容器,然后将创建它们的任务委托给该容器即可。 In a nice modern application you infrequently create objects (and almost never explicitly) and you never wory about their dependencies, it all gets done for you. 在一个不错的现代应用程序中,您很少创建对象(并且几乎从来没有明确地创建对象),并且您从不担心它们的依赖关系,所有这些都可以为您完成。

In C# my favourite IoC container is Autofac I've been using it for quite a while in a rowing project and it pretty much just keeps on doing what it needs to do. 在C#中,我最喜欢的IoC容器是Autofac,我在划船项目中已经使用了一段时间了,它几乎一直在做需要做的事情。 There are others, but I'd say it is a good place to start. 还有其他一些,但我想说这是一个不错的起点。

Yes, it is a correct implementation. 是的,这是正确的实现。 This switch statement may seem to be problematic and non-polymorphic but in fact, you can read about it in an excellent Code Clean book , rule G23: Prefer Polymorphism to If/Else or Switch/Case : 这个switch语句似乎是有问题的且不是多态的,但是实际上,您可以在出色的Code Clean书中阅读它,规则G23:与If / Else或Switch / Case相比,建议多态

“ONE SWITCH” rule: There may be no more than one switch statement for a given type of selection. “一个开关”规则:对于一种给定的选择类型,最多只能有一个开关语句。 The cases in that switch statement must create polymorphic objects that take the place of other such switch statements in the rest of the system. 该switch语句中的案例必须创建多态对象,该对象要在系统的其余部分中替代其他此类switch语句。

Your implementation abstracts creation of concrete object, co client don't have to worry about it and that is what this design pattern is for. 您的实现抽象了具体对象的创建,共同客户不必担心它,这就是该设计模式的目的。 Look also, for example, at Encapsulation section in the Wikipedia entry . 例如,也请参阅Wikipedia条目中的封装部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM