首页 > 科技 >

《JAVA与模式》之抽象工厂模式(3)

2018-10-27 06:21:36 网络整理 阅读:73 评论:0

* CPU插槽的孔数

*/

private int cpuHoles = 0;

/**

* 构造方法,传入CPU插槽的孔数

* @param cpuHoles

*/

public AmdMainboard(int cpuHoles){

this.cpuHoles = cpuHoles;

}

@Override

public void installCPU() {

// TODO Auto-generated method stub

System.out.println("AMD主板的CPU插槽孔数是:" + cpuHoles);

}

}

CPU与主板工厂类public class CpuFactory {

public static Cpu createCpu(int type){

Cpu cpu = null;

if(type == 1){

cpu = new IntelCpu(755);

}else if(type == 2){

cpu = new AmdCpu(938);

}

return cpu;

}

}

public class MainboardFactory {

public static Mainboard createMainboard(int type){

Mainboard mainboard = null;

if(type == 1){

mainboard = new IntelMainboard(755);

}else if(type == 2){

mainboard = new AmdMainboard(938);

}

return mainboard;

}

}

装机工程师类与客户类运行结果如下:public class ComputerEngineer {

/**

* 定义组装机需要的CPU

*/

private Cpu cpu = null;

/**

* 定义组装机需要的主板

*/

private Mainboard mainboard = null;

public void makeComputer(int cpuType , int mainboard){

/**

* 组装机器的基本步骤

*/

//1:首先准备好装机所需要的配件

prepareHardwares(cpuType, mainboard);

//2:组装机器

相关文章