/** * Created by Aaron on 15/9/14. */ publicclassUser{ publicstaticvoidmain(String[] args)throws InterruptedException{ Computer computer=new Computer(); computer.startup(); System.out.println("--------shutdown-----------"); computer.shutdown(); } }
package design.pattern.facade; /** * Created by Aaron on 15/9/14. */ publicclassComputer{ private CPU cpu; private Memory memory; private GraphicsCard graphicsCard; private Disk disk;
publicComputer(){ this.cpu = new CPU(); this.memory = new Memory(); this.graphicsCard = new GraphicsCard(); this.disk = new Disk(); }
/** * Created by Aaron on 15/9/14. */ publicclassWashingMachine{ publicvoidconnectPort(IWashFaucetAdapter washportadapter){ System.out.print(washportadapter.outToWashingPort()+" success!"); } }
package design.pattern.adapter;
/** * Created by Aaron on 15/9/14. */ publicinterfaceIWashFaucetAdapter{ String outToWashingPort(); }
package design.pattern.adapter;
/** * Created by Aaron on 15/9/14. */ publicclassWashingFaucetAdapterextendsFaucetimplementsIWashFaucetAdapter{
public String outToWashingPort(){ return"transform"+this.port()+" to washing port!"; } }
package design.pattern.adapter;
/** * Created by Aaron on 15/9/14. * 水龙头 */
publicclassFaucet{ public String port(){ System.out.print("facucet port ....."); return"facucet port"; } }
package design.pattern.adapter;
/** * Created by Aaron on 15/9/14. */ publicclassUser{ publicstaticvoidmain(String[] args){ // 创建水龙头、洗衣机、镶接头 WashingMachine washingMachine=new WashingMachine(); WashingFaucetAdapter washingFaucetAdapter= new WashingFaucetAdapter(); // 进行适配 washingMachine.connectPort(washingFaucetAdapter);
} }
输出结果:
facucet port .....transformfacucet port to washing port! success!
/** * Created by Aaron on 15/9/14. */ publicinterfaceIRecruitment{ voidrecruitment(String user); }
package design.pattern.proxy;
/** * Created by Aaron on 15/9/14. */ publicclassFounderWorkimplementsIRecruitment{ publicvoidrecruitment(String user){ System.out.println(this.getClass().getSimpleName()+"招聘员工"+user+"成功!"); } }
package design.pattern.proxy;
/** * Created by Aaron on 15/9/14. */ publicclassWorkProxyimplementsIRecruitment{ private IRecruitment recruitment;
publicWorkProxy(){ this.recruitment = new FounderWork(); }
/** * Created by Aaron on 15/9/14. */ publicclassChinaWashingMachineextendsAbstractWashingMachine{ }
package design.pattern.bridge;
/** * Created by Aaron on 15/9/14. */ publicclassHaierWashingMachineextendsAbstractWashingMachine{ }
package design.pattern.bridge;
/** * Created by Aaron on 15/9/14. */ publicclassUser{ publicstaticvoidmain(String[] args){ new HaierWashingMachine().setPip(new BluePip()); new HaierWashingMachine().setPip(new RedPip()); new ChinaWashingMachine().setPip(new BluePip()); } }
输出:
HaierWashingMachine set blue pip BluePip
HaierWashingMachine set Red RedPip
ChinaWashingMachine set blue pip BluePip
/** * Created by Aaron on 15/9/14. */ publicabstractclassAbstractCompany{ private String name; private Vector<AbstractCompany> companys=new Vector<AbstractCompany>(); public String getName(){ return name; }
publicvoidsetName(String name){ this.name = name; } publicvoiddisplay(int deep){ StringBuilder sb=new StringBuilder(); for(int i=0;i<deep;i++){ sb.append("\t"); } sb.append(this.getName()); System.out.println(sb.toString()); int l = this.getCompanys().size(); if (l > 0) { for (int i = 0; i < l; i++) { this.getCompanys().get(i).display(deep+2); } } } public Vector<AbstractCompany> getCompanys(){ return companys; } publicvoidremoveCompany(AbstractCompany company){ this.companys.remove(company); } publicvoidaddCompany(AbstractCompany company){ this.companys.add(company); } }
package design.pattern.composite;
/** * Created by Aaron on 15/9/14. */ publicclassCompanyextendsAbstractCompany{ publicCompany(String name){ this.setName(name); }
}
package design.pattern.composite;
/** * Created by Aaron on 15/9/14. */ publicclassTechDepartmentextendsAbstractCompany{ publicTechDepartment(){ } publicTechDepartment(String name){ this.setName(name); } }
package design.pattern.composite;
/** * Created by Aaron on 15/9/14. */ publicclassUIDepartmentextendsAbstractCompany{ publicUIDepartment(String name){ this.setName(name); }
publicUIDepartment(){ } }
package design.pattern.composite;
/** * Created by Aaron on 15/9/14. */ publicclassCEO{ publicstaticvoidmain(String[] args){ AbstractCompany company = new Company("总公司"); AbstractCompany abc = new TechDepartment("技术一部"); company.addCompany(abc); abc = new TechDepartment("技术二部"); company.addCompany(abc); abc = new TechDepartment("技术三部"); company.addCompany(abc); abc = new UIDepartment("UI一部"); company.addCompany(abc); abc = new UIDepartment("UI二部"); company.addCompany(abc); abc = new UIDepartment("UI三部"); company.addCompany(abc); AbstractCompany abc1 = new UIDepartment("UI一组"); abc.addCompany(abc1); abc1 = new UIDepartment("UI二组"); abc.addCompany(abc1); abc1 = new UIDepartment("UI三组"); abc.addCompany(abc1); company.display(0); } }