Builder mode and prototype mode

Source: Internet
Author: User

I. Builder Mode

The so-called builder mode is to create a complex class through different objects.

The builder mode also solves the following problems: When the objects to be created are complex (usually composed of many other objects ), we need the complex object creation process and the representation (Display) of this object.
The advantage of doing so is to build complex objects step by step. Because parameters can be introduced in the construction process of each step, this makes the presentation of the objects obtained after the creation of the same steps different.

First look at the class diagram:

Code example: (assume that the chicken wings, hamburgers, cola, and so on are existing classes)

# Include <iostream >#include <string> # include <vector> using namespace STD; Class taocan {public: taocan (){}~ Taocan () {}vector <string> V ;}; class builder {public: Builder (){}~ Builder () {} void addfood (string foodname) {TC. v. push_back (foodname);} void create () {cout <"The package consists of the following foods" <Endl; For (vector <string>: iterator iter = tc. v. begin (); iter! = Tc. v. end (); ++ ITER) {cout <* ITER <"" ;}} PRIVATE: taocan TC ;}; int main () {// The customer informs the waiter, the waiter told builder to combine the waiter and builder. // The customer told the waiter to: hamburger, chicken wings, cola, and fries. // So... builder * B = new Builder (); B-> addfood ("Hamburg"); B-> addfood ("Chicken Wings"); B-> addfood ("Cola "); b-> addfood ("fries"); B-> Create (); System ("pause"); Return 0 ;}

Ii. Prototype

The prototype mode also provides the self-replication function, which means that new objects can be created through existing objects. In C ++, copying constructor was a nightmare for programmers, the magic tricks of shallow copy and deep copy are also one of the root causes of fast food and system crashes during interviews by many programmers.

The class diagram is as follows:

The prototype mode. To put it bluntly, one object is used to initialize another object, achieving the replication effect.

The sample code is as follows:

#include<iostream>#include<string>#include<vector>using namespace std;class YuanXing{public:YuanXing(){}~YuanXing(){}virtual YuanXing* clone()=0;};class JuTiYuanXing:public YuanXing{public:JuTiYuanXing(){}JuTiYuanXing(JuTiYuanXing &m){*this=m;}~JuTiYuanXing(){}JuTiYuanXing*  clone(){return new JuTiYuanXing(*this);}};int main(){JuTiYuanXing *aa=new JuTiYuanXing;JuTiYuanXing *bb=aa->clone();system("pause");return 0;}

Prototype mode obtains the new object creation function by copying prototype. prototype itself is an "Object factory" (because it can produce objects). In fact, prototype mode and builder Mode,
In the abstractfactory mode, a class (object instance) is used to create objects (factory objects). The difference between them is: the builder mode focuses on step-by-step creation of complex objects (objects are not directly returned). The abstractfactory mode focuses on generating multiple dependent classes, while the prototype mode focuses on copying from itself
You have created a new class.

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.