Design Pattern---builder mode

Source: Internet
Author: User

 The design pattern is to generalize and merge the same parts, including not only objects (inheritance, polymorphism) but also business logic (process results, etc.), as well as data storage (using different databases).

Make each part independent and decoupled. Make the UI, business logic, data can be independent, update and so on.

For the builder model, the construction process is abstracted through the separation of the construction process from the specific performance. The same or similar construction process is separated to achieve the purpose of reuse.

  

The Director develops the order in which the product is produced, and when the parts are ready, the product can be produced by the entity class ConcreteBuilder.

  

     Public InterfaceBuilder {voidBuildparta (); voidBUILDPARTB (); voidBUILDPARTC ();   Product GetResult (); }    //Concrete Construction Tools     Public classConcreteBuilderImplementsBuilder {Part parta, PartB, PARTC;  Public voidBuildparta () {//here's how to build Parta code specifically};  Public voidBUILDPARTB () {//here's how to build PARTB code specifically};  Public voidBUILDPARTC () {//here's how to build PARTB code specifically};  PublicProduct GetResult () {//return to final assembly of finished product results}; }   //built by     Public classDirector {PrivateBuilder Builder;  PublicDirector (Builder builder) { This. Builder =Builder; } Public voidconstruct () {Builder.buildparta ();      BUILDER.BUILDPARTB ();     BUILDER.BUILDPARTC (); }} Public InterfaceProduct {} Public InterfacePart {} Copy code here's how to call Builder: ConcreteBuilder builder=NewConcreteBuilder (); Director Director=NewDirector (builder);   Director.construct (); Product Product= Builder.getresult ();
director.construct (); Is the part that produces product or product according to a certain process.
So for the same operation process, but the order is different, or a few steps apart, can be achieved by different. If you use the factory method,ConcreteBuilder is transparent to the upper UI.

Using the abstract factory pattern to encapsulate the code:

 Public Interface Ibuilder {    void  Builda ();     void buildb ();     void BUILDC ();        Productor GetResult ();}
 Public classConcreatebuilderImplementsIbuilder {PrivateProductor _mproductor =NewProductor (); @Override Public voidBuilda () {//TODO auto-generated Method StubSystem.out.println ("Small A"); _mproductor.setparta ("Small A"); } @Override Public voidbuildb () {//TODO auto-generated Method StubSystem.out.println ("Middle B"); _MPRODUCTOR.SETPARTB ("Middle B"); } @Override Public voidBuildc () {//TODO auto-generated Method StubSystem.out.println ("Large C"); _MPRODUCTOR.SETPARTC ("Large C"); } @Override PublicProductor GetResult () {//TODO auto-generated Method Stub        return_mproductor; }}
 Public classConcreatebuilderbImplementsIbuilder {PrivateProductor _mproductor =NewProductor (); @Override Public voidBuilda () {//TODO auto-generated Method StubSystem.out.println ("Middle A"); _mproductor.setparta ("Middle A"); } @Override Public voidbuildb () {//TODO auto-generated Method StubSystem.out.println ("Small B"); _MPRODUCTOR.SETPARTB ("Small B"); } @Override Public voidBuildc () {//TODO auto-generated Method StubSystem.out.println ("Small C"); _MPRODUCTOR.SETPARTC ("Small C"); } @Override PublicProductor GetResult () {return_mproductor; }}
 Public class Director {    private  ibuilder builder;      Public Director (Ibuilder mbuilder) {        = mbuilder;    }      Public void construct () {        Builder.builda ();        Builder.buildb ();        BUILDER.BUILDC ();    }}
 Public classProductor {PrivateString Parta; PrivateString PartB; PrivateString PARTC; /**     * @paramParta the Parta to set*/     Public voidSetparta (String parta) { This. Parta =Parta; }    /**     * @paramPartB the PartB to set*/     Public voidSETPARTB (String partB) { This. PartB =PartB; }    /**     * @paramPARTC the PARTC to set*/     Public voidSETPARTC (String partc) { This. PARTC =PARTC; }    /*(non-javadoc) * @see java.lang.object#tostring ()*/@Override PublicString toString () {//TODO auto-generated Method Stub        return(parta+ "\ t" +partb+ "\ T" +PARTC); }        }

Reflection + Abstract Factory mode:

 Public classBuildfactory {Private Static FinalString assemblyname = "Com.jayfulmath.designpattern.builder"; Private Static FinalString Realclass = "Concreatebuilder";  Public StaticIbuilder operator () {Ibuilder result=NULL; String ClassName=assemblyname+ "." +Realclass; Try{Class<?> C =Class.forName (className); Constructor<?> ct =C.getconstructor (); Result=( Ibuilder) (Ct.newinstance ()); } Catch(Nosuchmethodexception | ClassNotFoundException | instantiationexception | illegalaccessexception | IllegalArgumentException |invocationtargetexception e) {            //TODO auto-generated Catch blockE.printstacktrace (); }                returnresult; }}
/*for the build process, you can use the Director to PIN * for implementing class Concreatebuilder and Concreatebuilderb, you can encapsulate it in a way that uses the abstract factory pattern. **/ Public classBuildmainextendsbasicexample {@Override Public voidStartdemo () {//TODO auto-generated Method StubIbuilder Builder =Buildfactory.operator (); Director Mdirector=NewDirector (builder);        Mdirector.construct (); Productor Mproductor=Builder.getresult (); System.out.println ("Productor:" +mproductor.tostring ()); }}

As you can see, the main method only knows Ibuilder and director, by Mdirector.construct (); To construct, you can get productor .

Design Pattern---builder mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.