A sample decorator concerns the layout of an object. To inherit the type of this object, it must be inherited. Not all types are intended to be inherited, i.e. Designed for base classes, even if they can be inherited from a purely technical point of view (which I consider a design flaw).
The design of the decorator is based on the ability to change the behavior of objects without changing the objects themselves. By inheriting, you essentially modify the object itself, and what you get is a regular behavioral change through polymorphism, which means that you have not performed the same thing.
Thus, both decoration and inheritance use them. Use jewelry when one of them is right.
- you cannot inherit (e.g. if the class is
sealed in C #) - you should not inherit (the class is clearly not intended for the base class)
- you want to change the behavior of one specific object many times (by wrapping it with decorators of different behaviors)
Note that inheritance is the most powerful tool in the OO toolbar. With great power comes great responsibility and which is not always easy to handle. I would say: always make up or combine. When it is simply impossible to do, inherit. If you cannot inherit, try harder to compose or fill out. "
source share