/** * 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()); } }