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