設計模式 - 抽象工廠模式



抽象工廠模式圍繞一個超級工廠,該工廠建立其他工廠。這個工廠也稱為工廠的工廠。這種設計模式屬於建立型模式,因為它提供了一種建立物件的最優方法。

在抽象工廠模式中,一個介面負責建立相關物件的工廠,而無需顯式指定它們的類。每個生成的工廠都可以根據工廠模式提供物件。

實現

我們將建立一個 Shape 介面和一個實現它的具體類。下一步,我們建立抽象工廠類 AbstractFactory。定義工廠類 ShapeFactory,它擴充套件 AbstractFactory。建立了一個工廠建立者/生成器類 FactoryProducer。

AbstractFactoryPatternDemo,我們的演示類使用 FactoryProducer 獲取 AbstractFactory 物件。它將資訊(CIRCLE / RECTANGLE / SQUARE 用於 Shape)傳遞給 AbstractFactory 以獲取它需要的物件型別。

Abstract Factory Pattern UML Diagram

步驟 1

為形狀建立介面。

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

建立一個抽象類以獲取普通和圓形形狀物件的工廠。

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,以便透過傳遞資訊(如型別)來獲取具體類的工廠。

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.
廣告