英文链接源链接 [7593]
contenteditable="true"

Abstract Factory Pattern 介绍

抽象工厂模式围绕创建其他工厂的超级工厂工作。该工厂也称为工厂工厂。这种设计模式属于创建模式,因为该模式提供了创建对象的最佳方法之一。

在"抽象工厂"模式中,接口负责创建相关对象的工厂,而无需显式指定其类。每个生成的工厂都可以按照Factory模式提供对象。

实作

我们将创建一个 Shape 和 Color 接口以及实现这些接口的具体类。下一步,我们创建一个抽象工厂类 AbstractFactory 。工厂类 ShapeFactory 和 ColorFactory 被定义为每个工厂扩展 AbstractFactory 的位置。创建工厂创建者/生成器类 FactoryProducer 。

AbstractFactoryPatternDemo ,我们的演示类使用 FactoryProducer 来获取 AbstractFactory 对象。它将信息(形状的 CIRCLE/RECTANGLE/SQUARE )传递给 AbstractFactory ,以获取所需的对象类型。它还将信息(对于颜色为红色/绿色/蓝色)传递给 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.");
   }
}

第三步

创建一个Abstract类以获取法线和圆形对象的工厂。

AbstractFactory.java

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

第四步

创建扩展AbstractFactory的Factory类,以根据给定的信息生成具体类的对象。

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步

创建Factory生成器/生产者类以通过传递诸如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 rounded shape factory
      AbstractFactory shapeFactory = FactoryProducer.getFactory(false);
      //get an object of Shape Rounded Rectangle
      Shape shape1 = shapeFactory.getShape("RECTANGLE");
      //call draw method of Shape Rectangle
      shape1.draw();
      //get an object of Shape Rounded Square 
      Shape shape2 = shapeFactory.getShape("SQUARE");
      //call draw method of Shape Square
      shape2.draw();
      //get rounded 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.
点我分享笔记