The path to design pattern learning-builder Pattern

Source: Internet
Author: User
Document directory
  • Motivation:
  • Intent:
  • Structure:
  • Collaboration:
  • Code Implementation
  • Key points of the builder mode:

The generator mode is somewhat similar to the abstract factory mode. Both return classes composed of many methods and objects. The main difference between them is that the abstract factory returns a series of related classes, and the generator is to build a complex object step by step based on the data provided to it.

Motivation:

In a software system, sometimes it is faced with the creation of "a complex object", which is usually composed of sub-objects of each part using a certain algorithm; due to changes in requirements, the various parts of this complex object often face 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?

Intent:

Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.

-- Design Pattern gof

Structure:

Collaboration:

 

Code Implementation
Using system; using system. collections. generic; using system. text; namespace builder {class program {static void main (string [] ARGs) {console. writeline ("true: Chinese style houses, false: Western style houses"); string input = console. readline (); try {housebuilder builder = clienmanger. builder (bool. parse (input); house chinahouse = clienmanger. createhouse (builder); chinahouse. show ();} catch {console. writeline ("input error! ");} Console. readkey () ;}} public class house {private list <string> door = new list <string> (); public list <string> door {get {return door ;} set {DOOR = value ;}} private list <string> Windows = new list <string> (); public list <string> Windows {get {return windows ;} set {windows = value ;}} private string wall; Public String wall {get {return wall ;}set {wall = value ;}} private string floor; public String floor {get {return floor;} set {floor = value ;}} public void show () {console. writeline (door. count. tostring () + "+ door [0]. tostring (); console. writeline (Wall); console. writeline (windows. count. tostring () + "count" + windows [0]. tostring (); console. writeline (floor) ;}} public abstract class housebuilder {public abstract void builddoor (); public abstract void buildwall (); public abstract void buildwindows (); public abstract void buildfloor (); public abstract house gethouse ();} public class clienmanger {public static house createhouse (housebuilder builder) {builder. builddoor (); builder. buildfloor (); builder. buildwall (); builder. buildwindows (); Return builder. gethouse () ;}public static housebuilder Builder (bool BL) {If (BL) {return New chinahousebuilder () ;}else {return New eurohousebuilder ();}}} public class chinahousebuilder: housebuilder {private house chinahouse = new house (); Public override void builddoor () {chinahouse. door. add ("door (Chinese Style)"); chinahouse. door. add ("door (Chinese Style)");} public override void buildwall () {chinahouse. wall = "(Chinese Style)";} public override void buildwindows () {chinahouse. windows. add ("window (Chinese Style)"); chinahouse. windows. add ("window (Chinese Style)");} public override void buildfloor () {chinahouse. floor = "floor (Chinese Style)";} public override house gethouse () {return chinahouse;} public class eurohousebuilder: housebuilder {private house eurohouse = new house (); public override void builddoor () {eurohouse. door. add ("door (European style)");} public override void buildwall () {eurohouse. wall =" (European style)";} public override void buildwindows () {eurohouse. windows. add ("window (European style)");} public override void buildfloor () {eurohouse. floor = "floor (European style)";} public override house gethouse () {return eurohouse ;}}}

 

Key points of the builder mode:

1. The Builder mode is mainly used to "build a complex object by step ". In this process, "step-by-step" is a stable algorithm, while all parts of complex objects change frequently.

2. Where is the change point, and where is the encapsulation? The Builder mode is mainly used to cope with frequent changes in the "various parts of complex objects. Its disadvantage is that it is difficult to cope with changes in the demand for "step-by-step algorithm building.

3. The Abstract Factory mode solves the demand changes of "series objects" and the builder mode solves the demand changes of "Object parts. The builder mode is usually used in combination with the composite 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.