[Original-tutorial-serialization] "android big talk design mode"-design mode Creation Mode Chapter 5: builder Mode

Source: Internet
Author: User
<Big talk design model> description and copyright notice of this tutorial

L this document references and uses free images and content on the Internet, and is released in a free and open manner, hoping to contribute to the mobile Internet and the smart phone age! This document can be reproduced at will, but cannot be used for profit.

L if you have any questions or suggestions about this document, go to the official blog

Http://www.cnblogs.com/guoshiandroid/ (with the following contact information), we will carefully refer to your suggestions and modify this document as needed to benefit more developers!

L The latest and complete content of "big talk design mode" will be regularly updated on the official blog of guoshi studio. Please visit the blog of guoshi studio.

Http://www.cnblogs.com/guoshiandroid/get more updates.

Guoshi studio is a technical team dedicated to enterprise-level application development on the Android platform. It is committed to the best Android Application in China.ProgramDevelopment institutions provide the best Android enterprise application development training services.

Official contact information for Enterprise Training and Development Cooperation:

Tel: 18610086859

Email: hiheartfirst@gmail.com

QQ: 1740415547

Guoshi studio is better for you!

Builder Mode Let's live together! 

Builder ModeUse Cases: 

Gg and mm, who are deeply in love, cannot leave each other for a moment.

"Let's live together !" Gg said to mm, "Well" mm nodded shyly.

God, what's wrong with this world? Sure enough, boys in love are crazy, girls in love are fools! This madman is with a fool and I really don't know what to do. ^_^

Since we can't wait for drug cohabiting, we need to solve the house problem first.

Gg went around the school for a few days and asked a lot of people. Finally, he confirmed that he was about to leave the school. 500 meters The two-bedroom and one-bedroom room is located in a very good location, very warm and beautiful environment.

Gg thought that since we live together, we should not be too boring. We thought we would like to renovate the house, like setting up some furniture and buying kitchen utensils. Of course, this silly Gg won't do these things. Unfortunately, GG, immersed in love all day, is not familiar with the environment around the school. However, GG only asks the landlord for help. The landlord said this was a small problem, but the cost was a little high. Let's plan it first, and then ask the decoration master to directly complete the renovation. After nearly half a day of discussion, the solution was finalized. My brother reluctantly prepaid the decoration fee of two thousand yuan to the landlord. The next step is to wait for the decoration news. Then we moved in with MM.

Builder mode explanation: 

Builder pattern, also known as builder pattern, is one of the 23 design patterns proposed by gof.

The builder mode is one of the object creation modes. It is used to hide the process of creating a composite object. It abstracts the process of creating a composite object and uses subclass inheritance and overloading, dynamically create objects with composite attributes.

Separate the construction of a complex object from its
Representation so that the same construction process can create different
Representations.

Builder ModeUMLFigure: 

The builder mode involves the following roles:

Abstract builder role: provides an abstract interface to regulate the construction of each component of a product object. This interface generally specifies at least two methods. One is to create partial methods, such as builderpart, and the other is to return results, such as getproduct, to constrain specific builder implementations.
Concretebuilder roles: these roles are classes closely related to applications. They create product instances under application calls. This role product implements the abstract builder interface, which mainly completes step-by-step product creation and provides product object instances.
Director role: as the name suggests, it is the caller of the creation work who directs the use of specific creators to create the product. However, the director role does not have specific knowledge about the product category. What really possesses specific knowledge about the product category is the specific builder role.
Product role: a product role is a complex object during construction. Generally, the builder mode is used only for the creation of complex objects. There will be more than one product class in a system. These product classes do not necessarily have a common interface and may be completely unrelated, in this case, you need to provide multiple sets of abstraction and specific builders to create inconsistent products or use a unified interface to identify products.

The UML diagram of the builder mode is as follows:

In-depth analysis of builder Mode:

In a software system, sometimes it is faced with the creation of "a complex object", which is usually caused by the sub-objects of each partAlgorithmComponents; due to changes in requirements, each part of this complex object is often subject to drastic changes, but the algorithms that combine them are relatively stable. How to deal with this change? How can we provide a "encapsulation mechanism" to isolate the changes of "various parts of complex objects", so as to keep the "stable Construction Algorithm" in the system from changing with demand? This is the builder mode.

The builder mode separates the construction of complex objects from the presentation of objects, so that the same building process can create different representations.

Application Scenario Analysis andCodeImplementation: 

In the above application scenario, GG and mm, who are deeply in love, plan to live together. First, they must renovate the house. For house decoration, the landlord is equivalent to the Director (director). The landlord decides to use the decoration company, and the decoration company generally follows the unified decoration standard, the unified decoration standard here is equivalent to the abstract builder role. The specific decoration company the landlord is the specific builder role, and the house is equivalent to the Product role.

The UML Model diagram is as follows:

Create a house class:

PackageCom. diermeng. designpatter. Builder. impl;

/*

* Gg and Mm love nest

*/

Public ClassLovenest {

// Floor

PrivateString floor;

// Wall

PrivateString wall;

// Roof

PrivateString housetop;

 

PublicString getfloor (){

ReturnFloor;

}

 

Public VoidSetfloor (string floor ){

This. Floor = floor;

}

 

PublicString getwall (){

ReturnWall;

}

 

Public VoidSetwall (string wall ){

This. Wall = wall;

}

 

PublicString gethousetop (){

ReturnHousetop;

}

 

Public VoidSethousetop (string housetop)
{

This. Housetop = housetop;

}

 

}

 

Create an abstract interface for a decoration company:

PackageCom. diermeng. designpatter. Builder;

ImportCom. diermeng. designpatter. Builder. impl. lovenest;

 

/*

* Decoration company's abstract interface

*/

Public InterfaceHousebuilder {

// Floor repair

Public VoidMakefloor ();

// Wall repair

Public VoidMakewall ();

// Roof repair

Public VoidMakeroof ();

// House Return Method

PublicLovenest gethouse ();

}

Establish a specific decoration company:

PackageCom. diermeng. designpatter. Builder. impl;

ImportCom. diermeng. designpatter. Builder. housebuilder;

 

/*

* Companies that build love nest for Gg and mm

*/

Public ClassLovenestbuilderImplementsHousebuilder {

Lovenest =NewLovenest ();

 

PublicLovenest gethouse (){

ReturnLovenest;

}

 

Public VoidMakefloor (){

Lovenest. setfloor ("love nest's floor has been decorated ^_^ ");

}

 

Public VoidMakeroof (){

Lovenest. sethousetop ("the roof of love nest has been decorated ^_^ ");

}

 

Public VoidMakewall (){

Lovenest. setwall ("love nest wall has been decorated ^_^ ");

}

 

}

Build a decoration company's ctor class:

PackageCom. diermeng. designpatter. Builder. impl;

ImportCom. diermeng. designpatter. Builder. housebuilder;

 

/*

* The Director role of house decoration determines the final decoration company in the Method

*/

Public ClassHousedirector {

 

Public VoidMakehouse (housebuilder builder)
{

Builder. makefloor ();

Builder. makewall ();

Builder. makeroof ();

}

 

}

Finally, we create a test client:

PackageCom. diermeng. designpatter. Builder. client;

 

ImportCom. diermeng. designpatter. Builder. housebuilder;

ImportCom. diermeng. designpatter. Builder. impl. lovenest;

ImportCom. diermeng. designpatter. Builder. impl. housedirector;

Import
Com. diermeng. designpatter. Builder. impl. lovenestbuilder;

 

/*

* Test the client

*/

Public ClassBuildertest
{

 

Public Static VoidMain (string [] ARGs ){

 

// Decoration by a specific Decoration Company

Housebuilder builder =New
Lovenestbuilder ();

// Create a designer

Housedirector ctor =NewHousedirector ();

// Deliver the specific decoration company to the designer

Director. makehouse (builder );

 

// Return to house

Lovenest = builder. gethouse ();

 

// Display the decoration effect

System.Out. Println (lovenest. getfloor ());

System.Out. Println (lovenest. getwall ());

System.Out. Println (lovenest. gethousetop ());

}

 

}

The output result is as follows:

Aichao's floor has been decorated ^_^

Inest's wall has been decorated ^_^

Inest's roof has been decorated ^_^

 

Advantages and disadvantages of builder mode: 

Advantages:

In the builder mode, the client does not directly take charge of object creation. Instead, it submits the task to the specific creator class and gives the responsibility for how to assemble the task to the director class, the client is responsible for the call of objects, in line with a single responsibility principle. In addition, different creators can be selected, so objects can be displayed in different forms.

Disadvantages:

The Creator mode is consistent with the creation of objects with little difference in the product. If the difference is great, there will be a lot of specific creators. At this time, it is best to combine the factory method mode.

Introduction to the practical application of builder mode:

Object creation: the builder mode is designed for object creation.

Create a composite object: The created object is a composite object

Follow the creation process of each part of the object creation. Different factories have different methods to create product attributes.

Tip:

The builder mode is created to solve the problem of compound object creation. The Builder mode separates the construction of complex objects from the presentation of objects, so that the same building process can create different manifestations. It is advantageous to clarify the responsibilities and objectives of each part, and to optimize the software structure.

Gg and mm cohabiting love nest need to be decorated, which saves the trouble, but it costs money. After all, everything has two sides.



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.