简单的工厂与工厂方法:工厂与客户端中的switch语句 [英] Simple Factory vs Factory Method: Switch statement in factory vs. client

查看:176
本文介绍了简单的工厂与工厂方法:工厂与客户端中的switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,与简单工厂相比,工厂方法的主要优点之一是它不违反开放式SOLID原则.也就是说,添加新类型时,前者不需要修改switch语句.

I understand that one of the main advantages of the Factory Method over Simple Factory is that it doesn't violate the Open-Closed SOLID Principle. That is, the former doesn't require modifying the switch statement when new types are added.

我希望对其中的一件事进行澄清.如果我要使用一个简单的工厂,那么我将拥有一个这样的工厂(简化):

There is one piece on which I am hoping to get clarification. If I were to use a simple factory, I would have a factory like this (simplified):

public class ObjectFactory {
    public static IObject CreateObject(ObjectTypeEnum objectType) {
        switch (objectType) {
            case TypeA:
                return ObjectA;
                break;
            case TypeB:
                return ObjectB;
                break;
            case TypeC:
                return ObjectC;
                break;
        }
    }
}

,客户会这样称呼它:

IObject myObject = ObjectFactory.CreateObject(objectType);

文献中的缺点是,当添加新的对象类型时,将需要修改CreateObject.

The disadvantage in the literature is that CreateObject will need to be modified when new object types are added.

但是使用Factory方法,我们不只是将这种修改从工厂移动到客户端,就像这样(客户端代码):

But with the Factory Method, wouldn't we just be moving this modification from the factory to the client, like this (client code):

IObject myObject;
switch (objectType) {
            case TypeA:
                myObject = ObjectAFactory.CreateObject();
                break;
            case TypeB:
                myObject = ObjectBFactory.CreateObject();
                break;
            case TypeC:
                myObject = ObjectCFactory.CreateObject();
                break;
}

在这种情况下,每次添加新类型时都需要修改客户端,而在以前的情况下,则需要修改工厂.那么,一个相对于另一个的优势是什么?请不要将其标记为重复,我看过许多关于工厂的SO帖子,但都没有解决这一特定区别.

In this case the client will need to be modified each time a new type is added, versus in the previous case the factory would need to be modified. So what is the advantage then of one over the other? Please don't mark this as duplicate, I have looked at many SO posts about factories and none address this specific distinction.

是否有更好的解决方案在客户或工厂方都没有违反开放/封闭原则?

Is there a better solution that doesn't violate the Open/Closed Principle on either the client or factory side?

推荐答案

标准的Abstract Factory设计模式没有帮助吗?
简化的Java代码:

The standard Abstract Factory design pattern doesn't help?
Simplified Java code:

public interface IFactory {
    IObject createObject();
}

public class FactoryA implements IFactory {
    public IObject createObject() {
        return new ObjectA();
    }
}

public class FactoryB implements IFactory {
    public IObject createObject() {
        return new ObjectB();
    }
}

Client is configured (injected) with the needed Factory at run-time
    IFactory myFactory = ...   // new FactoryA();
...
IObject  myObject = myFactory.createObject();
...

另请参见 http://w3sdesign.com 上的GoF设计模式存储器/抽象工厂.

See also the GoF Design Patterns Memory / Abstract Factory at http://w3sdesign.com.

变种2
不要使用枚举对象类型,而应使用多态来定义对象类型(避免使用switch语句). 简化的Java代码:

VARIANT 2
Instead of using enum object types, define your object types with polymorphism (to avoid switch statements). Simplified Java code:

public interface IObjectType {
    int getObjectType();
    IObject createObject();
}

public class ObjectTypeA implements IObjectType {
    ...
    public IObject createObject() {
        return new ObjectA();
    }
}

public class ObjectTypeB implements IObjectType {
    ...
    public IObject createObject() {
        return new ObjectB();
    }
}

Client determines object type 
IObjectType myObjectType = ...   // new ObjectTypeA();
...
IObject  myObject = myObjectType.createObject();
...

我的结论:
我认为,更好的解决方案是使用多态来设计类型,而不是使用枚举常量. 这样可以避免使用switch语句,也不会违反客户端或工厂端的打开/关闭原则".

My conclusion:
I think, a better solution would be to design your types with polymorphism instead of using enum constans. This would avoid switch statements and wouldn't violate the Open/Closed Principle on either the client or factory side.

这篇关于简单的工厂与工厂方法:工厂与客户端中的switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆