Builder mode of design pattern

Source: Internet
Author: User

  Builder mode: Separates the construction of a complex object from its representation so that the same build process can create different representations. (Go to "Big talk design Mode").

After learning this pattern, I do not know how to compare with the previous simple factory model, and found that both are created objects. But the two are still different, and the simple factory model is a more diverse scenario for creating different objects,

The builder pattern is primarily used to create complex objects in which the build order is usually stable, but the inside of the object

Construction often faces complex changes.

The benefit of the builder model is that the construction code is separated from the presentation code, and because the build mode prints out how the product is assembled, it is necessary to change the internal representation of a product, just to define a concrete builder.

The following code: use graphics to paint.

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceBuilder Mode { Public classMan {PrivatePen p; PrivateGraphics G;  PublicPen P {Get            {                returnp; }            Set{p=value; }        }         PublicGraphics G {Get            {                returnG; }            Set{g=value; }        }         PublicMan (Pen MyP, Graphics myg) { This. P =MyP;  This. G =Myg; }         Public Virtual voidBuildhead () {} Public Virtual voidbuildbody () {} Public Virtual voidBuildleftarm () {} Public Virtual voidBuildrightarm () {} Public Virtual voidBuildleftleg () {} Public Virtual voidBuildrightleg () {}} Public classThinman:man { PublicThinman (Pen p, Graphics g):Base(P, g) {} Public Override voidBuildhead () {g.drawellipse (P, -, -, -, -); }         Public Override voidBuildbody () {G.drawrectangle (P, -, -,Ten, -); }         Public Override voidBuildleftarm () {g.drawline (P, -, -, +, -); }         Public Override voidBuildrightarm () {g.drawline (P, -, -, -, -); }         Public Override voidBuildleftleg () {g.drawline (P, -, -, $, Max); }         Public Override voidBuildrightleg () {g.drawline (P, -, -, -, Max); }    }     Public classFatman:man { PublicFatman (Pen p, Graphics g):Base(P, g) {} Public Override voidBuildhead () {g.drawellipse (P, -, -, -, -); }         Public Override voidBuildbody () {g.drawellipse (P, $, -, +, -); }         Public Override voidBuildleftarm () {g.drawline (P, -, -, -, -); }         Public Override voidBuildrightarm () {g.drawline (P, the, -, -, -); }         Public Override voidBuildleftleg () {g.drawline (P, -, -, $, Max); }         Public Override voidBuildrightleg () {g.drawline (P, -, -, -, Max); }    }     Public classManbuilder {man m;  PublicManbuilder (man MM) { This. m =MM; }         Public voidShow () {m.buildhead ();            M.buildbody ();            M.buildleftarm ();            M.buildrightarm ();            M.buildleftleg ();        M.buildrightleg (); }    }     Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidButton1_Click (Objectsender, EventArgs e) {Pen P=NewPen (Color.Blue); Graphics Gthin=Picturebox1.creategraphics (); Thinman Tman=NewThinman (P, Gthin); Manbuilder m=NewManbuilder (Tman);            M.show (); Graphics Gfat=Picturebox2.creategraphics (); Fatman Fman=NewFatman (P, Gfat); Manbuilder M1=NewManbuilder (Fman); M1.        Show (); }    }}

Operation Result:

Builder mode of design pattern

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.