/** * Created by Aaron on 15/9/13. */ publicinterfaceIParser{ String parse(Object obj); }
package design.pattern.factory;
/** * Created by Aaron on 15/9/13. */ publicclassJSONParserimplementsIParser{ @Override public String parse(Object obj){ //create json string return"{class:"+obj.getClass()+"}"; } }
package design.pattern.factory;
/** * Created by Aaron on 15/9/13. */ publicclassXMLParserimplementsIParser{ @Override public String parse(Object obj){ //create xml string.... return"<object><class>"+obj.getClass()+"</class></object>"; } }
package design.pattern.factory;
/** * Created by Aaron on 15/9/13. */ publicclassParserFactory{ publicstaticfinal String TYPE_XML="xml"; publicstaticfinal String TYPE_JSON="json"; publicstatic IParser buildParser(String type){ switch (type){ case ParserFactory.TYPE_XML:returnnew XMLParser(); case ParserFactory.TYPE_JSON:returnnew JSONParser(); } returnnull; }
/** * Created by Aaron on 15/9/13. */ publicclassCRMProjectBuilderimplementsIProjectBuilder{ publicCRMProjectBuilder(){ this.project = new Project(); }
/** * Created by Aaron on 15/9/13. */ publicclassDirector{ private IProjectBuilder builder; publicDirector(IProjectBuilder builder){ this.builder=builder; } public Project process(){ this.builder.makeRequirement(); this.builder.makePrototype(); this.builder.makeScheduler(); this.builder.makeAppFrameWork(); this.builder.makeTables(); this.builder.programming(); this.builder.test(); this.builder.delivery(); return builder.getProject(); }
publicstaticvoidmain(String[] args){ Director director = new Director(new CRMProjectBuilder()); Project project = director.process(); System.out.println(project.getImpleProcess()); } }
/** * 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); } }
/** * Created by Aaron on 15/9/16. */ publicabstractclassAbstractShape{ publicvoidrender(){ this.drawBackground(); this.drawGraphics(); this.drawSignature(); } abstractvoiddrawSignature(); abstractvoiddrawBackground(); abstractvoiddrawGraphics();
}
package design.pattern.temlatemothod;
/** * Created by Aaron on 15/9/16. */ publicclassCircleShapeextendsAbstractShape{ @Override publicvoiddrawSignature(){ System.out.println("draw circle signature!"); }
@Override publicvoiddrawBackground(){ System.out.println("draw circle background! "); }
@Override publicvoiddrawGraphics(){ System.out.println("draw circle graphics!"); } }
package design.pattern.temlatemothod;
/** * Created by Aaron on 15/9/16. */ publicclassRectShapeextendsAbstractShape{ @Override publicvoiddrawSignature(){ System.out.println("draw rect signature!"); }
@Override publicvoiddrawBackground(){ System.out.println("draw rect background! "); }
@Override publicvoiddrawGraphics(){ System.out.println("draw rect graphics!"); } }
package design.pattern.temlatemothod;
/** * Created by Aaron on 15/9/16. */ publicclassUser{ publicstaticvoidmain(String args[]){ new CircleShape().render(); System.out.println("-----"); new RectShape().render(); } }
输出结果:
draw circle background!
draw circle graphics!
draw circle signature!
-----
draw circle background!
draw circle graphics!
draw circle signature!
/** * Created by Aaron on 15/9/16. */ publicabstractclassSubject{ private ArrayList<Observer> observers=new ArrayList<Observer>(); publicvoidaddObserver(Observer observer){ this.observers.add(observer); } publicvoidremoveObserver(Observer observer){ this.observers.remove(observer); } publicvoidnotifyObserver(){ for(Observer observer:observers){ observer.update(this); } } }
package design.pattern.observer;
import java.awt.*;
/** * Created by Aaron on 15/9/16. */ publicclassGPSSubjectextendsSubject{ private Point point; publicvoidmove(Point point){ this.point=point; this.notifyObserver(); } }
package design.pattern.observer;
/** * Created by Aaron on 15/9/16. */ publicabstractclassObserver{ publicObserver(){
} publicabstractvoidupdate(Subject subject);
}
package design.pattern.observer;
/** * Created by Aaron on 15/9/16. */ publicclassMapObserverextendsObserver{ @Override publicvoidupdate(Subject subject){ System.out.println(this.getClass().getSimpleName()+"_"+this.hashCode()+" observer:"+subject.getClass().getSimpleName()+" position changed;"); } }
package design.pattern.observer;
import java.awt.*;
/** * Created by Aaron on 15/9/16. */ publicclassUser{ publicstaticvoidmain(String[] args){ GPSSubject subject=new GPSSubject(); subject.addObserver(new MapObserver()); Observer observer1=null; subject.addObserver(observer1=new MapObserver()); subject.move(new Point(200, 400)); System.out.println("remove one observer from subject's observer list!"); subject.removeObserver(observer1); subject.move(new Point(200,400)); } }
状态模式(State);
GoF这样定义: 允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它 所属的类。
以下是示例代码:
package design.pattern.state;
/** * Created by Aaron on 15/9/20. */ publicclassContextextendsAbstractLifeState{ publicstatic OpeningState openingState = new OpeningState(); publicstatic ClosingState closingState = new ClosingState(); publicstatic RunningState runningState = new RunningState(); publicstatic StoppingState stoppingState = new StoppingState(); private AbstractLifeState lifeState;
publicContext(){ }
public AbstractLifeState getLifeState(){ return lifeState; }
/** * Created by Aaron on 15/9/29. */ publicclassLoggerHandlerextendsHandler{ @Override publicvoidprocess(){ if(getSuccessor()!=null){ System.out.println(getClass().getSimpleName()+",处理请求,并调用下一个处理者"); getSuccessor().process(); }else{ System.out.println(getClass().getSimpleName()+",仅处理,无下一处理者"); } } }
package design.pattern.responsibilitychain;
/** * Created by Aaron on 15/9/29. */ publicclassValidateHandlerextendsHandler{ @Override publicvoidprocess(){ if(getSuccessor()!=null){ System.out.println(getClass().getSimpleName()+",处理请求,并调用下一个处理者"); getSuccessor().process(); }else{ System.out.println(getClass().getSimpleName()+",仅处理,无下一处理者"); } } }
package design.pattern.responsibilitychain;
/** * Created by Aaron on 15/9/29. */ publicclassUser{ publicstaticvoidmain(String[] args){ Handler validate = new ValidateHandler(); Handler handler = new LoggerHandler(); validate.setSuccessor(handler); validate.process(); } }
/** * Created by Aaron on 15/9/29. */ publicclassUser{ publicstaticvoidmain(String[] args){ AudioPlay audioPlay = new AudioPlay(); PlayCommand playCommand = new PlayCommand(audioPlay); RewindCommand rewindCommand = new RewindCommand(audioPlay); StopCommand stopCommand = new StopCommand(audioPlay);