开发者

Question about a possible design pattern

开发者 https://www.devze.com 2023-01-02 15:51 出处:网络
I have such a design in my mind.... My aim is to reuse the program with some features included and without some features.

I have such a design in my mind.... My aim is to reuse the program with some features included and without some features. What is it called in the literature? More information: there are events.. Even开发者_运维问答ts cause calls to function1() or function2()...

Features have functions which are called when events takes place. A feature may influence what functions are called at a event. A feature may influence what is executed for more than one event.

So it looks it could be the observer pattern + hasa relationship...

class feature1
{
void feature1functionx();
void feature1functiony();
}

class feature2
{
void feature2functionw();
void feature2functionz();

}

class program: feature1, feature2
{
 vector<string> data;
void function1()
{
 feature2functionw();
}
void function2()
{
 feature1functiony();
 feature2functionz();
}
void execute()
{
  function1();
  function2();
}

}


Inheritance models a IS-A relationship.

It would seem natural here to use a HAS-A relationship if you wish to reuse the functions: and that is composition.

class program
{
public:
  void function1()
  {
    m2.feature2functionw();
  }
  void function2()
  {
    m1.feature1functiony();
    m2.feature2functionz();
  }
  void execute()
  {
    this->function1();
    this->function2();
  }
private:
  feature1 m1;
  feature2 m2;
};

I know that private inheritance is sometimes thought of as a short-cut, but it does not bring anything to the table here, so prefer composition as it does not tie you as much.

EDIT: Added the definition of the methods since it apparently wasn't that clear.


This idea seems strange and complicated to me. This is what refinement and replacement are for in a class hierarchy.

Specifically I would be looking for something like this:

class feature1
{
  virtual void function1();
  virtual void function2();
  void execute() {
    function1();
    function2();
  }

}

class feature2:public feature1 //this is your program class
{
  virtual void function1() 
  {
     feature1::function1() //call previous functionality to refine
     //do feature2 stuff here
  }

  virtual void function2()
  {
     //don't call base class to replace
     //do feature2 stuff here
  }
}

Now feature2.Execute() does what you want.


The code you posted reminds me of the so-called "Template method" pattern. In theory, this pattern looks like this:

class Base
{
public:
    void execute()
    {
        function1();
        function2();
    }
private:
    virtual void function1();
    virtual void function2();
};

and then later in some class:

class Derived1 : public Base
{
    void function1()
    {
        //implementation here...
    }
};

and you then call

b->execute(); //b is of type Base* and points to an object of type Derived1

and get your stuff done.

Not sure how this would work in your case, probably not at all, but at least this is an example of a pattern very similar to what you describe.


It almost sounds like an interface class but it sounds like you are implementing a lot of code in those base classes rather than have them purely abstract, which is the common way of making interfaces in C++. I suppose this could work if you're sure the base classes will never share any names etc. I don't know if they could also be described as mixin classes as described in this question: A use for multiple inheritance?

0

精彩评论

暂无评论...
验证码 换一张
取 消