Java设计模式——抽象工厂模式
抽象工厂模式也是创建模式,可以把它理解成创建工厂的工厂,这种模式也是我们经常使用的。
在抽象工厂中的接口是用来创建工厂的,每个生成的工厂又都可以按照工厂模式创建其他对象。
举例说明:
- 创建Shape接口及其实现类Circle、Square、RoundedRectangle、RoundedSquare
- 创建抽象工厂类AbstractFactory
- 创建工厂类ShapeFactory,它继承AbstractFactory抽象类
- 创建工厂生成类FactoryProducer
- 创建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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」