According to Wiki https://en.wikipedia.org/wiki/Decorator_pattern#Usage
A decorator makes it possible to add or alter behavior of an interface at run-time. Alternatively, the adapter can be used when the wrapper must respect a particular interface and must support polymorphic behavior, and the Facade when an easier or simpler interface to an underlying object is desired.
I'm not getting it about polymorphic behavior
.
Where's polymorphic happened in Adaptor, isn't adaptor just transform one interface into another interface ?
Yes.
Say you have two classes that do vaguely similar things but have different interfaces. You can wrap one with an adapter so its interface matches the other's interface (or give them both adapters to some shared API), then the same client code can use either one polymorphically.
For example, two similar classes:
An adapter so one (arbitrarily, the
Bicycle
) can be controlled using the same interface as the other:That allows the client code that treats them polymorphically:
Say you wanted to use runtime polymorphism (i.e. virtual dispatch) rather than compile time polymorphism: you would have either derived the
Car
and adapter from a base class withvirtual bool start();
andvirtual bool accelerate_gently_to(double kph);
, or create a second adapter forCar
and have both adapters derive from such a base class, then you can dispatch polymorphically from non-templated code: