2

次のケースの親クラスと 2 つの子クラスがあるとします。各子は、親から継承されたパラメーターに新しいパラメーターを追加します。例

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;
}
}

ここでファクトリメソッドを使用するにはどうすればよいですか?

4

3 に答える 3

3

ここでは、「純粋な」ファクトリまたはファクトリ メソッド パターンを使用することはできません。これらのパターンは、インスタンスの作成メカニズムが類似している場合に、同じ基本クラス (またはインターフェイス) の異なるサブクラスのインスタンスを作成する場合に適しています。たとえば、すべてのクラスには、同じプロトタイプを持つコンストラクターまたはファクトリー メソッドがあります。

この場合、リフレクションまたは省略記号を使用できます。

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());
    }
}
于 2013-01-09T19:28:06.683 に答える
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おそらく、インターフェイスを実際に使用する必要はありません。

于 2013-01-09T19:25:54.447 に答える
0

以下のように、最初の子または 2 番目の子を生成するファクトリ クラスがある場合があります。

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

于 2013-01-09T19:25:34.547 に答える