I'd like to use the Decorator Pattern in C++ and still be able to rely on the signature/identity of the decorated object. Is it possible to do it in C++?
That is, I'd like to decorate a component:
class Component {
public:
Component();
virtual void doSomething();
}
with a decorator:
class Decorator : public Component {
public:
Decorator(Component*);
virtual void doSomething();
private:
Component* _component;
}
such that when I do:
Component foo;
Decorator(&foo) bar;
std::cout << typeid(bar).name() << std::endl;
it prints "Component" instead of "Decorator".
(This is actually pretty simple to do in Python using the decorator module but I'm learning C++ at the moment and don't even know where to start looking for an answer to this question.)
This is useful in case I want to extend the Component class but still be able to use it in a transparent way (as if it wouldn't have been extended):
class ExtendDecorator : public Decorator {
public:
ExtendDecorator(Component*);
virtual void doSomething();
private:
void doSomethingMore();
}
void ExtendDecorator::doSomething() {
Decorator::doSomething();
doSomethingMore();
}