Builder mode and prototype mode

Source: Internet
Author: User

1. The Builder mode is called the builder mode, which creates 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 to separate the process of creating a complex object from the representation (Display) of this object. The advantage of this is to build a complex object step by step, because parameters can be introduced in the construction process of each step, the presentation of objects obtained after the creation of the same step is different. First, let's look at the class diagram: code example: (assume that the chicken wings, hamburgers, cola, and so on are existing classes) [cpp] # 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 ;} # 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;} 2: 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: Prototype. To put it bluntly, one object is used to initialize another object, achieving the replication effect. The sample code is as follows: [cpp] # 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 ;} # 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;} The Prototype mode obtains the function of creating a new object by copying the Prototype, prototype itself is an "Object factory" (because it can produce objects). In fact, Prototype mode and Builder mode and AbstractFactory mode both use a class (object instance) specifically responsible for object creation (factory objects). The difference between them is that the Builder mode focuses on step-by-step creation of complex objects (objects are not directly returned ), the AbstractFactory mode focuses on generating multiple mutual dependencies. Depends on the class object, and the Prototype mode focuses on copying itself to create 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.