工厂模式
来源:互联网 发布:centos防火墙添加端口 编辑:程序博客网 时间:2024/05/16 16:12
简单工厂模式
1. 目的
工厂模式就是专门负责将大量有共同接口的类实例化,而且不必事先知道每次是要实例化哪一个类的模式。它定义一个用于创建对象的接口,由子类决定实例化哪一个类。
2 . 简单工厂模式的结构
3. 一个简单例子
java 代码
- // 产品接口
- public interface Product {
- public void getName();
- }
- // 具体产品A
- public class ProductA implements Product {
- public void getName() {
- System.out.println(" I am ProductA ");
- }
- }
- // 具体产品B
- public class ProductB implements Product {
- public void getName() {
- System.out.println(" I am ProductB ");
- }
- }
- // 工厂类
- public class ProductCreator {
- public Product createProduct(String type) {
- if (" A ".equals(type)) {
- return new ProductA();
- }
- if (" B ".equals(type)) {
- return new ProductB();
- } else
- return null;
- }
- public static void main(String[] args) {
- ProductCreator creator = new ProductCreator();
- creator.createProduct(" A ").getName();
- creator.createProduct(" B ").getName();
- }
- }
• 在编码时不能预见需要创建哪一种类的实例。
• 一个类使用它的子类来创建对象。
• 开发人员不希望创建了哪个类的实例以及如何创建实例的信息暴露给外部程序。
抽象工厂模式
1. 抽象工厂模式可以说是简单工厂模式的扩展,它们主要的区别在于需要创建对象的复杂程度上。
在抽象工厂模式中,抽象产品可能是一个或多个,从而构成一个或多个产品族。 在只有一个产品族的情况下,抽象工厂模式实际上退化到工厂方法模式。
2. 抽象工厂模式的结构
3. 一个简单例子
java 代码
- // 产品 Plant接口
- public interface Plant {
- }
- // 具体产品PlantA,PlantB
- public class PlantA implements Plant {
- public PlantA() {
- System.out.println(" create PlantA ! ");
- }
- public void doSomething() {
- System.out.println(" PlantA do something ");
- }
- }
- public class PlantB implements Plant {
- public PlantB() {
- System.out.println(" create PlantB ! ");
- }
- public void doSomething() {
- System.out.println(" PlantB do something ");
- }
- }
- // 产品 Fruit接口
- public interface Fruit {
- }
- // 具体产品FruitA,FruitB
- public class FruitA implements Fruit {
- public FruitA() {
- System.out.println(" create FruitA ! ");
- }
- public void doSomething() {
- System.out.println(" FruitA do something ");
- }
- }
- public class FruitB implements Fruit {
- public FruitB() {
- System.out.println(" create FruitB ! ");
- }
- public void doSomething() {
- System.out.println(" FruitB do something ");
- }
- }
- // 抽象工厂方法
- public interface AbstractFactory {
- public Plant createPlant();
- public Fruit createFruit();
- }
- // 具体工厂方法
- public class FactoryA implements AbstractFactory {
- public Plant createPlant() {
- return new PlantA();
- }
- public Fruit createFruit() {
- return new FruitA();
- }
- }
- public class FactoryB implements AbstractFactory {
- public Plant createPlant() {
- return new PlantB();
- }
- public Fruit createFruit() {
- return new FruitB();
- }
- }
在以下情况下,应当考虑使用抽象工厂模式。
首先,一个系统应当不依赖于产品类实例被创立,组成,和表示的细节。这对于所有形态的工厂模式都是重要的。
其次,这个系统的产品有多于一个的产品族。
第三,同属于同一个产品族的产品是设计成在一起使用的。这一约束必须得在系统的设计中体现出来。
最后,不同的产品以一系列的接口的面貌出现,从而使系统不依赖于接口实现的细节。
其中第二丶第三个条件是我们选用抽象工厂模式而非其它形态的工厂模式的关键性条件。
- 工厂模式 -- 工厂方法
- 工厂模式 -- 抽象工厂
- 工厂模式-简单工厂
- 工厂模式-工厂方法
- 工厂模式:静态工厂
- 工厂模式-简单工厂
- 工厂模式-抽象工厂
- 工厂模式-工厂方法
- 工厂 > 工厂方法模式
- 工厂模式-静态工厂
- 工厂模式-抽象工厂
- 工厂模式
- 工厂模式
- 工厂模式
- 工厂模式
- 工厂模式
- 工厂模式
- 工厂模式
- MFC使用ADO访问MSSQL2005数据库
- Wince5.0下如何将Public和Private下的代码移植到BSP下
- 美女,僧人,腐尸,菩萨美女
- 下载网站快捷方式
- Source Insight中查看文件显示全路径
- 工厂模式
- 开始谈谈设计模式,一并学习巩固
- operation of db block
- Tomcat项目部署方式
- linux mount命令备忘--指定编码
- 图标按钮、位图按钮、CBitmapButton类位图按钮、按钮的自绘制
- 关于最近的碎念
- inno setup 安装包制作工具
- sqlserver 2008 R2 故障恢复移动文件