【问题标题】:use factory design pattern with subclass that has extra constructor arguments将工厂设计模式与具有额外构造函数参数的子类一起使用
【发布时间】:2013-01-09 19:19:57
【问题描述】:

假设我有以下情况 Parent 类和两个 Child 类,每个子类都向从父类继承的参数添加一个新参数。示例

public class Parent {

private int x;

public Parent(int x) {this.x = x;}

public int getX() {return x;}

public void setX(int x) {this.x = x;}
}

第一个孩子

public class FirstChild extends Parent {

private int y;

public FirstChild(int x, int y) {
    super(x);
    this.y = y;
}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}
}

第二个孩子

public class SecondChild extends Parent{
private int z;

public SecondChild(int x, int z) {
    super(x);
    this.z = z;
}

public int getZ() {
    return z;
}

public void setZ(int z) {
    this.z = z;
}
}

那么我如何在这里使用工厂方法呢?,

【问题讨论】:

    标签: java design-patterns factory


    【解决方案1】:

    您不能在此处使用“纯”工厂或工厂方法模式。当您想要创建相同基类(或接口)的不同子类的实例时,这些模式非常有用,以防创建实例的机制相似。例如,所有类都有具有相同原型的构造函数或工厂方法。

    在这种情况下,您可以使用反射或省略:

    class MyFactory {
        Parent createInstance(Class clazz, int ... args) {
            if (FirstChild.class.equals(clazz)) {
                return new FirstChild(args[0]);
            }
            if (SecondChild.class.equals(clazz)) {
                return new SecondChild(args[0], args[1]);
            }
            throw new IllegalArgumentException(clazz.getName());
        }
    }
    

    【讨论】:

      【解决方案2】:
      interface Factory {
          Parent newParent();
      } 
      
      class FirstFactory implements Factory {
          Parent newParent() { return new FirstChild(); }
      }
      
      class SecondFactory implements Factory {
          Parent newParent() { return new SecondChild(); }
      }
      
      
      class Client {
          public void doSomething() {
              Factory f = ...; // get the factory you need 
              Parent p = f.newParent();
              use(p)
          } 
      
          // or more appropriately
          public void doSomethingElse(Factory f) {
              Parent p = f.newParent();
              use(p)
          }
      
      }
      
      // Or...
      
      abstract class Client {
          public void doSomething() {
              Factory f = getFactory();
              Parent p = f.newParent();
              use(p)
          } 
          abstract Factory getFactory(); 
      }
      
      class FirstClient extends Client {
          Factory getFactory() {
              return new FirstFactory();
          } 
      }
      
      class SecondClient extends Client {
          Factory getFactory() {
              return new SecondFactory();
          } 
      }
      

      或者(可能更适合您的需要):

      public class ChildrenFactory {
          FirstChild newFistChild() { return new FirstChild(); } 
          SecondChild newSecondChild() { return new SecondChild(); } 
          // or
          Parent newFistChild() { return new FirstChild(); } 
          Parent newSecondChild() { return new SecondChild(); } 
      }
      

      可能你真的不需要使用Parent 接口。

      【讨论】:

        【解决方案3】:

        您可能有一个工厂类,它生成如下所示的第一个孩子或第二个孩子

        class Factory{
           Parent createChild(String child) {
              if(child.equals("first"))
                 return new FirstChild();
              if(child.equals("second"))
                 return new SecondChild();
        
        
           }
        }
        

        下面的这个链接也将帮助你更好地理解工厂模式http://www.hiteshagrawal.com/java/factory-design-pattern-in-java

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-12
          相关资源
          最近更新 更多