vendredi 8 juillet 2016

Which design pattern is good to acheive run tiime polymorphism in this case?

Following is the hierarchy to consider

                               Device
                                  ^
                                  |
                                  |
                            -------------
                            |           |
                            |           |
                       Television   AirConditioner

we have another class Remote to control the Devices.

Approach 1

class Remote 
{    
     Device* d; 
     public:
     Remote(Device* d){
        this->d = d;
     }

     void switchOn(){ 
          d->on();
     }
     //other methods 
};


//Simple classes for concept only.

class Device
{
    public: 
       virtual void on() = 0;
};
class Television : public Device
{
    public:
       void on(){
           cout << "television is turned on";
       }
       //other methods.
};
class AirConditioner : public Device
{
    public:
        void on(){
            cout << "Ac is turned on";
        }
};

int main(){

    Device *tv = new Television();
    Device *ac = new AirConditioner();

    Remote TVremote(tv); //assigning tv to the TVremote.
    Remote ACremote(ac); //assigning ac to the ACremote.
    TVremote->switchOn();
    ACremote->switchOn();

    return 0;
 }

The above program is self explanatory, where we are taking the advantage of run time polymorphism.

In the above program we bind the Television and AirConditioner object to Remote through Device* that is we are saying Remote has-a Device associated with it.

Approach 2

This is the approach which came to my mind at first step that is

Device has-a Remote So, this leads to following

class Remote 
{    

     public:
     void switchOn(Device* d){ 
         d->on();
     }
     //other methods 
};


//Simple classes for concept only.

class Device
{
    private:
       Remote r;

    public: 
        Device(Remote* r){

              this->r = *r;
        }

       virtual void on() = 0;
};
class Television : public Device
{
    public:

       Television(Remote* r): Device(r){}
       void on(){
           cout << "television is turned on";
       }
       //other methods.
};
class AirConditioner : public Device
{
    public:
        AirConditioner(Remote* r): Device(r){}
        void on(){
            cout << "Ac is turned on";
        }
};

int main()
{
     Remote TvRemote;
     Remote AcRemote;

     Television* tv = new Television(TvRemote);
     AirConditioner* ac = new AirConditioner(AcRemote);

     TvRemote.switchOn(tv); //now we have to pass tv to the method although 
                            // Remote is of tv
     AcRemote.switchOn(ac); // same as above.


     return 0;
}

So I have following questions

Q 1 When we have to program the above scenario the first thing which comes to human brain is Television has-a Remote (which is Approach 2) but when we implement it we need to pass Remote to the Device in the Device Constructor and also Device to the switchOn() method of the Remote, so we are unable to use the attachment of Remote and Device in the Device Constructor. How to get away with it?

So, what needs to be done here? Is approach 2 is better than the Approach 1? If yes, then what is the solution of above problem? If no then how do I satisfy myself that approach 1 is better?

I personally think (correct me if my view point is wrong) that Television has-a Remote is much more appealing than the other Approach.

Please help in deciding which approach is good?

EDIT One device is to be controlled with one Remote.

Aucun commentaire:

Enregistrer un commentaire