Java设计模式——抽象工厂模式

抽象工厂模式也是创建模式,可以把它理解成创建工厂的工厂,这种模式也是我们经常使用的。
在抽象工厂中的接口是用来创建工厂的,每个生成的工厂又都可以按照工厂模式创建其他对象。

举例说明:

  1. 创建Shape接口及其实现类Circle、Square、RoundedRectangle、RoundedSquare
  2. 创建抽象工厂类AbstractFactory
  3. 创建工厂类ShapeFactory,它继承AbstractFactory抽象类
  4. 创建工厂生成类FactoryProducer
  5. 创建AbstractFactoryPatternDemo类,用来测试抽象工厂模式

1、创建接口Shape

1
2
3
public interface Shape {
   void draw();
}

2、创建接口Shape实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Rectangle implements Shape {
   @Override
   public void draw() {
      System.out.println("Inside Rectangle::draw() method.");
   }
}
 
public class Square implements Shape {
   @Override
   public void draw() {
      System.out.println("Inside Square::draw() method.");
   }
}
 
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.");
   }
}

3、创建抽象类AbstractFactory

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

4、创建抽象类AbstractFactory的实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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、创建工厂生成类FactoryProducer

1
2
3
4
5
6
7
8
9
public class FactoryProducer {
   public static AbstractFactory getFactory(boolean rounded){  
      if(rounded){
         return new RoundedShapeFactory();        
      }else{
         return new ShapeFactory();
      }
   }
}  

6、测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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.

 

posted @   iceriver315  阅读(814)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示