class Bass
{
public:
virtualint getPrice()
{
return price;
}
protected:
int price;
}
class BaseA : Base
{
public:
BaseA(int t) : Base(t);
int getPrice()
{
Base::getPrice();
}
}
class BaseB : BaseA
{
BaseB() : BaseA(9) // here price is set to 9, will objective is to change the price of Base
int getPrice()
{
BaseA::getPrice();
}
}
class BaseC : BaseB
{
BaseC(BaseB * _b)
{
b = _b;
}
int getPrice()
{
return b->getPrice()+ 3; // objective is to update and not change the price wi
}
private:
BaseB* b;
}
}
Assuming everything else is declared correctly.
Thank you
Why does the "decorator" (aka "adapter") inherit from the most derived class?
Lets say you have a type Window and your code creates and uses a window. Fine.
Then you invent a shiny window. Simple: inherit ShinyWindow from Window, specialize the shininess, and create a ShinyWindow instead of Window. Fine, the code still uses the object as a window like before. You can keep adding features via inheritance, but you have to replace the object creation code every time.
What if creation is inmutable? Lets create an another object that looks like a window (with a twist). Whatever you do that that object is actually done to the real window object (which might be shiny), but our adapter twists some operations. Rather than replacing an object, we have added a second object that "decorates" the first (assuming that we use the decorator as the window instead of the original object).
One can still upgrade the original object via normal inheritance, even though it is used by (possibly many) decorator objects.
ow.I got it.Apparently I was suppose to make a new decorator class. Thanx alot though.Ow, and Decorator is similar to Composite and not Adapter I think.
But thanks though.
> Decorator is similar to Composite and not Adapter I think.
Decorator is similar to the Proxy pattern; in both, the wrapper exposes the same interface as that of the wrapped object.
The interface of the decorated object is identical to the object that it decorates; The interface of the proxy object is identical to the object that it proxies for.
Adapter, Bridge and Facade are similar; in all three the interface that is exposed is different from the interface of the object that is adapted.