Template method pattern _c language for C + + design Patterns

Source: Internet
Author: User

Objective

Leaving the company that has worked for nearly two years, the days are no longer hectic, can be idle down, lying at home on the bed, thinking of how to go after the road, to tell the truth, really confused, graduated from 2012 to now, not a long time, but the things learned really is very limited, has been engaged in the development of Windows platform. Speaking of the development of Windows platform, we are sure to know the hook, even if you do not know the hook, for COM should also know, my series of Bowen also made a comprehensive summary of COM. Plainly, the hook is in the implementation of a function, there will be a series of implementation process, for this process is generally fixed, such as: What the first step, what the second step, what the last step, are designed. and concrete how to complete each step, can be controlled by the programmer. COM is also the case, COM is facing interface, when the completion of a functional module, it may be a series of interfaces of the stack call, and interface implementation is controlled by the programmer. Plainly, a function of the implementation of the module is fixed, but for each step of the specific implementation is not fixed. How do you usually do this kind of demand? From the design pattern point of view, this design pattern is called the template method pattern, you may not know the name of the design pattern, but, this method you have already used. Now I have a detailed summary of the template method pattern.

Template method Mode

In Gof's design pattern: The basics of reusable object-oriented software, the template method pattern is defined in this way: Define an algorithm skeleton in an operation, and delay some steps into subclasses. Templatemethod allows subclasses to redefine certain steps of a modified algorithm without altering the interface of an algorithm.

I combine this template method pattern with an example from my actual development project. We have done a product, this product is similar to a cloud of file management client. For such a client, because of its cloud server has three kinds of, and each kind of communication between the server and external interface is not consistent, this need to implement the client to screen cloud server and interface differences, and provide a unified operating interface, so in the implementation of this client at the same time, We have implemented a framework that is common to servers and interfaces, such as taking a file download to say. Our implementation is probably as follows:

Copy Code code as follows:

Class FileOperation
{
Public
BOOL DownloadFile (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 bool Dobegindownloadfile (wchar_t *psrc, wchar_t *pdest);
virtual bool Dodownloadfile (wchar_t *psrc, wchar_t *pdest);
virtual bool Doenddownloadfile (wchar_t *psrc, wchar_t *pdest);
};

Class Httpfileoperation:public FileOperation
{
Protected
virtual bool Dobegindownloadfile (wchar_t *psrc, wchar_t *pdest);
virtual bool Dodownloadfile (wchar_t *psrc, wchar_t *pdest);
virtual bool Doenddownloadfile (wchar_t *psrc, wchar_t *pdest);
};

Class Soapfileoperation:public FileOperation
{
Protected
virtual bool Dobegindownloadfile (wchar_t *psrc, wchar_t *pdest);
virtual bool Dodownloadfile (wchar_t *psrc, wchar_t *pdest);
virtual bool Doenddownloadfile (wchar_t *psrc, wchar_t *pdest);
};

The process of downloading the file is: First call Dobegindownloadfile, perform some operations before downloading the file, then call Dodownloadfile to implement the real file download, and finally call Doenddownloadfile to complete the cleanup of the file download. For any server, this process of downloading files will not change. And in the Dobegindownloadfile, dodownloadfile and doenddownloadfile internal specific is how to achieve, by the programmer according to the specific cloud server and the external interface to complete. When the end client completes the file download operation, only the DownloadFile function can be called to complete. As you can see, in the above code, only DownloadFile is public, and the other operations are protected. This also means that the framework we have completed externally exposes only the DownloadFile interface.

UML Class Diagram

AbstractClass (abstract Class): Defines the primitive operations of abstractions, and the specific subclasses redefine them to implement the steps of an algorithm. The main is to implement a template method to define the skeleton of an algorithm. The template method calls not only primitive operations, but also operations defined in AbstractClass or other objects.
Concreteclass (Concrete Class): Implements the primitive operation 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 invariant algorithm flow is completed in the Templatemethod of AbstractClass.

Use occasion

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 should indicate which operations can be redefined and which ones must be redefined. To effectively reuse an abstract class, a subclass writer must have a clear understanding of 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 the model is very simple, there is no more to speak of.

Copy Code code as follows:

#include <iostream>
using namespace Std;

Class AbstractClass
{
Public
void Templatemethod ()
{
PrimitiveOperation1 ();
cout<< "Templatemethod" <<endl;
PrimitiveOperation2 ();
}

Protected
virtual void PrimitiveOperation1 ()
{
cout<< "Default Operation1" <<endl;
}

virtual void PrimitiveOperation2 ()
{
cout<< "Default Operation2" <<endl;
}
};

Class Concreteclassa:public AbstractClass
{
Protected
virtual void PrimitiveOperation1 ()
{
cout<< "Concretea Operation1" <<endl;
}

virtual void PrimitiveOperation2 ()
{
cout<< "Concretea Operation2" <<endl;
}
};

Class Concreteclassb:public AbstractClass
{
Protected
virtual void PrimitiveOperation1 ()
{
cout<< "Concreteb Operation1" <<endl;
}

virtual void PrimitiveOperation2 ()
{
cout<< "Concreteb Operation2" <<endl;
}
};

int main ()
{
AbstractClass *pabstracta = new Concreteclassa;
Pabstracta->templatemethod ();

AbstractClass *PABSTRACTB = new Concreteclassb;
Pabstractb->templatemethod ();

if (pabstracta) Delete pabstracta;
if (PABSTRACTB) Delete pabstractb;
}

Summarize

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

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.