Designpatterns Study Notes: C + + language implementation---1.5 Prototype

Source: Internet
Author: User

Designpatterns Study Notes: C + + language implementation---1.5 Prototype

2016-07-21

(WWW.CNBLOGS.COM/ICMZN)

Pattern understanding

1. Prototype prototype Mode definition
Provides the ability of a class to replicate itself by creating new objects from existing objects. Because the prototype pattern is actually a copy of the binary stream in memory,
So the performance is better than just passing the new one object. Different implementations are related to the specific language.

2. Advantages of prototype mode
(1) Excellent performance, in-memory binary-based copy
(2) In the prototype mode, it does not pass the constructor function.

3. Application Scenarios for prototype mode
(1) need to create a large number of objects according to existing objects;
(2) The creation of an object requires a lot of preparation, waiting for other modules to be ready;
(3) The same object is paid to other objects for processing.

4. Prototype model discussion and attention issues
(1) Prototype mode note shallow copy and deep copy
Language base type shallow copy, pointer, reference shallow copy only address content needs attention
C + +: implementation of a copy constructor that requires a dependent class
(2) This is the replication problem that requires reference to the object

* A class member is a class member created by new polymorphism to make a deep copy and to be ~xx in a destructor.
* When the memory assignment operation is = (), the right side is a temporary object due to the operation, you can perform an efficient assignment, and the member directly points to the location of the member address within the temporary object.
Then, set the member pointer of the temporary object to nullptr. "Mobile Assignment Construction"
Prototype:
Cxxobject (const cxxobject && Linshi)
{//Move constructor
M_pname = Linshi.m_pname;
Linshi.m_pname = nullptr;

}
(3) In fact, Prototype, Builder, and Abstract factory all create other classes through a Class (object)
However, there is a point of focus between them.
Builder focuses on the object building process, does not directly return the class object, the Supervisor director coordinates the specific builder class to create the object separately according to the different construction process;
Abstract Factory focuses on the specific factory to create different objects of this type, and is directly returned to the object;
Prototye focuses on returning a copy of this object based on an object, you must pay attention to the deep copy. 

Program Implementation (c + +)

Prototype.h

1 #pragmaOnce2 3#include <string>4#include <iostream>5#include <vector>6 classCabsprototype7 {8  Public:9 Cabsprototype ();Ten~Cabsprototype (); One  Public: A     Virtualcabsprototype* Clone ()Const; - }; -  the  - classCconcentprototype: PublicCabsprototype - { - Private: +     //language base type shallow copy, pointer, reference shallow copy only address content needs attention -     intM_iage; +     Char* M_PNAME;//deep Copy, construction flush initialization, coordinated shampoo in System Astd::vector<Double>M_dscores; at  -  -  Public: -     //Note that there is no default value parameter required to be in front of the default values, the inevitable reason -Cconcentprototype (Conststd::vector<Double>& SOCRESV,Const Char* AName ="Default Name",intAge = -): M_iage (age) -     { inM_pname =nullptr; -  to         intL = strlen (aName) +1; +M_pname =New Char[l]; - strcpy_s (M_pname, L, aName); the  *         //M_dscores.reserve (Socresv.size ()); $M_dscores =SOCRESV;Panax NotoginsengStd::cout <<"The generic constructor executes. "<<Std::endl; -     } the~Cconcentprototype () +     { A delete[] M_pname; the     } +Cconcentprototype (Constcconcentprototype& obj)//copy Constructor -     { $M_iage =Obj.m_iage; $  -         intL = strlen (obj.m_pname) +1; -M_pname =New Char[l]; the strcpy_s (M_pname, L, obj.m_pname); - Wuyi m_dscores.clear (); theM_dscores =Obj.m_dscores; -Std::cout <<"The copy constructor executes. "<<Std::endl; Wu     } -     //deep copy and shallow copy are considered Aboutcabsprototype* Clone ()Const Override; $     voidOutPut () -     { -Std::cout << m_pname << m_iage <<"Score:"; -  A          for(auto t:m_dscores) +Std::cout << T <<"   "; theStd::cout <<Std::endl; -     } $  the};

Prototype.cpp

1#include"AbsPrototype.h"2 3 4 Cabsprototype::cabsprototype ()5 {6 }7cabsprototype::~Cabsprototype ()8 {9 }Ten  Onecabsprototype* Cabsprototype::clone ()Const A { -     returnnullptr; - } the  -  - // -cabsprototype* Cconcentprototype::clone ()Const + { -     //implementation of Prototype mode using copy-mess function +cconcentprototype* p =NewCconcentprototype (* This); A     returnp; at     //child pointer, assigned to the parent class pointer, converted to a subclass pointer at the call -}

(1) Template application

Main.cpp

1 //Prototype.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include"AbsPrototype.h"6#include <iostream>7 8 9 int_tmain (intARGC, _tchar*argv[])Ten { Onestd::vector<Double> scores{11.2,33.4,45.6,99.9}; Acconcentprototype* POBJ =NewCconcentprototype (scores,"Zhongguo", -); -Pobj->OutPut (); -  theStd::cout <<"-----------------"<<Std::endl; -      for(inti =0; I <Ten; i++) -     { -cconcentprototype* Ptem = (cconcentprototype*) pobj->clone (); +Ptem->OutPut (); -     } +  ASystem"Pause"); at     return 0; -}

(2) Output display

Designpatterns Study Notes: C + + language implementation---1.5 Prototype

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.