This is my first post, so be kind. This is a question from an interview I recently received, but I could not find the answer after the search (Google, C ++ FAQ, etc.).
There is an I1 interface with the behavior of b1 (). There are 3 classes A, B, C. All of these classes implement the I1 interface, overriding b1 (). There is a fourth class D, which has behavior (b1) defined in interface I1, and additional behavior b2
Question: how do you create class D.
My answer was to create another I2 interface that defines the behavior of b2 () and makes class D implement I1 and I2 (multiple inheritance in C ++) by overriding both b1 () and b2 ()
The interviewer agreed with the decision, but asked that if new classes with a new set of behaviors appear in the future, how will we deal with this
I could only think of adding additional interfaces (I3, I4, etc.) and performing multiple inheritance, but I know that here you get into a huge number of derived classes with corresponding interfaces
The interviewer seemed to be expecting a better solution, but he did not disclose the answer. I would be interested to know how experts will solve this design problem.
PS: After thinking seriously about this, I think the answer may be to use a design pattern, but looking at the general design patterns, I could not find a single one that would fit this problem.
edit 1: , , , , .
@Nawaz, @Alexandre @Sjoerd . ++/design, .
Vistor by @Nawaz , , , . @Alexandre .
.
,
1) , . ( )
: ( @Nawaz) - , , , ..
2) , ( )
: , , ( , )
3) . , , - .
, , 1), , 2) 3).
IDude, , Dude, .
class IComposerBehavior;
class IComposer
{
public:
virtual ~IComposer() {}
virtual void accept(IComposerBehavior *behaviour) = 0 ;
};
class IComposerBehavior
{
public:
virtual ~IComposerBehavior() {}
virtual void visit(IComposer * ) = 0;
};
class Dude : public IDude, public IComposer
{
public:
virtual void accept(IDudeBehavior *behaviour)
{
behaviour->visit(this);
}
virtual void accept(IComposerBehavior *behaviour)
{
behaviour->visit(this);
}
};
class SymphonyComposerBehavior : public IComposerBehavior
{
public:
virtual void visit(IComposer *dude) { cout << "Dude is composing a symphony" << endl; }
};
SymphonyComposerBehavior.
, Dude , .
, , , . , , , , @Alexandre?
( , ). , - Robot. ,
- Initially We are only producing Toy Robots
- Then Human helper Robots
- Then Self Healing Robots (would just correct itself when defective)
- Then Humanoid Robots
- Then machine Robots (that are not human like but as a substitute for any machine you can think of) . I have deliberately put this here even though its place should be before with a correct evolution scheme.
- finally Humanoid Robots with life (atleast we can dream :-) )
, , ,
, . , , , (, , ), ?
.