Prototype mode: Specifies the kind of object created with the prototype instance, and creates a new object by copying the prototypes.
The central idea is cloning. For example, we often need to copy the ID card, sometimes need a few, in fact, is the clone, about the code and UML class diagram, I will no longer describe, because this is too simple, is to define a copy constructor, define a clone function, in the clone inside the new this pointer can be.
I still mainly record the template method mode, it is quite interesting to feel this.
Template method Pattern: Defines the skeleton of an algorithm in an operation, and delays some steps to the implementation of subclasses.
For example, we often need to write DD in the development, that is, the detailed design, the general project team will have a standard template, if this template by the module description, module composition, interface design, flow chart of the four parts (real life may be different, there will be more content, here just assume that, is to express the idea of template method pattern). A software system is generally composed of a lot of modules, each need to write DD, then we just need to press the template, the contents of their modules to fill in, and then submit on OK. If this needs to be implemented in C + +, how do we achieve that?
We can set a template base class, define module description, module composition, interface design, flowchart of the four virtual functions, and then define a base class is not a virtual function, the name of the function, each module to inherit the base class, and implement their own module description, module composition, interface design, flowchart This four parts can be, We call the module class that uses the base class to go to new, and the invocation function of the base class is implemented. If we now have pwer and network two modules that need to write DD, then its UML class diagram looks like this:
Perhaps the UML class diagram above is not intuitive enough, we are looking at the code may understand:
#include <iostream>using namespacestd;classbasetemplate{protected: Virtual voidModuledescription () {};//Module Description Virtual voidModulecomposition () {};//Module Composition Virtual voidInterfacedesign () {};//Interface Design Virtual voidFlowChart () {};//Flowchart Public: voidSubmit () {moduledescription (); Modulecomposition (); Interfacedesign (); FlowChart (); }};classNetwork: Publicbasetemplate{protected: Virtual voidModuledescription ()//Module Description{cout<<"Network moduledescription ()"<<Endl; } Virtual voidModulecomposition ()//Module Composition{cout<<"Network modulecomposition ()"<<Endl; }; Virtual voidInterfacedesign ()//Interface Design{cout<<"Network interfacedesign ()"<<Endl; }; Virtual voidFlowChart ()//Flowchart{cout<<"Network FlowChart ()"<<Endl; }; };classPower: Publicbasetemplate{ Public: Virtual voidModuledescription ()//Module Description{cout<<"Power moduledescription ()"<<Endl; } Virtual voidModulecomposition ()//Module Composition{cout<<"Power modulecomposition ()"<<Endl; }; Virtual voidInterfacedesign ()//Interface Design{cout<<"Power interfacedesign ()"<<Endl; }; Virtual voidFlowChart ()//Flowchart{cout<<"Power FlowChart ()"<<Endl; }; };intMainintargcChar**argv) {basetemplate* Modulea =NewNetwork (); Modulea-submit (); Basetemplate* Moduleb =NewPower (); Moduleb-submit (); DeleteModulea, Moduleb; return 0;}
The output interface is as follows:
Each design mode, each study, there is a "warm so know the new" feeling, then we have a lot of warm, more know the new bar, because of lifelong learning ...... .....
Prototype mode and template method mode (c + +)