Design Mode [builder mode]

Source: Internet
Author: User
Tags building windows
Overview:


Separates a complex build from its representation so that different representations can be created during the same build process.
[Expansion] is different from Abstract Factory: In the builder mode, there is a mentor who manages the builder. the user is in contact with the mentor. The mentor contacts the builder and finally gets the product. That is, the construction mode can enforce a step-by-step construction process.
[For more information, see http://blog.csdn.net/mahoking]

 

Applicability:


1. When creating complex objects, algorithms should be independent of the components of the objects and their assembly methods;
2. When the building process must allow different representations of the constructed object.

 

Participants:


1. Builder
Specify an abstract interface for each part of a product object.
2. concretebuilder [concrete specific, tangible, real, and actual ;]
Implement the builder interface to build and assemble each part of the product;
Define and specify the representation it creates;
Provides an interface for retrieving (retrieving) products.
3. Director
Construct an object using the builder Interface
4. Product
Indicates a complex object to be constructed. Concretbuilder creates an internal representation of the product and defines its assembly process, including the class that defines the components, including the interfaces that assemble these components into the final product.

 

Example:


There is a person named hoking who needs to build his own house (product) on his homestead. hoking will not build a house or design his own house. So he needs to build a house builder because the builder will build a wall, but will not design the house, and he also needs to find a designer because they will design the house. Of course, there must be an agreement between them that the workers fully listen to the leaders of designers. In this way, under the command of design, the workers started building the house. In this process, the designer only participates in the design and command, so the hoking needs to ask the workers for a house.

 

Case Study
/*** Abstract interface object builder * @ author mahc **/public interface builder {public void makewindows (); Public void makefloors (); public room finishroom ();} /*** designer * @ author mahc **/public class director {public void Order (Builder) {system. out. println ("under the command of the designer, the worker starts building the house! "); Builder. makefloors (); builder. makewindows (); system. Out. println (" under the supervision of the designer, the worker completes the construction of the house! ") ;}}/*** Builder * @ author mahc **/public class hardhat implements builder {private string windows =" "; private string floors = ""; @ overridepublic void makewindows () {system. out. println ("construction workers start building windows! "); Windows =" finished ";}@ overridepublic void makefloors () {system. Out. println (" the construction worker starts laying the floor! "); Floors =" finished ";}@ overridepublic room finishroom () {return new room (" hoking ");}} /*** representative room * @ author mahc **/public class room {private string householder; // public room (string householder) {super (); this. householder = householder;} public void saywelcome () {system. out. println ("welcomen" + householder + "to yours' new room! ");}}

The following is the test code section.

/*** Builder pattern test case * @ author mahc **/public class testbuilderpattern {public static void main (string [] ARGs) {// create a designer Director ctor = new director (); // create a builder hardhat = new hardhat (); // The designer downloads the construction command director to the builder. order (hardhat); // construction workers deliver house room = hardhat to hoking. finishroom (); room. saywelcome ();}}

 

[For more information, see http://blog.csdn.net/mahoking]


 

Design Mode [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.