设计模式 - 抽象工厂模式

抽象工厂模式围绕着一个创造其他工厂的超级工厂.这家工厂也被称为工厂工厂.这种类型的设计模式属于创建模式,因为此模式提供了创建对象的最佳方法之一.

在抽象工厂模式中,接口负责创建相关对象的工厂而不用明确指定他们的类.每个生成的工厂都可以按照工厂模式给出对象.

实现

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

AbstractFactoryPatternDemo ,我们的演示类使用 FactoryProducer 来得到一个 AbstractFactory 对象.它会将信息( CIRCLE/RECTANGLE/SQUARE Shape )传递给 AbstractFactory 以获取所需的对象类型.它还将信息( RED/GREEN/BLUE 用于 Color )传递给 AbstractFactory 以获取所需对象的类型.

抽象工厂模式UML图

步骤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

创建一个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.