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

Source: Internet
Author: User
Source: http://www.blogjava.net/flustar/archive/2007/12/03/builder.html
Builder mode definition: separates the construction of a complex object from its representation, so that different representations can be created during the same construction process. When I first came into contact with this model, I really couldn't understand what it meant and what's the use of it. As a result, the Internet Googled and finally got the universally accepted explanation: the construction mode is to create a complex object step by step, it allows users to build complex objects by specifying the types and content of complex objects. users do not know the specific internal building details. The following example shows how to use this mode, Code Import Java. util. arraylist; interface builder {public void buildparta (); Public void buildpartb (); Public void buildpartc (); Public Product getproduct ();} class product {private arraylist <string> parts = new arraylist <string> (); Public void add (string part) {parts. add (part);} public void show () {system. out. println ("product consists of the following parts:"); For (string S: parts) {system. out. println (s) ;}} class worker imp Lements builder {private product; Public void buildparta () {Product = new product (); product. add ("Part A");} public void buildpartb () {product. add ("B Part");} public void buildpartc () {product. add ("C part");} public product getproduct () {return product;} class designer {public void Order (Builder) {builder. buildparta (); builder. buildpartb (); builder. buildpartc () ;}} public class test {P Ublic static void main (string [] ARGs) {designer = new designer (); Builder = new worker (); designer. order (builder); product Product = builder. getproduct (); product. show () ;}} the output result is as follows: the product consists of the following parts: part A, Part B, and part C. From this example, we can see the builder mode, the process of building objects is divided into parts. Summary: The Builder mode mainly aims to decouple the process of building complex objects from its components. So that we don't have to worry about how each part is assembled.

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.