Template method Mode (behavioral mode)
To change and not to change
Change-is the eternal theme of software, how to manage the complexity of change? The artistry and complexity of design patterns lie in how they are analyzed, and discover the changing points and stability points in the system, and use specific design methods to cope with this change.
Motive (motivation)
In the software construction process, it often has a stable overall operation structure for a task, but each sub-step has a lot of changing requirements, or because of fixed reasons (such as the relationship between the framework and the application) and the overall structure of the task can not be implemented simultaneously.
How to flexibly deal with the change of sub-steps or the requirement of late realization under the premise of determining stable operation structure?
Intentions (Intent)
Defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. Temple method allows subclasses to redefine some specific steps of the algorithm without altering the structure of an algorithm. --"Design pattern" GoF
Structure (Structure)
Automotive Test Framework Development Example
Automotive Test Framework developers do not know what to test the operation of the specific car, all can only develop a framework, and then by the Hongqicar developers rewrite the framework to implement the specific method.
//Automotive Test Framework Developer-first development Public Abstract classVehical//represents a car { protected Abstract voidStartup (); protected Abstract voidRun (); protected Abstract voidTurn (intdegree); protected Abstract voidStop (); Public voidTest () {//... Test Data LoggingStartup ();//late binding--left to application developers; extension points//... Test Data LoggingRun ();//late binding--left to application developers; extension points//... Test Data LoggingTurn (...);//late binding--left to application developers; extension points//... Test Data LoggingStop ();//late binding--left to application developers; extension points } } Public classVehicaltestframework { Public Static voiddotest (vehical vehical) {//...vehical. Test (); } }
//Red Flag Car Developer--post-development Public classhongqicar:vehical {protected Override voidStartup () {//...... } protected Override voidRun () {//...... } protected Override voidTurn (intdegree) { //...... } protected Override voidStop () {//...... } }
Several points of Temple method mode
- Temple method mode is a very basic design pattern and has a lot of applications in object-oriented system. It uses the most concise mechanism (the polymorphism of virtual function) to provide flexible extensibility points for many application frameworks, and is the basic implementation structure of code reuse.
- In addition to the flexibility to respond to changes in the sub-steps, "Do Not call me, let me call you" the reverse control structure is a typical application of template method.
- In terms of implementation, virtual methods called by template method can have implementations or no implementations (abstract methods, pure virtual methods), but they are generally recommended to be set to the protected method.
Reprint please specify the source:
Jesselzj
Source: http://jesselzj.cnblogs.com
Design mode 13:template Method template approach mode (behavioral mode)