[Design mode] Good food tastes different-builder Mode

Source: Internet
Author: User

I. Overview

The builder mode is defined to separate the construction of a complex object from its representation, so that different representations can be created during the same construction process. First, it aims to build a complex object, which usually requires severalSub-ObjectOr step by step to complete the final construction of this object, and the sub-object of this complex object often needs constant changes, but its construction process is relatively stable.

Essence: constructs the same object through a unified process or constraint.


2. Example

1) construct a villain with feet, hands, body, and head

Disadvantages: A villain constructed in this way may easily forget to construct a hand or foot.


Void button#click (Object sender, eventargs e) {pen P = new pen (color. yellow); graphics gthin = picturebox1.creategraphics (); gthin. drawellipse (p, 50, 20, 30, 30); // header gthin. drawrectangle (p, 60, 50, 10, 50); // body gthin. drawline (p, 60, 50, 40,100); // left-hand gthin. drawline (p, 70, 50, 90,100); // right hand gthin. drawline (p, 60,100, 45,150); // left gthin. drawline (p, 70,100, 85,150); // The right foot graphics gfat = picturebox2.creategraphics (); gfat. drawellipse (p, 50, 20, 30, 30); gfat. drawellipse (p, 45, 50, 40, 50); // fat gfat. drawlines (p, 50, 50, 30,100); gfat. drawline (p, 80, 50,100,100); gfat. drawline (p, 60,100, 45,150); gfat. drawline (p, 70,100, 85,150 );}


2) construct villain 2

Improvement: in order to avoid errors that may occur every time you build a villain, you can write the method of building the villain into a class and call the constructor every time you construct the method.

Disadvantage: What should I do if I forget to draw my legs or arm when constructing a tall class?

Solution: Build a basic class for a person and write all the methods to be implemented as pure virtual functions. This avoids the lack of a part.

Private class personthinbuilder // thin class {public void build () {G. drawellipse (p, 50, 20, 30, 30); G. drawrectangle (p, 60, 50, 10, 50); G. drawline (p, 60, 50, 40,100); G. drawline (p, 70, 50, 90,100); G. drawline (p, 60,100, 45,150); G. drawline (p, 70,100, 85,150) ;}} class personfatbuilder // fat class {public void build () {G. drawellipse (p, 50, 20, 30, 30); G. drawellipse (p, 45, 50, 40, 50); G. drawline (p, 50, 50, 30,100); G. drawline (p, 80, 50,100,100); G. drawline (p, 60,100, 45,150); G. drawline (p, 70,100, 85,150 );}}

3) builder Mode

Note: if some people need to draw more details, they only need to add the corresponding function to a specific person. Only when each class needs to be created must it be written to the abstract class.


Namespace builder mode {abstract class personbuilder // abstract class {protected graphics g; protected pen P; Public personbuilder (Graphics g, pen p) {This. G = g; this. P = P;} // if it is C ++, it must be written as a pure virtual function in the form of public abstract void buildhead (); public abstract void buildbody (); public abstract void buildarmleft (); public abstract void buildarmright (); public abstract void buildlegleft (); public abstract void buildlegright ();} class personthinbuilder: personbuilder // integrated abstract class, every function in the abstract class {public personthinbuilder (Graphics g, pen P): Base (G, p) {} public override void buildhead () {G. drawellipse (p, 50, 20, 30, 30);} public override void buildbody () {G. drawrectangle (p, 60, 50, 10, 50);} public override void buildarmleft () {G. drawline (p, 60, 50, 40,100);} public override void buildarmright () {G. drawline (p, 70, 50, 90,100);} public override void buildlegleft () {G. drawline (p, 60,100, 45,150);} public override void buildlegright () {G. drawline (p, 70,100, 85,150) ;}} class personfatbuilder: personbuilder // others are similar to {public personfatbuilder (Graphics g, pen P): Base (g, P) {} public override void buildhead () {G. drawellipse (p, 50, 20, 30, 30);} public override void buildbody () {G. drawellipse (p, 45, 50, 40, 50);} public override void buildarmleft () {G. drawline (p, 50, 50, 30,100);} public override void buildarmright () {G. drawline (p, 80, 50,100,100);} public override void buildlegleft () {G. drawline (p, 60,100, 45,150);} public override void buildlegright () {G. drawline (p, 70,100, 85,150) ;}} class persondirector // command construction class (isolating user and construction process associations) {private personbuilder Pb; Public persondirepb (personbuilder Pb) // pass on the people to be built (tall, fat, and thin) {This. PB = Pb;} public void createperson () {Pb. buildhead (); Pb. buildbody (); Pb. buildarmleft (); Pb. buildarmright (); Pb. buildlegleft (); Pb. buildlegright () ;}} private void button#click (Object sender, eventargs e) {pen P = new pen (color. yellow); // paint brush color personthinbuilder PTB = new personthinbuilder (picturebox1.creategraphics (), P); // create a fat object persondirepdpdthin = new persondirept( PTB); // create a builder object, pass the object pdthin. createperson (); // construct the fat man personfatbuilder pfb = new personfatbuilder (picturebox2.creategraphics (), P); persondirepdfpdfpdfat = new persondirector (pfb); done. createperson ();}


Iii. Analysis of builder Mode

Using system; using system. collections. generic; using system. text; namespace builder mode {class product {ilist <string> parts = new list <string> (); Public void add (string part) // Add product parts {parts. add (part);} public void show () // list product parts {console. writeline ("\ n product creation ----"); foreach (string part in parts) {console. writeline (Part) ;}} abstract class builder // abstract class, which specifies the three parts that must be implemented {public abstract void buildparta (); Public abstract void buildpartb (); public abstract product getresult ();} class concretebuilder1: builder // a specific builder class, inherit the abstract class and implement all the interfaces {private product Product = new product (); Public override void buildparta () {product. add ("Part A");} public override void buildpartb () {product. add ("Part B");} public override product getresult () {return product;} class concretebuilder2: builder // specific builder class {priva Te product Product = new product (); Public override void buildparta () {product. add ("Part X");} public override void buildpartb () {product. add ("part y");} public override product getresult () {return product;} class director // implement each sub-module {public void construct (Builder) // The abstract class is passed in. You can actually pass in the object of the specific implementation class. {Builder. buildparta (); builder. buildpartb () ;}} class program {static void main (string [] ARGs) {Director ctor = new director (); builder b1 = new concretebuilder1 (); builder b2 = new concretebuilder2 (); Director. construct (B1); // The conductor uses builder1 to build the product p1 = b1.getresult (); // gets the constructed product p1.show (); Director. construct (B2); product P2 = b2.getresult (); p2.show (); console. read ();}}}






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.