- 设计模式教程
- 设计模式 - 主页
- 设计模式 - 概述
- 设计模式-工厂模式
- 抽象工厂模式
- 设计模式-单例模式
- 设计模式-构建器模式
- 设计模式 - 原型模式
- 设计模式-适配器模式
- 设计模式-桥接模式
- 设计模式-过滤器模式
- 设计模式-复合模式
- 设计模式 - 装饰模式
- 设计模式-外观模式
- 设计模式-享元模式
- 设计模式-代理模式
- 责任链模式
- 设计模式-命令模式
- 设计模式-解释器模式
- 设计模式-迭代器模式
- 设计模式——中介者模式
- 设计模式 - 纪念品模式
- 设计模式-观察者模式
- 设计模式-状态模式
- 设计模式-空对象模式
- 设计模式-策略模式
- 设计模式-模板模式
- 设计模式-访客模式
- 设计模式-MVC模式
- 业务代表模式
- 复合实体模式
- 数据访问对象模式
- 前控制器模式
- 拦截过滤器模式
- 服务定位器模式
- 传输对象模式
设计模式-抽象工厂模式
抽象工厂模式围绕创建其他工厂的超级工厂工作。这个工厂也被称为工厂的工厂。这种类型的设计模式属于创建模式,因为该模式提供了创建对象的最佳方法之一。
在抽象工厂模式中,接口负责创建相关对象的工厂,而无需显式指定它们的类。每个生成的工厂都可以根据工厂模式提供对象。
执行
我们将创建一个 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.");
}
}
RoundedSquare.java
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 的工厂类,以根据给定信息生成具体类的对象。
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 等信息来获取工厂
工厂生产者.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.