13th Builder Model

Source: Internet
Author: User

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

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.