`

读《研磨设计模式》-代码笔记-抽象工厂模式-Abstract Factory

 
阅读更多
声明:
本文只为方便我个人查阅和理解,详细的分析以及源代码请移步 原作者的博客http://chjavach.iteye.com/



package design.pattern;

/*
 * Abstract Factory Pattern
 * 抽象工厂模式的目的是:
 * 通过在抽象工厂里面定义一组产品接口,方便地切换“产品簇”
 * 这些接口是相关或者相依赖的,组合在一起形成一个完整的“产品簇”
 * 可以认为抽象工厂的一个子类(实现)就是一个“产品”
 * 
 * 书上以“组装电脑“为例:
 * 电脑有一些必须的组件,例如CPU、主板等等
 * CPU有很多种,主板也有很多种,不同的组合就组成不同的电脑
 * 要注意CPU和主板的兼容性
 * 
 * 定义一个电脑接口:
 * interface IComputer{
 * 		ICPU createCPU();
 * 		IMainboard createMainboard();
 * }
 * 
 * 组装电脑时可自由选择CPU和主板的组合(只要两者兼容):
 * class ComputerA implements IComputer {
 * 		ICPU createCPU() {
 * 			//在这里可自由选择CPU的实现
 * 		}
 * 		IMainboard createMainboard() {
 * 			//在这里可自由选择主板的实现
 * 		}
 * }
 * 那么就可以实现不同的“产品簇”:ComputerB,ComputerC...
 * 要注意的是,“自由选择CPU和主板”是装机人员的权利(要确保兼容性)
 * 而消费者只能“自由选择一种电脑”(例如ComputerA或者ComputerB)
 * 
 * 由此也可看出,抽象工厂完全贯彻了“面向接口编程”的原则
 * 
 * 书上讲到一个扩展:如果要添加子产品怎么办?例如在电脑组装方案里面添加一个显示器的选择:
 * interface IComputer{
 * 		ICPU createCPU();
 * 		IMainboard createMainboard();
 * 		IScreen createScreen();
 * }
 * 提供的解决方案是,把抽象工厂弄得更抽象、更笼统:
 * interface IComputer {
 * 		Object createComputerComponent(int type);		//由type决定创建的是哪种电脑组件:CPU,主板...
 * }
 * 
 * 书上讲到的另一个扩展是DAO(Data Access Object)
 * 使用DAO可以较方便地切换数据库
 * DAO一直在用,但此前没有意识到这是抽象工厂模式。。
 */


interface IAbstractFactory {
	
	//type=1表示创建CPU type=2表示创建主板 type=3表示创建显示器
	public Object createComponent(int type);
	
}


interface ICPU {
	
	public void operationOfCPU();
	
}


interface IMainboardd {
	
	public void operationOfMainboard();
	
}


class IntelCPU implements ICPU {
	
	public void operationOfCPU() {
		System.out.println("Intel cpu operation.");
	}
	
}


class AMDCPU implements ICPU {
	
	public void operationOfCPU() {
		System.out.println("AMD cpu operation.");
	}
	
}


class WeiXingMainboard	implements IMainboardd {
	
	public void operationOfMainboard() {
		System.out.println("微星主板的操作");
	}
	
}


class JiJiaMainboard implements IMainboardd {
	
	public void operationOfMainboard() {
		System.out.println("技嘉主板的操作");
	}
	
}


//组装方案1:Intel的CPU + 技嘉主板
class Schema1 implements IAbstractFactory {

	public Object createComponent(int type) {
		Object obj = null;
		if (type == 1) {
			obj = new IntelCPU();
		} else if (type == 2){
			obj = new JiJiaMainboard();
		}
		return obj;
	}
	
}


//组装方案2:AMD的CPU + 微星主板
class Schema2 implements IAbstractFactory {
	
	public Object createComponent(int type) {
		Object obj = null;
		if (type == 1) {
			obj = new AMDCPU();
		} else if (type == 2){
			obj = new WeiXingMainboard();
		}
		return obj;
	}
	
}


//扩展1--组装方案3(前面的方案都没有考虑显示器):Intel的CPU + 技嘉主板 + 三星显示器
class Schema3 implements IAbstractFactory {
	
	public Object createComponent(int type) {
		Object obj = null;
		if (type == 1) {
			obj = new AMDCPU();
		} else if (type == 2){
			obj = new WeiXingMainboard();
		} else if (type == 3) {
			obj = new SamsungScreen();
		}
		return obj;
	}
	
}

interface IScreen {
	
	public void operationOfScreen();
	
}


class SamsungScreen implements IScreen {

	public void operationOfScreen() {
		System.out.println("Samsung screen operation.");
	}
	
}


class ComputerEngineer {
	
	private ICPU cpu;
	private IMainboardd mainboard;
	private IScreen screen;
	
	public void makeComputer(IAbstractFactory schema) {
		
		//1-创建cpu 2-创建主板 3-显示器 不安全的地方在这里,因为需要强制转换
		this.cpu = (ICPU) schema.createComponent(1);
		this.mainboard = (IMainboardd) schema.createComponent(2);
		this.screen = (IScreen) schema.createComponent(3);
		
		this.cpu.operationOfCPU();
		this.mainboard.operationOfMainboard();
		if (this.screen != null) {
			this.screen.operationOfScreen();
		}
	}
	
}


//扩展2-DAO
abstract class DAOFactory {
	
	public abstract IStudentDAO createStudentDAO();
	
	public abstract ITeacherDAO createTeacherDAO();
	
}


interface IStudentDAO {
	
	public void saveStudent(String student);
	
}


interface ITeacherDAO {
	
	public void saveTeacher(String teacher);
	
}


class StudentMysqlDAOImpl implements IStudentDAO {

	public void saveStudent(String student) {
		System.out.println("Save student in mysql.");
	}
	
}


class TeacherMysqlDaoImpl implements IStudentDAO {
	
	public void saveStudent(String teacher) {
		System.out.println("Save teacher in mysql.");
	}
	
}

//^^^^^^以上,业务层调用DAO时,直接调用接口,更换数据库时,提供另一个DAOImpl即可,业务层代码不用变:
class StudentOracleDAOImpl implements IStudentDAO {
	
	public void saveStudent(String student) {
		System.out.println("Save student in oracle.");
	}
	
}


//这个类是用来测试的
public class AbstractFactoryPattern {

	public static void main(String[] args) {
		ComputerEngineer engineer = new ComputerEngineer();
		
		IAbstractFactory schema = new Schema1();
		engineer.makeComputer(schema);
		System.out.println();
		
		schema = new Schema2();
		engineer.makeComputer(schema);
		System.out.println();
		
		schema = new Schema3();
		engineer.makeComputer(schema);
	}

}
1
0
分享到:
评论

相关推荐

    研磨设计模式-配套源代码

    研磨设计模式-配套源代码研磨设计模式-配套源代码

    研磨设计模式-配套源代码.rar

    研磨设计模式-配套源代码.rar

    研磨设计模式-配套源代码.7z

    研磨设计模式-配套源代码.7z

    研磨设计模式-配套源代码 UTF-8格式

    研磨设计模式第二版-配套源代码 UTF-8格式 研磨设计模式第二版-配套源代码 UTF-8格式

    研磨设计模式-陈臣.epub

    “1.1 设计模式是什么 1.1.1 什么是模式 从字面上理解,模,就是模型、模板的意思;式,就是方式、方法的意思。综合起来,所谓模式就是:可以作为模型或模板的方式或方法。... “研磨设计模式”。 iBooks.

    研磨设计模式-陈臣.王斌.扫描高清版PDF

    设计模式(Design Pattern)是一套被反复使用、多数人知晓的、经过分类的、代码设计经验的总结。 使用设计模式的目的:为了代码可重用性、让代码更容易被他人理解、保证代码可靠性。 设计模式使代码编写真正工程化;...

    研磨设计模式-陈臣pdf

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式-part2

    第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 命令模式...

    研磨设计模式-part4

    第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 命令模式...

    研磨设计模式-part3

    第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 命令模式...

    研磨设计模式书-配套源代码(全)

    1:本源代码是《研磨设计模式》一书的配套源代码 2:每个模式的示例源代码放在一个单独的文件夹下,以该模式的英文名称命名 3:每个模式下分成多个example,按照书的示例顺序分别命名为example1、example2.........

    研磨设计模式-陈臣.mobi kindle版

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式--chjavach的博客文章

    单例模式、工厂方法模式、策略模式、命令模式和桥接模式。

    研磨设计模式源码

    研磨设计模式的配套源码,请下载,不要想了,解压后,直接放到eclipse下面即可运行!

    研磨设计模式 源代码

    研磨设计模式 源代码 书上面的例子都在里面

    研磨设计模式(完整带书签).part2.pdf

    第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 命令模式...

    研磨设计模式全部源代码

    研磨设计模式全部源代码,个人收集,仅用学习使用,不可用于商业用途,如有版权问题,请联系删除!

    研磨设计模式带书签完整版228M.7z.001

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式(完整带书签).part1.pdf

    第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章 中介者模式(Mediator) 第11章 代理模式(Proxy) 第12章 观察者模式(Observer) 第13章 命令模式...

    研磨设计模式 完美书签 完整(一)

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

Global site tag (gtag.js) - Google Analytics