Java Learning notes-design pattern nine. Builder mode

Source: Internet
Author: User
Tags stub

Builder mode, which separates the construction of a complex object from its representation, so that the same build process can create different representations.

Product class:

1 Package Cn.happy.design_pattern._09builder;2 3 import java.util.ArrayList;4 import java.util.List;5 6  Public classProduct {7 8list<string> parts =NewArraylist<>();9     Ten      Public voidAdd (String part) { One Parts.add (part); A     } -      -      Public voidShow () { theSystem. out. println ("Create product ..."); -          for(String item:parts) { -System. out. println (item); -         } +     } -}

Builder class:

1 Package Cn.happy.design_pattern._09builder;2 3  Public Abstract classBuilder {4 5      Public Abstract voidBuildparta ();6      Public Abstract voidBUILDPARTB ();7      Public AbstractProduct GetResult ();8}

Builder sub-class:

1 Package Cn.happy.design_pattern._09builder;2 3  Public classConcreteBuilder1 extends Builder {4 5     PrivateProduct Product =NewProduct ();6 @Override7      Public voidBuildparta () {8Product.add ("Part A");9 Ten     } One  A @Override -      Public voidBUILDPARTB () { -Product.add ("Part B"); the  -     } -  - @Override +      PublicProduct GetResult () { -         //TODO auto-generated Method Stub +         returnproduct; A     } at  - } -  -  Public classConcreteBuilder2 extends Builder { -  -     PrivateProduct Product =NewProduct (); in @Override -      Public voidBuildparta () { toProduct.add ("Part x"); +  -     } the  * @Override $      Public voidBUILDPARTB () {Panax NotoginsengProduct.add ("Part y"); -  the     } +  A @Override the      PublicProduct GetResult () { +         //TODO auto-generated Method Stub -         returnproduct; $     } $  -}

Director class:

1 Package Cn.happy.design_pattern._09builder; 2 3  Public class Director {45      Public void Construct (Builder builder) {6        Builder. Buildparta (); 7         Builder. BUILDPARTB (); 8     }9 }

Test class:

1 Package Cn.happy.design_pattern._09builder;2 3  Public classMmain {4      Public Static voidMain (string[] args) {5Director Director =NewDirector ();6Builder B1 =NewConcreteBuilder1 ();7Builder B2 =NewConcreteBuilder2 ();8 Director. Construct (B1);9 B1. GetResult (). Show ();Ten Director. Construct (B2); One B2. GetResult (). Show (); A     } -}

Java Learning Note-design pattern nine. 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.