design pattern——抽象工厂模式

本文介绍抽象工厂模式的设计理念与实现方式,通过具体示例代码展示了如何利用该模式创建一系列相关或相互依赖的对象,而不指定它们具体的类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 针对问题:和工厂方法模式类似,用一个继承体系来实现创建对象的多变性。不同的是,抽象工厂模式针对的是产品族(许多个产品).

 

 

 

 

 

抽象工厂模式结构图:

 

 

 

 

 

 

抽象工厂模式实现代码:

/**
 * 产品A接口
 * @author bruce
 *
 */
public abstract class ProductA {
	
	public abstract void descriptionA();
}



/**
 * 产品B接口
 * @author bruce
 *
 */
public abstract class ProductB {
	
	public abstract void descriptionB();
}




/**
 * 创建者接口
 * @author bruce
 *
 */
public abstract class Creator {
	
	public abstract ProductA createProductA();
	
	public abstract ProductB createProductB();
}




/**
 * 产品A1(产品A)
 * @author bruce
 *
 */
public class ProductA1 extends ProductA{

	@Override
	public void descriptionA() {
		// TODO Auto-generated method stub
		System.out.println("ProductA:ProductA1");
	}
}

/**
 * 产品A2(产品A)
 * @author bruce
 *
 */
public class ProductA2 extends ProductA{

	@Override
	public void descriptionA() {
		// TODO Auto-generated method stub
		System.out.println("ProductA:ProductA2");
	}

}



/**
 * 产品B1(产品B)
 * @author bruce
 *
 */
public class ProductB1 extends ProductB{
	
	@Override
	public void descriptionB() {
		// TODO Auto-generated method stub
		System.out.println("ProductB:ProductB1");
	}
}


/**
 * 产品B2(产品B)
 * @author bruce
 *
 */
public class ProductB2 extends ProductB{
	
	@Override
	public void descriptionB() {
		// TODO Auto-generated method stub
		System.out.println("ProductB:ProductB2");
	}
}




/**
 * 创建类1
 * @author bruce
 *
 */
public class ConcreteCreator1 extends Creator{

	@Override
	public ProductA createProductA() {
		// TODO Auto-generated method stub
		return new ProductA1();
	}

	@Override
	public ProductB createProductB() {
		// TODO Auto-generated method stub
		return new ProductB1();
	}

}



/**
 * 创建者2
 * @author bruce
 *
 */
public class ConcreteCreator2 extends Creator{

	@Override
	public ProductA createProductA() {
		// TODO Auto-generated method stub
		return new ProductA2();
	}

	@Override
	public ProductB createProductB() {
		// TODO Auto-generated method stub
		return new ProductB2();
	}

}






/**
 * 测试
 * @author bruce
 */
public class Client {
	
	public static void main(String[] args) {
		Creator creator=new ConcreteCreator1();
		ProductA pa=creator.createProductA();
		ProductB pb=creator.createProductB();
		pa.descriptionA(); //ProductA:ProductA1
		pb.descriptionB(); //ProductB:ProductB1
		
		creator=new ConcreteCreator2();
		pa=creator.createProductA();
		pb=creator.createProductB();
		pa.descriptionA(); //ProductA:ProductA2
		pb.descriptionB(); //ProductB:ProductB2
	}

}

 

作者:刘伟 基础知识 基础知识设计模式概述 从招式与内功谈起——设计模式概述(一) 从招式与内功谈起——设计模式概述(二) 从招式与内功谈起——设计模式概述(三) 面向对象设计原则 面向对象设计原则之单一职责原则 面向对象设计原则之开闭原则 面向对象设计原则之里氏代换原则 面向对象设计原则之依赖倒转原则 面向对象设计原则之接口隔离原则 面向对象设计原则之合成复用原则 面向对象设计原则之迪米特法则 六个创建型模式 六个创建型模式 简单工厂模式-Simple Factory Pattern 工厂三兄弟之简单工厂模式(一) 工厂三兄弟之简单工厂模式(二) 工厂三兄弟之简单工厂模式(三) 工厂三兄弟之简单工厂模式(四) 工厂方法模式-Factory Method Pattern 工厂三兄弟之工厂方法模式(一) 工厂三兄弟之工厂方法模式(二) 工厂三兄弟之工厂方法模式(三) 工厂三兄弟之工厂方法模式(四) 抽象工厂模式-Abstract Factory Pattern 工厂三兄弟之抽象工厂模式(一) 工厂三兄弟之抽象工厂模式(二) 工厂三兄弟之抽象工厂模式(三) 工厂三兄弟之抽象工厂模式(四) 工厂三兄弟之抽象工厂模式(五) 单例模式-Singleton Pattern 确保对象的唯一性——单例模式 (一) 确保对象的唯一性——单例模式 (二) 确保对象的唯一性——单例模式 (三) 确保对象的唯一性——单例模式 (四) 确保对象的唯一性——单例模式 (五) 原型模式-Prototype Pattern 对象的克隆——原型模式(一) 对象的克隆——原型模式(二) 对象的克隆——原型模式(三) 对象的克隆——原型模式(四) 建造者模式-Builder Pattern 复杂对象的组装与创建——建造者模式(一) 复杂对象的组装与创建——建造者模式(二) 复杂对象的组装与创建——建造者模式(三) 七个结构型模式 七个结构型模式 适配器模式-Adapter Pattern 不兼容结构的协调——适配器模式(一) 不兼容结构的协调——适配器模式(二) 不兼容结构的协调——适配器模式(三) 不兼容结构的协调——适配器模式(四) 桥接模式-Bridge Pattern 处理多维度变化——桥接模式(一) 处理多维度变化——桥接模式(二) 处理多维度变化——桥接模式(三) 处理多维度变化——桥接模式(四) 组合模式-Composite Pattern 树形结构的处理——组合模式(一) 树形结构的处理——组合模式(二) 树形结构的处理——组合模式(三) 树形结构的处理——组合模式(四) 树形结构的处理——组合模式(五) 装饰模式-Decorator Pattern 扩展系统功能——装饰模式(一) 扩展系统功能——装饰模式(二) 扩展系统功能——装饰模式(三) 扩展系统功能——装饰模式(四) 外观模式-Facade Pattern 深入浅出外观模式(一) 深入浅出外观模式(二) 深入浅出外观模式(三) 享元模式-Flyweight Pattern 实现对象的复用——享元模式(一) 实现对象的复用——享元模式(二) 实现对象的复用——享元模式(三) 实现对象的复用——享元模式(四) 实现对象的复用——享元模式(五) 代理模式-Proxy Pattern 设计模式之代理模式(一) 设计模式之代理模式(二) 设计模式之代理模式(三) 设计模式之代理模式(四) 十一个行为型模式 十一个行为型模式 职责链模式-Chain of Responsibility Pattern 请求的链式处理——职责链模式(一) 请求的链式处理——职责链模式(二) 请求的链式处理——职责链模式(三) 请求的链式处理——职责链模式(四) 命令模式-Command Pattern 请求发送者与接收者解耦——命令模式(一) 请求发送者与接收者解耦——命令模式(二) 请求发送者与接收者解耦——命令模式(三) 请求发送者与接收者解耦——命令模式(四) 请求发送者与接收者解耦——命令模式(五) 请求发送者与接收者解耦——命令模式(六) 解释器模式-Interpreter Pattern 自定义语言的实现——解释器模式(一) 自定义语言的实现——解释器模式(二) 自定义语言的实现——解释器模式(三) 自定义语言的实现——解释器模式(四) 自定义语言的实现——解释器模式(五) 自定义语言的实现——解释器模式(六) 迭代器模式-Iterator Pattern 遍历聚合对象中的元素——迭代器模式(一) 遍历聚合对象中的元素——迭代器模式(二) 遍历聚合对象中的元素——迭代器模式(三) 遍历聚合对象中的元素——迭代器模式(四) 遍历聚合对象中的元素——迭代器模式(五) 遍历聚合对象中的元素——迭代器模式(六) 中介者模式-Mediator Pattern 协调多个对象之间的交互——中介者模式(一) 协调多个对象之间的交互——中介者模式(二) 协调多个对象之间的交互——中介者模式(三) 协调多个对象之间的交互——中介者模式(四) 协调多个对象之间的交互——中介者模式(五) 备忘录模式-Memento Pattern 撤销功能的实现——备忘录模式(一) 撤销功能的实现——备忘录模式(二) 撤销功能的实现——备忘录模式(三) 撤销功能的实现——备忘录模式(四) 撤销功能的实现——备忘录模式(五) 观察者模式-Observer Pattern 对象间的联动——观察者模式(一) 对象间的联动——观察者模式(二) 对象间的联动——观察者模式(三) 对象间的联动——观察者模式(四) 对象间的联动——观察者模式(五) 对象间的联动——观察者模式(六) 246 十一个行为型模式 状态模式-State Pattern 处理对象的多种状态及其相互转换——状态模式(一) 处理对象的多种状态及其相互转换——状态模式(二) 处理对象的多种状态及其相互转换——状态模式(三) 处理对象的多种状态及其相互转换——状态模式(四) 处理对象的多种状态及其相互转换——状态模式(五) 处理对象的多种状态及其相互转换——状态模式(六) 策略模式-Strategy Pattern 算法的封装与切换——策略模式(一) 算法的封装与切换——策略模式(二) 算法的封装与切换——策略模式(三) 算法的封装与切换——策略模式(四) 模板方法模式-Template Method Pattern 模板方法模式深度解析(一) 模板方法模式深度解析(二) 模板方法模式深度解析(三) 访问者模式-Visitor Pattern 操作复杂对象结构——访问者模式(一) 操作复杂对象结构——访问者模式(二) 操作复杂对象结构——访问者模式(三) 操作复杂对象结构——访问者模式(四) 设计模式趣味学习(复习) 设计模式趣味学习(复习) 设计模式与足球(一) 设计模式与足球(二) 设计模式与足球(三) 设计模式与足球(四) 设计模式综合应用实例 设计模式综合应用实例 多人联机射击游戏 多人联机射击游戏中的设计模式应用(一) 多人联机射击游戏中的设计模式应用(二) 数据库同步系统 设计模式综合实例分析之数据库同步系统(一) 设计模式综合实例分析之数据库同步系统(二) 设计模式综合实例分析之数据库同步系统(三)
Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all. The examples are chosen so you will be able to absorb the core concepts easily and quickly. This book presents the topic of design patterns in Java in such a way that anyone can grasp the idea. By giving easy to follow examples, you will understand the concepts with increasing depth. The examples presented are straightforward and the topic is presented in a concise manner. Key features of the book: Each of the 23 patterns is described with straightforward Java code. There is no need to know advanced concepts of Java to use this book. Each of the concepts is connected with a real world example and a computer world example. The book uses Eclipse IDE to generate the output because it is the most popular IDE in this field. This is a practitioner's book on design patterns in Java. Design patterns are a popular topic in software development. A design pattern is a common, well-described solution to a common software problem. There is a lot of written material available on design patterns, but scattered and not in one single reference source. Also, many of these examples are unnecessarily big and complex. Table of Contents Chapter 1: Introduction Chapter 2: Observer Patterns Chapter 3: Singleton Patterns Chapter 4: Proxy Patterns Chapter 5: Decorator Patterns Chapter 6: Template Method Patterns Chapter 7: Strategy Patterns (Or, Policy Patterns) Chapter 8: Adapter Patterns Chapter 9: Command Patterns Chapter 10: Iterator Patterns Chapter 11: Facade Patterns Chapter 12: Factory Method Patterns Chapter 13: Memento Patterns Chapter 14: State Patterns Chapter 15: Builder Patterns Chapter 16: Flyweight Patterns Chapter 17: Abstract Factory Patterns Chapter 18: Mediator Patterns Chapter 19: Prototype Patterns Chapter 20: Chain of Responsibility Patterns Chapter 21: Composite Patterns Chapter 22: Bridge Patterns (Or Handle/Body Patterns) Chapter 23: Visitor Patterns Chapter 24: Interpreter Patterns
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值