views:

119

answers:

4

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.. Events 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();
}

}
+7  A: 

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.

Matthieu M.
features may use the same underlying data structure... In that case, this does not seem to be good...
Aftershock
@Aftershock: I fail to see why this would be a problem.
ereOn
e.g. how will m2 manipulate data? Friends?
Aftershock
@Aftershock: It's kinda difficult to answer that without having a perfect understand of what exactly are your "features" and what they share.
ereOn
@Matthieu M. HAS-A == aggregation?
Kezzer
@Kezzer: HAS-A = containment. @Aftershock: it is up to the `program` class to define its interface and call `m1` and `m2` appropriately.
Matthieu M.
A: 

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.

Elemental
Sorry number of functions could be misleading...These functions (function1,function2 in different classes) may not be related at all..
Aftershock
A: 

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.

Semen Semenych
A: 

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: http://stackoverflow.com/questions/573913/a-use-for-multiple-inheritance

identitycrisisuk