Design Pattern 简明教程

Design Pattern - Abstract Factory Pattern

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

Abstract Factory patterns work around a super-factory which creates other factories. This factory is also called as factory of factories. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

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

In Abstract Factory pattern an interface is responsible for creating a factory of related objects without explicitly specifying their classes. Each generated factory can give the objects as per the Factory pattern.

Implementation

我们将创建一个形状接口和一个实现它的具体类。我们创建 AbstractFactory 抽象工厂类作为下一步。定义工厂类 ShapeFactory,它扩展了 AbstractFactory。创建了一个工厂创建器/生成器类 FactoryProducer。

We are going to create a Shape interface and a concrete class implementing it. We create an abstract factory class AbstractFactory as next step. Factory class ShapeFactory is defined, which extends AbstractFactory. A factory creator/generator class FactoryProducer is created.

AbstractFactoryPatternDemo,我们的演示类使用 FactoryProducer 获取 AbstractFactory 对象。它将信息(形状的 CIRCLE / RECTANGLE / SQUARE)传递给 AbstractFactory 以获取它需要的对象类型。

AbstractFactoryPatternDemo, our demo class uses FactoryProducer to get a AbstractFactory object. It will pass information (CIRCLE / RECTANGLE / SQUARE for Shape) to AbstractFactory to get the type of object it needs.

abstractfactory pattern uml diagram

Step 1

为形状创建一个接口。

Create an interface for Shapes.

Shape.java

public interface Shape {
   void draw();
}

Step 2

创建实现相同接口的具体类。

Create concrete classes implementing the same interface.

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.");
   }
}

Step 3

创建一个抽象类来获取普通和圆形形状对象的工厂。

Create an Abstract class to get factories for Normal and Rounded Shape Objects.

AbstractFactory.java

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

Step 4

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

Create Factory classes extending AbstractFactory to generate object of concrete class based on given information.

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

Step 5

创建一个工厂生成器/生产者类通过传递诸如形状的信息来获取工厂。

Create a Factory generator/producer class to get factories by passing an information such as Shape

FactoryProducer.java

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

Step 6

使用 FactoryProducer 获取 AbstractFactory 以通过传递诸如类型的信息来获取具体类的工厂。

Use the FactoryProducer to get AbstractFactory in order to get factories of concrete classes by passing an information such as 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();

   }
}

Step 7

验证输出。

Verify the output.

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