*I’m using “Module” to mean some extension of a class, either through inheritance or composition.
Say I have one of the following declarations of an Entity
:
using EUnit = int;
class Entity : /*With inheritance*/
Positional<EUnit> {
public:
Entity();
};
or
class Entity { /*With composition*/
Position<EUnit> pos; //Same as "Positional", but renamed
public:
Entity();
};
And the declaration of Position
/Positional
:
template <class T>
class Positional {
T xPos;
T yPos;
public:
Positional();
Positional(T x, T y);
void moveTo(T x, T y);
void moveBy(T xOff, T yOff);
T getX() const;
T getY() const;
};
What if I want to create a class called RandomWalk
that can be used to cause the Entity
(or any other Positional
class) to randomly move to a location?
Going the inheritance route, I thought of having RandomWalk
inherit from Positional
so it has access to a position to act on. If I then set up Entity
like:
class Entity :
Positional<EUnit>, virtual RandomWalk<EUnit> { ...
RandomWalk
now how access to Entity
‘s position, but it involved “diamond inheritance”. I’m virtually inheriting RandomWalk
, so only one version of a Positional
should exist, but multiple inheritance like this still seems to be frowned upon from what I’ve read.
That leaves me with composition. The only way I could think of giving RandomWalk
access to the Entity
‘s position this way was to do something like pass a reference/pointer to the Entity
‘s Positional
to the RandomWalk
constructor. Using pointers for something like this seems like overkill though.
And now I’m out of ideas, and I’d like suggestions/advice (or just general comments on the problem).
I know I could just directly add a randomWalk
function to Entity
, but I’m looking at this through the perspective of re-use.
And yes, RandomWalk
is kind of a dumb use-case, but I’m sure this problem (or a similar one) occurs in the real world.
Some alternatives:
-
Have RandomWalk inherit from Positional. With this definition, a RandomWalk is a Positional entity which can walk randomly or to specific locations. I don’t really like this approach; it leaves no way to handle arbitrary permutations walkers (e.g., RandomWalk, SquareDanceWalk, etc.).
-
Have RandomWalk contain a “WalkRandomly” function which takes in a Positional parameter. Entities which need need random movement can call this function. If necessary, an abstract class ICanRandomWalk could be inherited from (diamond inheritance is fine if no more than one base class contains an implementation), with the ICanRandomWalk being implemented as calling a member of a private instance of RandomWalk.
2