lundi 20 avril 2015

Must Invoke first design pattern

I am looking for an elegant solution for my case. I tried to find a design pattern that specified and offers solution for this case but i failed to find one.

I have a base class that uses to store general object and later Invoke it. I want the execution will be separated into two parts:

  1. A must have part which will always take place (do1st()).
  2. User defined code (do2nd()).

For example:

class InvokeBase
{
public:
    InvokeBase(void *ptr) : context_(ptr) {}
    virtual ~InvokeBase () {}
    void operator()() = 0; 
protected:
    void do1st() {//Mandatory code to execute for every InvokeBase type when calling operator()};
    void * context_;
};

class InvokeDerived : public InvokeBase
{
public: 
    InvokeDerived(void *ptr) : base(ptr){}
    virtual ~InvokeDerived();
    void do2nd() {//User defined code}
    void operator()()  
    {
        do1st();  // << How to force this execution?
        do2nd();
    } 
};

void main()
{
     InvokeBase *t = new InvokeDerived();
     t(); // << here i want the execution order will be do1st and then do2nd. 
}

The trick is that i want do1st will execute always, that i will not have to call it from InvokeDerived. I want to allow the user to inherit from InvokeBase with the guarantee that do1st will always be called when invoking the operator().

Aucun commentaire:

Enregistrer un commentaire