lundi 7 mars 2016

How to design a specialised proxy class from a generic proxy.

Consider the following case of a proxy pattern. (Not sure this truly is a “proxy pattern”, but I’ll go with it).

I am given a class Dog and a proxy class DogProxy, which modify/extends a Dog's functionality and is a Dog itself

class DogProxy : public Dog

The proxy class has a public method

void DogProxy::setSource(Dog *)

which is called to “plug” the proxy onto a specific Dog object.

DogProxy is given and ready to use, and I am also given another (non proxy) subclass

class GermanShepard : public Dog. 

I need to implement a new proxy class which is specialised to be used use with and only with a GermanShepard.

Such GermanShepardProxy should have

void GermanShepardProxy::setSource(GermanShepard *)

instead of the generic setSource(Dog *), although this is not a constraint, it just seems to me to make sense.

I would like GermanShepardProxy to be implemented in terms of DogProxy, which already contains most of the functionality.

I could publicly inherit from DogProxy, but in doing so I have an inconsistency, because hiding DogProxy::setSource(Dog *) violates the principle that GermanShepardProxy is a DogProxy. I could use protected inheritance instead, but then GermanShepardProxy would not be a Dog anymore and therefore quite useless.

To summarize: given Dog, DogProxy and GermanShepard as above, how can I effectively design a GermanShepardProxy, which should be a Dog but not a DogProxy?

Aucun commentaire:

Enregistrer un commentaire