Design-pattern-abstract-factory-pattern

提供:Dev Guides
移動先:案内検索

デザインパターン-抽象ファクトリーパターン

Abstract Factoryパターンは、他の工場を作成するスーパー工場を回避します。 この工場は工場の工場とも呼ばれます。 このタイプのデザインパターンは、オブジェクトを作成する最適な方法の1つであるため、作成パターンの下にあります。

Abstract Factoryパターンでは、インターフェイスは、クラスを明示的に指定せずに関連オブジェクトのファクトリを作成する役割を果たします。 生成された各ファクトリは、ファクトリパターンに従ってオブジェクトを提供できます。

実装

Shapeインターフェースとそれを実装する具体的なクラスを作成します。 次のステップとして、抽象ファクトリクラスAbstractFactoryを作成します。 ファクトリクラスShapeFactoryが定義され、AbstractFactoryを拡張します。 ファクトリクリエーター/ジェネレータークラスFactoryProducerが作成されます。

AbstractFactoryPatternDemo、デモクラスはFactoryProducerを使用してAbstractFactoryオブジェクトを取得します。 情報(形状のCIRCLE/RECTANGLE/SQUARE)をAbstractFactoryに渡し、必要なオブジェクトのタイプを取得します。

抽象的なファクトリパターンUML図

ステップ1

Shapesのインターフェイスを作成します。

Shape.java

public interface Shape {
   void draw();
}

ステップ2

同じインターフェースを実装する具体的なクラスを作成します。

RoundedRectangle.java

public class RoundedRectangle implements Shape {
   @Override
   public void draw() {
      System.out.println("Inside RoundedRectangle::draw() method.");
   }
}

RoundedSquare.java

public class RoundedSquare implements Shape {
   @Override
   public void draw() {
      System.out.println("Inside RoundedSquare::draw() method.");
   }
}

Rectangle.java

public class Rectangle implements Shape {
   @Override
   public void draw() {
      System.out.println("Inside Rectangle::draw() method.");
   }
}

ステップ3

Abstractクラスを作成して、NormalおよびRounded Shapeオブジェクトのファクトリを取得します。

AbstractFactory.java

public abstract class AbstractFactory {
   abstract Shape getShape(String shapeType) ;
}

ステップ4

指定された情報に基づいて具象クラスのオブジェクトを生成するために、AbstractFactoryを拡張するファクトリクラスを作成します。

ShapeFactory.java

public class ShapeFactory extends AbstractFactory {
   @Override
   public Shape getShape(String shapeType){
      if(shapeType.equalsIgnoreCase("RECTANGLE")){
         return new Rectangle();
      }else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new Square();
      }
      return null;
   }
}

RoundedShapeFactory.java

public class RoundedShapeFactory extends AbstractFactory {
   @Override
   public Shape getShape(String shapeType){
      if(shapeType.equalsIgnoreCase("RECTANGLE")){
         return new RoundedRectangle();
      }else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new RoundedSquare();
      }
      return null;
   }
}

ステップ5

ファクトリージェネレーター/プロデューサークラスを作成して、Shapeなどの情報を渡すことで工場を取得します。

FactoryProducer.java

public class FactoryProducer {
   public static AbstractFactory getFactory(boolean rounded){
      if(rounded){
         return new RoundedShapeFactory();
      }else{
         return new ShapeFactory();
      }
   }
}

ステップ6

FactoryProducerを使用してAbstractFactoryを取得し、typeなどの情報を渡すことで具体的なクラスのファクトリーを取得します。

AbstractFactoryPatternDemo.java

public class AbstractFactoryPatternDemo {
   public static void main(String[] args) {
     //get shape factory
      AbstractFactory shapeFactory = FactoryProducer.getFactory(false);
     //get an object of Shape Rectangle
      Shape shape1 = shapeFactory.getShape("RECTANGLE");
     //call draw method of Shape Rectangle
      shape1.draw();
     //get an object of Shape Square
      Shape shape2 = shapeFactory.getShape("SQUARE");
     //call draw method of Shape Square
      shape2.draw();
     //get shape factory
      AbstractFactory shapeFactory1 = FactoryProducer.getFactory(true);
     //get an object of Shape Rectangle
      Shape shape3 = shapeFactory1.getShape("RECTANGLE");
     //call draw method of Shape Rectangle
      shape3.draw();
     //get an object of Shape Square
      Shape shape4 = shapeFactory1.getShape("SQUARE");
     //call draw method of Shape Square
      shape4.draw();

   }
}

ステップ7

出力を確認します。

Inside Rectangle::draw() method.
Inside Square::draw() method.
Inside RoundedRectangle::draw() method.
Inside RoundedSquare::draw() method.