Click here to Skip to main content
15,868,164 members
Articles / Programming Languages / C++11
Tip/Trick

Policy Design Pattern and Variadic Template Technique

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
8 Apr 2015CPOL2 min read 18.2K   7   2
This article presents simple combination of Policy design pattern and Variadic Template Technique.

Introduction

Every software developer in C++ is faced with the generic programming. Some of them have started to use the Policy design pattern. I have spent much time on research of that pattern and found an interesting form of it with the feature of C++11 - Variadic template technique.  

Background

I have spent much time on research of Policy design pattern and implemented it once in one commercial project. However, some time ago, I decided to improve my project by including C++11 features and found a very nice form for Policy pattern.

Using the Code

The new form of Policy pattern can have the next form:

C++
template<typename... Policies>
class PolicyAndVaradic: public Policies{};

This form is different from the old form:

C++
template<typename PolicyOne, typename PolicyTwo>
class PolicyOldForm: public PolicyOne, PolicyTwo{};

It leads to the interesting idea - it is possible to create a class which amount of policies is resolved in the time of compiling. 

So, you can ask - how it can be used? I wrote a simple code:

C++
template<typename... Policies>
class PolicyAndVariadic: public Policies...{};

class PolicyOne
{
public:
    void executePolicyOne()
    {
        std::cout << "execute PolicyOne" << std::endl;    
    }
};

class PolicyTwo
{
public:
    void executePolicyTwo()
    {
        std::cout << "execute PolicyTwo" << std::endl;
    }
};

typedef PolicyAndVariadic<PolicyOne, PolicyTwo> PolicyOneAndPolicyTwo;

PolicyOneAndPolicyTwo linstance;

linstance.executePolicyOne();

linstance.executePolicyTwo();

As you see, in the code the template class PolicyOneAndPolicyTwo includes implementation of classes PolicyOne and PolicyTwo via the template PolicyAndVariadic. In old style, the amount of Policies was limited by old standards of C++. The C++11 allows to get more nice view policy template class. 

You can ask - it works perfectly with default constructors, but what to do if we want to include policies, which constructors have to arguments? It leads to the next nice view form:

C++
template<typename... Policies>
class PolicyAndVaradic: public Policies...
{
public:
    template<typename... Args>
    PolicyAndVaradic(const Args... Arg)
        : Policies(Arg)...{}
};

class PolicyOne
{
    std::string mText;

public:
    PolicyOne(const char* aText):mText(aText){}
    
    void executePolicyOne()
    {
        std::cout << mText << std::endl;
    }
};

class PolicyTwo
{
    std::string mText;
public:

    PolicyTwo(const char* aText):mText(aText){}

    void executePolicyTwo()
    {
        std::cout << mText << std::endl;
    }
};

PolicyOneAndPolicyTwo linstance("PolicyOne", "PolicyTwo");

linstance.executePolicyOne();

linstance.executePolicyTwo();

So, I think that I do not need to explain how it works.

Of course, you can say the every serious library on policy pattern needs more complex actions with the policies and this form cannot be suitable. I agree with it. However, if you want to create a simple template solution with static polymorphism, then the policy pattern with variadic template can be a suitable solution.

Points of Interest

I think that it could be interesting to include a Lambda expression pattern into the policy pattern.

History

  • 8th April, 2015: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionVariadic Policues Pin
geoyar13-Apr-15 13:34
professionalgeoyar13-Apr-15 13:34 
AnswerRe: Variadic Policues Pin
Evgeny Pereguda13-Apr-15 20:00
Evgeny Pereguda13-Apr-15 20:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.