Decorator Design Pattern

Decorator is a structural design pattern that allows you to attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.

Extending a class is the first thing that comes to mind when you need to modify the behavior of an object. However, inheritance has several important caveats that you should be aware of. Inheritance is static. You cannot modify the behavior of an existing object at run time. You can only replace the entire object with one created from a different subclass. Subclasses can have only one main class. In most languages, inheritance does not allow a class to inherit behaviors from multiple classes at the same time.

One of the ways to overcome these caveats is through aggregation or composition rather than inheritance. Both alternatives work in much the same way: one object has a reference to another and delegates some work to it, whereas with inheritance, the object itself can do that work, inheriting the behavior of its superclass.

Lets look at the examples I created as below.

Sample PHP

Sample C#

By Yuuma.




tel. 06-6454-8833(平日 10:00~17:00)