设计模式 - 抽象工厂模式



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

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

实现

我们将创建一个 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.
广告