Design Pattern series prototype Pattern

Source: Internet
Author: User

Design Pattern series prototype Pattern
Prototype mode specifies the type to be created through the instance object, which is essentially different from the factory method mode we mentioned in the previous article, the factory method mode defines different subclasses through inheritance of classes to create different types of objects. It belongs to the class mode. prototype mode generates different types of object instances by calling the combined object members, belongs to the object mode. Due to this feature, prototype is applicable to the following scenarios: · when you need to determine the instantiated class at runtime, such as when dynamically loading the library, avoid creating too many subclasses. Too many sub-classes are always undesirable. In factory method, we also mention using templates or parameterization to reduce the number of sub-classes. · If the status of the instantiated object is too many combinations, it is more convenient to create a prototype Library first and obtain the prototype through the Registry to generate the target object. Advantages of prototype include: Adding and deleting products during runtime, changing objects, this is a common advantage of the combination mode-reducing the number of child classes-the biggest drawback of applying prototype dynamic configuration using classes is that it may be difficult to implement clone operations in some languages, especially when it contains the amount of circular references. Next we will continue to use the example in the previous article to develop a chip design software and implement it with prototype. All images are inherited from MaskFigure and must implement the clone function. The object prototype pointer is input during MaskDesigner initialization, and the new image is generated by clone of the prototype during MakeFigure. The Factory method calls the constructor of the graphic class to generate a new object through the class corresponding to the graph. In other words, the factory method uses the design drawing to re-draw a new image, prototype is a figure already drawn by copy. The class structure is as follows: code implementation: copy the code // mask. hpp # ifndef MASK_HPP # define MASK_HPP class MaskFigure {public: virtual ~ MaskFigure () = 0; virtual MaskFigure * clone () = 0; protected: MaskFigure (); MaskFigure (const MaskFigure &) ;}; class MaskRound: public MaskFigure {public: maskRound (); MaskRound (const MaskRound &); MaskRound * clone ();~ MaskRound () ;}; class MaskRec: public MaskFigure {public: MaskRec (); MaskRec (const MaskRec &); MaskRec * clone ();~ MaskRec () ;}; class MaskTri: public MaskFigure {public: MaskTri (); MaskTri (const MaskTri &); MaskTri * clone ();~ MaskTri () ;};# endif // mask. cpp # include <iostream> # include "mask. hpp "using std: cout; using std: endl; MaskFigure: MaskFigure () {cout <" init MaskFigure "<endl;} MaskFigure :: maskFigure (const MaskFigure & mf) {cout <"copy Figure" <endl;} MaskFigure ::~ MaskFigure () {cout <"delete MaskFigure" <endl;} MaskRound: MaskRound () {cout <"Draw roundness on Mask" <endl;} MaskRound :: maskRound (const MaskRound & mr): MaskFigure (mr) {cout <"copy roundness" <endl;} MaskRound * MaskRound: clone () {return new MaskRound (* this);} MaskRound ::~ MaskRound () {cout <"delete MaskRound" <endl;} MaskRec: MaskRec () {cout <"Draw rectangle on Mask" <endl;} MaskRec :: maskRec (const MaskRec & mr): MaskFigure (mr) {cout <"copy rectangle" <endl;} MaskRec * MaskRec: clone () {return new MaskRec (* this);} MaskRec ::~ MaskRec () {cout <"delete MaskRec" <endl;} MaskTri: MaskTri () {cout <"Draw triangle on Mask" <endl;} MaskTri :: maskTri (const MaskTri & mt): MaskFigure (mt) {cout <"copy triangle" <endl;} MaskTri * MaskTri: clone () {return new MaskTri (* this);} MaskTri ::~ MaskTri () {cout <"delete MaskTri" <endl;} // maskdesigner. hpp # ifndef FIGUREDESIGNER_HPP # define FIGUREDESIGNER_HPP # include "mask. hpp "class FigureDesigner {public: FigureDesigner (MaskFigure * mf) {figure = mf;} MaskFigure * MakeFigure () {return figure-> clone ();} private: maskFigure * figure;}; # endif // main. cc # include <memory> # include <iostream> # include "maskdesigner. hpp "using std: cout; using std: e Ndl; using std: shared_ptr; int main () {MaskRound mro; MaskRec mre; MaskTri mtr; FigureDesigner md1 (& mro); shared_ptr <MaskFigure> mfptr1 (partition ()); figureDesigner md2 (& mre); shared_ptr <MaskFigure> mfptr2 (md2.MakeFigure (); FigureDesigner md3 (& mtr); shared_ptr <MaskFigure> mfptr3 (partition ());} when copying code to prototype mode, it is necessary to mention the c ++ Covariance feature, that is, the covariant nature of c ++. when implementing a virtual function, generally, the virtual function to be rewritten by a derived class must have the same return value type as the base class function, the number of parameters and Type. However, this also has a special case. If the return value type meets the following conditions, it can be different. the Return Value Type of the base class and the derived class virtual function is a pointer to the class or reference 2. the return value pointer of a base-class virtual function or the class type that the reference points to is the return value pointer of a derived class or the indirect or direct base class that the reference points to, that is, belongs to the same inheritance system. This indicates that the following conditions are allowed. Copy the code class Base {/*... */}; class Derived: public Base {/*... */}; class B {virtual Base * func () {return new Base;} virtual ~ B () {}}; class D: public B {Derived * func () {return new Derived;} virtual ~ D () {}}; copy the code fanc's return values to base * and Derived *, respectively, within the same inheritance system. However, if the returned value is changed to another type, such as the basic type, it is not allowed. This feature is fully applied in prototype. The clone function of MaskFigure returns the MaskFigure * pointer type, and the clone functions of MaskRound, MaskRec, and MaskTri return the MaskRound *, respectively *, maskRec * And MaskTri * pointer types. (End)

Related Article

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.