The builder pattern for Java design patterns

Source: Internet
Author: User

Builder mode: The construction of a complex object is separated from its representation, allowing the same build process to create different representations.

Simple example:

1  Public classProduct2 {3     PrivateString Parta;4     PrivateString PartB;5     6      Public voidSetparta (String parta)7     {8          This. Parta =Parta;9     }Ten  One      Public voidSETPARTB (String PartB) A     { -          This. PartB =PartB; -     } the      - @Override -      PublicString toString () -     { +         return"[Parta =" + Parta + ", PartB =" + PartB + "]"; -     } + } A  at  Public InterfaceFactory - { -      Public voidinit (); -      Public voidBuildparta (); -      Public voidBUILDPARTB (); -      PublicProduct getproduct (); in } -  to  Public classFactory1a2bImplementsFactory + { -     Privateproduct product; the      * @Override $      Public voidInit ()Panax Notoginseng     { -Product =NewProduct (); the     } +      A @Override the      Public voidBuildparta () +     { -Product.setparta ("1"); $     } $  - @Override -      Public voidBUILDPARTB () the     { -PRODUCT.SETPARTB ("2");Wuyi     } the  - @Override Wu      PublicProduct getproduct () -     { About         returnproduct; $     } - } -  -  Public classDirector A { +     PrivateFactory Factory =Newfactory1a2b (); the      -      PublicProduct newproduct () $     { the factory.init (); the Factory.buildparta (); the FACTORY.BUILDPARTB (); the         returnfactory.getproduct (); -     } in } the  the  Public classTest About { the      Public Static voidMain (string[] args) the     { theDirector Director =NewDirector (); + System.out.println (Director.newproduct ()); -     } the}

The builder pattern for Java design patterns

Related Article

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.