C + + Design pattern-Template Method mode

Source: Internet
Author: User

Template Method Mode

In Gof's design model: The basics of reusable object-oriented software, the template method pattern is said to define an algorithmic skeleton in an operation, while delaying some steps into subclasses. Templatemethod allows subclasses to redefine some specific steps of the algorithm without changing the interface of an algorithm.

I'll combine this template approach pattern with an example of what I'm doing in a real-world development project. We have worked on a product that resembles a cloud-like file management client. For such a client, because there are three servers in the cloud, and the communication between each server and the external interface is not consistent, this requires the implementation of the client to shield the cloud server and interface differences, and provide a unified operating interface, so while implementing this client, We implemented a framework, a common framework for servers and interfaces, such as downloading files. Our implementation is probably as follows:

classfileoperation{ Public:     BOOLDownloadFile (wchar_t *psrc, wchar_t *pDest) {          if(!psrc | |!pdest)return false; if(! Dobegindownloadfile (PSRC, PDest))return false; if(! Dodownloadfile (PSRC, PDest))return false; if(! Doenddownloadfile (PSRC, PDest))return false; }protected:     Virtual BOOLDobegindownloadfile (wchar_t *psrc, wchar_t *pDest); Virtual BOOLDodownloadfile (wchar_t *psrc, wchar_t *pDest); Virtual BOOLDoenddownloadfile (wchar_t *psrc, wchar_t *pDest);};classHttpfileoperation: Publicfileoperation{protected:     Virtual BOOLDobegindownloadfile (wchar_t *psrc, wchar_t *pDest); Virtual BOOLDodownloadfile (wchar_t *psrc, wchar_t *pDest); Virtual BOOLDoenddownloadfile (wchar_t *psrc, wchar_t *pDest);};classSoapfileoperation: Publicfileoperation{protected:     Virtual BOOLDobegindownloadfile (wchar_t *psrc, wchar_t *pDest); Virtual BOOLDodownloadfile (wchar_t *psrc, wchar_t *pDest); Virtual BOOLDoenddownloadfile (wchar_t *psrc, wchar_t *pDest);};

The process of downloading the file is: Call Dobegindownloadfile First, perform some actions before downloading the file, then call Dodownloadfile to implement the real file download, and finally call Doenddownloadfile to complete the file download cleanup. For any server, the process of downloading the file will not change. The Dobegindownloadfile, Dodownloadfile, and Doenddownloadfile are implemented internally by programmers based on specific cloud servers and externally exposed interfaces. When the final client goes through the file download operation, it can only be done by invoking the DownloadFile function. As you can see, in the above code, only DownloadFile is public, and the other operation functions are protected. This also means that the framework we complete exposes only downloadfile interfaces to the outside.

UML Class Diagram

AbstractClass (abstract Class): Defines the primitive operations of the abstraction, and the specific subclasses redefine them to implement the steps of an algorithm. The main is to implement a template method, define an algorithm skeleton. The template method not only invokes primitive operations, it also invokes operations defined in AbstractClass or other objects.
Concreteclass (Concrete Class): Implements primitive operations to complete the steps associated with a particular subclass in the algorithm.
Because the steps of implementing an algorithm are redefined in the specific subclass Concreteclass, the constant algorithm flow is done in AbstractClass Templatemethod.

Use occasions

A template method is a basic technique for code reuse. They are particularly important in class libraries, which extract public behavior from the class library. When using template methods, it is important that the template method indicate which actions can be redefined and which must be redefined. To effectively reuse an abstract class, the subclass writer must clearly understand which operations are designed to be redefined.

Code implementation

Based on the above class diagram, this paper makes a simple implementation of the template method pattern. Since this mode is very simple, there is no more to talk about.

1#include <iostream>2 using namespacestd;3 4 classAbstractClass5 {6  Public:7      voidTemplatemethod ()8      {9 PrimitiveOperation1 ();Tencout<<"Templatemethod"<<Endl; One PrimitiveOperation2 (); A      } -  - protected: the      Virtual voidPrimitiveOperation1 () -      { -cout<<"Default Operation1"<<Endl; -      } +  -      Virtual voidPrimitiveOperation2 () +      { Acout<<"Default Operation2"<<Endl; at      } - }; -  - classConcreteclassa: PublicAbstractClass - { - protected: in           Virtual voidPrimitiveOperation1 () -      { tocout<<"Concretea Operation1"<<Endl; +      } -  the      Virtual voidPrimitiveOperation2 () *      { $cout<<"Concretea Operation2"<<Endl;Panax Notoginseng      } - }; the  + classCONCRETECLASSB: PublicAbstractClass A { the protected: +           Virtual voidPrimitiveOperation1 () -      { $cout<<"Concreteb Operation1"<<Endl; $      } -  -      Virtual voidPrimitiveOperation2 () the      { -cout<<"Concreteb Operation2"<<Endl;Wuyi      } the }; -  Wu intMain () - { AboutAbstractClass *pabstracta =NewConcreteclassa; $Pabstracta->Templatemethod (); -  -AbstractClass *PABSTRACTB =NewCONCRETECLASSB; -Pabstractb->Templatemethod (); A  +      if(PABSTRACTA)Deletepabstracta; the      if(PABSTRACTB)DeletePABSTRACTB; -}

Summarize

Template method mode, generally very good to accept, very good understanding, no difficulty; for this design pattern, I personally feel can be compared with the decoration mode. There are some similarities. Well, that's the end of the tutorial on this design pattern.

C + + Design pattern-Template Method mode

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.