Design pattern (iii) builder mode

Source: Internet
Author: User

Core Essence:

Separate construction of the object subassembly, which is the responsibility of the builder, is separated and assembled (by the director). So that complex objects can be constructed.

Usability:

When the build process of an object is complex

Characteristics:

Because of the realization of the decoupling of the construction and assembly. Different builders, the same assembly, can also make different objects, the same builder, different assembly order can also make different objects. That is to realize the construction algorithm, the decoupling of the Assembly algorithm, to achieve a better reuse

Application Scenarios:

Implementing the example code:

 PackageCom.lp.builder; Public classAirShip {PrivateOrbitalmodule Orbitalmodule;//Orbital Module    PrivateEngine engine;//engine    PrivateEscapetower Escapetower;//Escape pod     Publicorbitalmodule Getorbitalmodule () {returnOrbitalmodule; }     Public voidsetorbitalmodule (Orbitalmodule orbitalmodule) { This. Orbitalmodule =Orbitalmodule; }     PublicEngine Getengine () {returnengine; }     Public voidSetengine (Engine engine) { This. engine =engine; }     Publicescapetower Getescapetower () {returnEscapetower; }     Public voidsetescapetower (Escapetower escapetower) { This. Escapetower =Escapetower; }        }classorbitalmodule{PrivateString name; Orbitalmodule (String name) { This. Name =name; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }    }classengine{PrivateString name; Engine (String name) { This. Name =name; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}classescapetower{PrivateString name; Escapetower (String name) { This. Name =name; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}
spacecraft and the build that can be assembled
 Package Com.lp.builder;  Public Interface Airshipbuilder {    Engine builderengine ();    Orbitalmodule builderorbitamodule ();    Escapetower builderescapertower ();}
Spaceship Construction Interface
 Package Com.lp.builder; // assemble the ship .  Public Interface airshipdirector {        AirShip directairship ();}
spacecraft Assembly Interface

 PackageCom.lp.builder; Public classLpairshipbuilderImplementsairshipbuilder{@Override PublicEngine Builderengine () {System.out.println ("Building Li Peng Brand engine"); return NewEngine ("Li Peng Brand Engines"); } @Override Publicorbitalmodule Builderorbitamodule () {System.out.println ("Building Li Peng's orbital module"); return NewOrbitalmodule ("Li Peng Rail module"); } @Override Publicescapetower Builderescapertower () {System.out.println ("Building Li Peng's escape cabin"); return NewEscapetower ("Li Peng Escape Cabin"); }    //Stringbuildr}
Li Peng's Spaceship construction interface

 PackageCom.lp.builder; Public classLpairshipdirectorImplementsAirshipdirector {PrivateAirshipbuilder Builder; Lpairshipdirector (Airshipbuilder builder) { This. Builder =Builder; } @Override PublicAirShip directairship () {Engine e=Builder.builderengine (); Orbitalmodule o=Builder.builderorbitamodule (); Escapetower es=Builder.builderescapertower (); AirShip Ship=NewAirShip ();        Ship.setengine (e);        Ship.setescapetower (es);        Ship.setorbitalmodule (o); returnShip ; }}
Li Peng's spacecraft assembly interface

Design pattern (iii) 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.