structural pattern, it allows a behavior to be added to an object
The decorator implements the interface of the original class and forwards the requests from its own interface to that class, but it also performs additional actions before and after these forwarded requests — these are the decorations.
Here the knight, aided by his attack bonus and the enemy’s weak armor, will successfully hit the ogre. But the game is far from over. As the units fight, the surviving ones gain experience and eventually become veterans. A veteran unit is still the same kind of unit, but it gains attack and defense bonuses, reflecting its combat experience.
We do not want to change any of the class interfaces, but we want to modify the behavior of the attack() and defense() functions. This is the job of the decorator pattern, and what follows is the classic implementation of the VeteranUnit decorator:
the classic decorator pattern limitation
the lifetimes of these objects must be carefully managed
to C++
when designer have added a special ability to the Knight unit, but the VeteranUnit decorator do not know the added ability
can’t handle cross-casting well(casting to a type in another branch of the same hierarchy)
Decorators the C++ way
To solve the two problem, we indorduct the CRTP C++ idiom
This preservation of the interface is a fundamental feature of the Decorator pattern. It is also one of its most serious limitations.
adapter pattern
It is a sturctural pattern that allows an interface of a class to be used as another, different interface. It allows an existing class to be used in code that expects a different interface, without modifying the original class.
Adapter is very general, broad pattern. It can be used to implement several other, more narrowly defined patterns — in particular, the decorator.
Converting an object from its current interface to the interface needed by a particular application, without rewriting the object itself, is the purpose and the use of the Adapter pattern.