tags:

views:

67

answers:

2

Lets say Michael Jackson and I are objects of same class HumanBeing. But he had a better implementation of the behaviour 'dance' than me .

How do I do this programatically , so that 2 objects of same class can have different implementation?

class HumanBeing
{
public :
    HumanBeing(){};
    void dance() { }
};


HumanBeing Me , MJ;

Me.dance();  ///bad dance
MJ.dance();  //good dance
+5  A: 

You're describing a scenario where you might employ a Strategy Pattern, perhaps in your case having multiple implementations of a "dance" strategy which can be attached to a person at runtime.

HumanBeing Me(new BadDancer);
HumanBeing MJ(new GoodDancer);
Paul Dixon
I can see it now 'private Dancer tinaTurner;'
akf
you made me groan :)
Paul Dixon
+1  A: 

You should read Dealing with roles by Martin Fowler and make the right decision. Each solution has pro and cons.

rodrigoap