说明: 1、client只会找到ComputerEngineer,并告诉他用哪个cpu和那个mainboard 2、computerEngineer只会从cpuFactory和mainboardFactory分别获得cpu和mainboard,并分别告诉它们的cpu型号、mainboard型号 3、cpuFactory会生产多种CPU,一种是intel,一种amd mainboardFactory会生产多种mainboard,一种intel,一种amd 4、所有的cpu都实现cpu接口 所有mianboard实现mainboard接口
5、
/** * Project Name:DesignPatterns * File Name:Cpu.java * Package Name:com.louis.simplefactory * Date:2017年9月25日下午9:11:14 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved. * */ package com.louis.simplefactory; /** * ClassName:Cpu * Function: cpu抽象类 * Reason: TODO ADD REASON. * Date: 2017年9月25日 下午9:11:14 * @author michael * @version * @since JDK 1.7 * @see */ public interface Cpu { public void calculate(); }
/** * Project Name:DesignPatterns * File Name:AmdCpu.java * Package Name:com.louis.simplefactory * Date:2017年9月25日下午9:16:34 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved. * */ package com.louis.simplefactory; /** * ClassName:AmdCpu * Function: TODO ADD FUNCTION. * Reason: TODO ADD REASON. * Date: 2017年9月25日 下午9:16:34 * @author michael * @version * @since JDK 1.7 * @see */ public class CpuAmd implements Cpu { private int pins = 0; public CpuAmd(int pins){ this.pins = pins; } @Override public void calculate() { System.out.println("AMD CPU的针脚数:" + pins); } }
/** * Project Name:DesignPatterns * File Name:IntelCpu.java * Package Name:com.louis.simplefactory * Date:2017年9月25日下午9:13:24 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved. * */ package com.louis.simplefactory; /** * ClassName:IntelCpu * Function: TODO ADD FUNCTION. * Reason: TODO ADD REASON. * Date: 2017年9月25日 下午9:13:24 * @author michael * @version * @since JDK 1.7 * @see */ public class CpuIntel implements Cpu { //cpu针脚数 private int pins = 0; public CpuIntel(int pins){ this.pins = pins; } @Override public void calculate() { System.out.println("Intel CPU的针脚数是:"+pins); } }
/** * Project Name:DesignPatterns * File Name:CpuFactory.java * Package Name:com.louis.simplefactory * Date:2017年9月25日下午9:27:39 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved. * */ package com.louis.simplefactory; /** * ClassName:CpuFactory * Function: TODO ADD FUNCTION. * Reason: TODO ADD REASON. * Date: 2017年9月25日 下午9:27:39 * @author michael * @version * @since JDK 1.7 * @see */ public class CpuFactory { public static Cpu createCpu(int type){ Cpu cpu = null; if(type ==1){ cpu = new CpuIntel(755); } else if(type ==2 ){ cpu = new CpuAmd(938); } return cpu; } }
/** * Project Name:DesignPatterns * File Name:Mainboard.java * Package Name:com.louis.simplefactory * Date:2017年9月25日下午9:12:13 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved. * */ package com.louis.simplefactory; /** * ClassName:Mainboard * Function: 主板抽象类 * Reason: TODO ADD REASON. * Date: 2017年9月25日 下午9:12:13 * @author michael * @version * @since JDK 1.7 * @see */ public interface Mainboard { public void installCPU(); }
/** * Project Name:DesignPatterns * File Name:MainboardAmd.java * Package Name:com.louis.simplefactory * Date:2017年9月25日下午9:22:47 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved. * */ package com.louis.simplefactory; /** * ClassName:MainboardAmd * Function: TODO ADD FUNCTION. * Reason: TODO ADD REASON. * Date: 2017年9月25日 下午9:22:47 * @author michael * @version * @since JDK 1.7 * @see */ public class MainboardAmd implements Mainboard { /*CPU插槽的孔数*/ private int cpuHoles = 0; /** * 构造方法,传入CPU插槽的孔数 * @param cpuHoles */ public MainboardAmd(int cpuHoles){ this.cpuHoles = cpuHoles; } @Override public void installCPU() { // TODO Auto-generated method stub System.out.println("AMD主板的CPU插槽孔数是:" + cpuHoles); } }
/** * Project Name:DesignPatterns * File Name:MainboardIntel.java * Package Name:com.louis.simplefactory * Date:2017年9月25日下午9:25:44 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved. * */ package com.louis.simplefactory; /** * ClassName:MainboardIntel Function: TODO ADD FUNCTION. Reason: TODO ADD * REASON. Date: 2017年9月25日 下午9:25:44 * * @author michael * @version * @since JDK 1.7 * @see */ public class MainboardIntel implements Mainboard { /** * CPU插槽的孔数 */ private int cpuHoles = 0; /** * 构造方法,传入CPU插槽的孔数 * * @param cpuHoles */ public MainboardIntel(int cpuHoles) { this.cpuHoles = cpuHoles; } @Override public void installCPU() { // TODO Auto-generated method stub System.out.println("Intel主板的CPU插槽孔数是:" + cpuHoles); } }
/** * Project Name:DesignPatterns * File Name:MainboardFactory.java * Package Name:com.louis.simplefactory * Date:2017年9月25日下午9:30:01 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved. * */ package com.louis.simplefactory; /** * ClassName:MainboardFactory * Function: TODO ADD FUNCTION. * Reason: TODO ADD REASON. * Date: 2017年9月25日 下午9:30:01 * @author michael * @version * @since JDK 1.7 * @see */ public class MainboardFactory { public static Mainboard createMainboard(int type) { Mainboard mainboard = null; if (type == 1) { mainboard = new MainboardIntel(755); } else if (type == 2) { mainboard = new MainboardAmd(938); } return mainboard; } }
/** * Project Name:DesignPatterns * File Name:ComputerEngineer.java * Package Name:com.louis.simplefactory * Date:2017年9月25日下午9:33:31 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved. * */ package com.louis.simplefactory; /** * ClassName:ComputerEngineer * Function: 组装电脑 * Reason: TODO ADD REASON. * Date: 2017年9月25日 下午9:33:31 * @author michael * @version * @since JDK 1.7 * @see */ public class ComputerEngineer { //定义cpu private Cpu cpu; //定义主板 private Mainboard mainboard; /* * 组装电脑 * */ public void makeComputer(int cpuType,int mainboardType){ //1准备好装机所需要的配置 this.prepareHardwares(cpuType, mainboardType); //2组装机器 //3测试机器 //4支付客户 } public void prepareHardwares(int cpuType,int mainboardType){ //cpu和主板的具体实现 //装机师傅直接找相应的工程获取 this.cpu = CpuFactory.createCpu(cpuType); this.mainboard = MainboardFactory.createMainboard(mainboardType); //测试配件是否好用 this.cpu.calculate(); this.mainboard.installCPU(); } }
/** * Project Name:DesignPatterns * File Name:Client.java * Package Name:com.louis.simplefactory * Date:2017年9月25日下午9:44:08 * Copyright (c) 2017, 2692613726@qq.com All Rights Reserved. * */ package com.louis.simplefactory; /** * ClassName:Client * Function: TODO ADD FUNCTION. * Reason: TODO ADD REASON. * Date: 2017年9月25日 下午9:44:08 * @author michael * @version * @since JDK 1.7 * @see */ public class Client { public static void main(String[] args) { //用户指定用哪个cpu或者mainboard ComputerEngineer computerEngineer = new ComputerEngineer(); computerEngineer.makeComputer(1, 1); } }
原文:http://www.cnblogs.com/Michael2397/p/7594339.html