Design pattern C + + Implementation IX: Builder mode

Source: Internet
Author: User

Builder mode: Used primarily to create complex objects in which the built-in order of internal builds is usually stable, but the construction of objects within the object often faces complex changes. The advantage is that the construction code is separated from the presentation code, and because the builder hides how the product is assembled, the internal representation of a product needs to be changed, and only one concrete builder can be defined. The builder pattern is when the algorithm that creates the complex object should be independent of the part of the object and the mode in which they are assembled.

#ifndef builder_h#define bulder_h#include<iostream>//#include <windows.graphics.printing.h>using namespace Std;class person{public:virtual void Buildhead () =0;virtual void buildbody () = 0;virtual void Buildarmleft () = 0; virtual void buildarmright () = 0;virtual void Buildlegleft () = 0;virtual void Buildlefrighy () = 0;}; Class Fatperson:p ublic person{public:void buildhead () {cout << "fathead\n";} void Buildbody () {cout << "fatbody\n";} void Buildarmleft () {cout << "Fat armleft\n";} void Buildarmright () {cout << "Fat armright\n";} void Buildlegleft () {cout << "Fat legleft\n";} void Buildlefrighy () {cout << "Fat legright\n";}}; Class Thinperson:p ublic person{public:void buildhead () {cout << "thinhead\n";} void Buildbody () {cout << "thinbody\n";} void Buildarmleft () {cout << "Thin armleft\n";} void Buildarmright () {cout << "Thin armright\n";} void Buildlegleft () {cout << "Thin legleft\n";} void Buildlefrighy () {cout << "Thin LegrigHt\n ";}}; Class Persondirector{person * Per;public:persondirector (person * m):p er (m) {}void Creatperson () {Per->buildarmleft ( );p er->buildarmright ();p er->buildbody ();p er->buildhead ();p er->buildlefrighy ();p Er->buildlegleft ();}}; #endif
#include "Bulder.h" int main () {person * F = new Fatperson; Persondirector PDf (F); Pdf.creatperson (); Person * T = new Thinperson; Persondirector PDt (T); Pdt.creatperson (); return 0;}


Design pattern C + + Implementation IX: 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.