java - 使工厂方法更具动态性

标签 java oop design-patterns dynamic

我正在我的代码中实现工厂模式,所以在工厂中遇到一件有趣的事情,我可以用反射替换工厂方法中的 if else 条件,使我的代码更加动态。

下面是两种设计的代码......

1) if-else 条件

   public static Pizza createPizza(String type) {

        Pizza pizza = null;

            if(type.equals(PizzaType.Cheese))
            {
                pizza = new CheesePizza();
            }

            else if (type.equals(PizzaType.Tomato))
            {
                pizza = new TomatoPizza();
            }

            else if (type.equals(PizzaType.Capsicum))
            {
                pizza = new CapsicumPizza();
            }
            else
            {
                try {
                    throw new Exception("Entered PizzaType is not Valid");
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }


        return pizza;
    }

2) 有反射

    public static Pizza createPizza(String type) {

            Pizza pizza = null;

            for(PizzaType value : PizzaType.values())
            {
                if(type.equals(value.getPizzaTypeValue()))
                {
                    String fullyQualifiedclassname = value.getClassNameByPizzaType(type);

                    try {
                        pizza = (Pizza)Class.forName(fullyQualifiedclassname).newInstance();
                    } catch (InstantiationException | IllegalAccessException
                            | ClassNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            return pizza;
        }

第二种方式对我来说非常好,因为我可以通过使用它使我的代码更具动态性,因为我可以创建一个属性文件,其中包含与其关联的类和类型的名称,以服务于更多的打开和关闭,如如果将来所有者想要向 PizzaStore 添加更多比萨饼,那么他只需在属性文件中添加条目,然后再创建一个 Pizza 子类。

但我读到反射有很多缺点,其中很少提到。

It is hack of compiler
Automatic development tools are not able to work with reflections code
It's difficult to debug reflections code
Reflection complicates understanding and navigation in code
Significant performance penalty

非常想知道哪种设计好,因为我非常有兴趣让我的代码越来越动态。

最佳答案

您也可以在中间使用设计。例如

public interface Factory<T> {
  public T newInstance();
}

public class TomatoPizzaFactory implements Factory<TomatoPizza> {

    @Override
    public TomatoPizza newInstance() {
        return new TomatoPizza();
    }
}

public class PizzaFactory {

    private Map<String, Factory<? extends Pizza>> factories = new HashMap<String, Factory<? extends Pizza>>();

    public PizzaFactory(){
        factories.put(PizzaType.Cheese, new CheesePizzaFactory());
        factories.put(PizzaType.Tomato, new TomatoPizzaFactory());
    }

    public Pizza createPizza(String type){
        Factory<? extends Pizza> factory = factories.get(type);
        if(factory == null){
           throw new IllegalArgumentException("Unknown pizza type");
        }
        return  factory.newInstance();
    }
}

为简单的实例化实现一个 DefaultConstructorFactory

public class DefaultConstructorFactory<T> implements Factory<T> {

    private Class<T> type;

    public DefaultConstructorFactory(Class<T> type) {
      this.type = type;
    }

    public T newInstance() {
       try {
         return type.newInstance();
       } catch (InstantiationException e) {
         throw new IllegalStateException("Can not instantiate " + type, e);
       } catch (IllegalAccessException e) {
         throw new IllegalStateException("Can not instantiate " + type, e);
    }
  }
}

But I read that reflection has many disadvantages mentioned few of them.

It is hack of compiler

这可能是一种 hack,但如果您正在编写基础架构代码,您将经常使用反射。尤其是当您编写必须在运行时内省(introspection)类的框架时,因为框架不知道它将在运行时处理的类。想想hibernate、spring、jpa等

Automatic development tools are not able to work with reflections code

没错,因为您将很多编译器问题转移到了运行时。因此,您应该像编译器一样处理反射异常并提供良好的错误消息。

在某些 IDE 中也只有很少的重构支持。因此,在更改反射代码使用的代码时必须小心。

不过,您可以编写测试来快速发现错误。

It's difficult to debug reflections code

这也是对的,因为您不能直接跳转到方法中,您必须调查变量以找出访问了哪个对象的哪个成员。 这有点间接。

Reflection complicates understanding and navigation in code

是的,如果用错了。仅当您不知道在运行时必须处理的类型(基础结构或框架代码)时才使用反射。如果您知道类型并且只想尽量减少编写的代码,请不要使用反射。如果您想尽量减少编写的代码,请选择更好的设计。

Significant performance penalty

我不这么认为。当然,反射调用是间接方法调用,因此必须执行更多代码才能实现与直接方法调用相同的效果。但这样做的开销很小。

关于java - 使工厂方法更具动态性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33466847/

相关文章:

java - 在 Java 中解析 ISO 8601 日期格式,如 2015-06-27T13 :16:37. 363Z

java - 单元化字段是否消耗内存?

java - Java设计类时在ArrayList中添加对象

php - 有没有办法到处避免 $this-> ?

c# - 动态状态机的更多 .net 方法

java - 选择哪种设计? - 优点和缺点

java - 如何丢弃/合并重复的 AWT 事件?

java - 如何禁用按钮一天

c++ - 通过传递不同的参数创建对象

c# - 如何在没有接口(interface)的情况下模拟多重继承?