Java Implementation Builder (builder) mode

Source: Internet
Author: User

Builder mode is the creation mode, creating a product that hides the creation process, using a combination of methods, by the conductor (director) to determine the construction process

public interface Builder {void Buildparta (); void Buildpartb (); void Buildpartc ();}

public class Builderimpl implements Builder {@Overridepublic void Buildparta () {System.out.println ("Building Part A");} @Overridepublic void Buildpartb () {System.out.println ("Building part B"); @Overridepublic void Buildpartc () {System.out.println ("Building part C");}}

public class BuilderImpl2 implements Builder {@Overridepublic void Buildparta () {System.out.println ("Building part AA");} @Overridepublic void Buildpartb () {System.out.println ("Building part BB"); @Overridepublic void Buildpartc () {System.out.println ("Building part CC");}}

/** * Conductor: Guide How to build * Combination builder * @author Stone * */public class Director {Private builder Builder;public director (builder Builder) {This.builder = builder;} /** * Construction Method: Defines the process of construction * if additional processes are required, then new Commander Director can */public void construct () {System.out.println ("director conductor Builder for construction "); Builder.buildparta (); BUILDER.BUILDPARTB (); BUILDER.BUILDPARTC ();}

/** * Conductor: Guide How to build * Combination builder * @author Stone * */public class Director2 {private Builder builder;public Director2 (Builde R builder) {This.builder = builder;} /** * Construction Method: Defines the process of construction * if additional processes are required, then new Commander Director can */public void construct () {System.out.println ("Director2 command Builder for construction "); BUILDER.BUILDPARTB (); BUILDER.BUILDPARTC (); Builder.buildparta ();}

  /* Builder mode: The builder mode is to centralize various products to manage, to create composite objects, * *  to separate the construction of a complex object from its representation, so that the same build process can create different representations, a design pattern known as the builder pattern */public Class Test {public static void main (string[] args) {Builder builder = new Builderimpl ();D Irector director = new Director (b Uilder);/* * Same build process, different modules (Buildparta, BUILDPARTB, BUILDPARTC) implementation, new one builder implementation * Different build process, new one director * different build process, Different module implementations, new Director,new builder */director.construct (); System.out.println (""); Builder builder2 = new BuilderImpl2 ();D irector Director2 = new Director (builder2);d irector2.construct (); System.out.println (""); Builder Builder3 = new BuilderImpl2 ();D irector2 Director3 = new Director2 (builder3);d irector3.construct ();}}

Print:

Director directing builder to build parts a build part B build Parts cdirector command builder build building parts AA Construction Parts bb Construction Parts CCDIRECTOR2 command Builder to build construction parts BB construction Parts cc construction Parts AA


Java Implementation Builder (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.