专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 抽象工厂设计模式详解

抽象工厂设计模式详解

更新时间:2021-10-18 13:40:56 来源:动力节点 浏览859次

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

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

执行

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

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

第1步

为 Shapes 创建一个界面。

形状.java

public interface Shape {
   void draw();
}

第2步

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

圆角矩形.java

public class RoundedRectangle implements Shape {
   @Override
   public void draw() {
      System.out.println("Inside RoundedRectangle::draw() method.");
   }
}
public class RoundedSquare implements Shape {
   @Override
   public void draw() {
      System.out.println("Inside RoundedSquare::draw() method.");
   }
}

矩形.java

public class Rectangle implements Shape {
   @Override
   public void draw() {
      System.out.println("Inside Rectangle::draw() method.");
   }
}

第3步

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

抽象工厂.java

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

第4步

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

形状工厂

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;
   }
}
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等信息来获取工厂

工厂生产者.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.

以上就是抽象工厂设计模式详解,如果您想了解更多关于Java的知识,不妨来关注一下动力节点的Java在线学习,里面有更多的教程在等着大家,希望对大家的学习能够有所帮助。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>