The builder pattern is when the algorithm that creates the complex object should be independent of the part of the object and the mode in which they are assembled.
Importjava.util.LinkedList;Importjava.util.List;/*** Created by Hero on 16-4-3.*/ Public classProduct {Privatelist<string> parts =NewLinkedlist<>(); Public voidAdd (String part) {Parts.add (part); } Public voidShow () { for(String p:parts) {System.out.print (P+ "\ T"); } System.out.println (); }}/*** Created by Hero on 16-4-3.*/ Public Abstract classBuilder { Public Abstract voidBuildparta (); Public Abstract voidBUILDPARTB (); Public AbstractProduct GetResult ();}/*** Created by Hero on 16-4-3.*/ Public classConcreteBuilder1extendsBuilder {PrivateProduct Product =NewProduct (); @Override Public voidBuildparta () {Product.add (A); } @Override Public voidBUILDPARTB () {Product.add ("B"); } @Override PublicProduct GetResult () {returnproduct; }}/*** Created by Hero on 16-4-3.*/ Public classConcreteBuilder2extendsBuilder {PrivateProduct Product =NewProduct (); @Override Public voidBuildparta () {Product.add ("X"); } @Override Public voidBUILDPARTB () {Product.add ("Y"); } @Override PublicProduct GetResult () {returnproduct; }}/*** Created by Hero on 16-4-3.*/ Public classDirector { Public voidConstruct (Builder builder) {Builder.buildparta (); BUILDER.BUILDPARTB (); }} Public classMain { Public Static voidMain (string[] args) {Director Director=NewDirector (); ConcreteBuilder1 ConcreteBuilder1=NewConcreteBuilder1 (); ConcreteBuilder2 ConcreteBuilder2=NewConcreteBuilder2 (); Product Product=NULL; Director. Construct (ConcreteBuilder1); Product=Concretebuilder1.getresult (); Product.show (); Director. Construct (CONCRETEBUILDER2); Product=Concretebuilder2.getresult (); Product.show (); }}
13th Builder Model