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

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



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);
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值