*This post may contain affiliate links. As an Amazon Associate we earn from qualifying purchases.
Decorator pattern is a type of structural design pattern such as Composite Pattern, Bridge Pattern, and Adapter Pattern just to name a few. Its main function is to use?abstract class or interface to implement with composition.
What are the Decorator Pattern Steps?
- First, make sure that the context meets all requirements such as a common-to-all interface, a couple of wrappers and optional embellishments, and a non-optional or single core component.
- To make all classes interchangeable, create a ?Lowest Common Denominator? interface.
- Also, you have to create a ?Decorator? which is a second level base class that supports the optional wrapper classes.
- It is worth mentioning that the Decorator and Core class inherit from the LCD interface.
- This is important because the Decorator class declares a composition connection to the LCD interface and it delegates to the LCD object.
- Then, for every optional embellishment, you have to define a Decorator derived class.
- These classes delegate to the Decorator base class and implement their wrapper functionality.
- Clients configure the ordering and type of Decorator and Core object.
How do you Change the Object’s Behavior?
Whenever you want to change the function of an object at run-time, you have to use decorator pattern. Thus, the modified object experiences a behavior change as well.
However, besides composition, we can also use inheritance to change an object?s behavior, but even if it can be applied to all class instances, you can use inheritance only at compile time.
Decorative patterns are used when we want to add a new remove function to any existing run-time behavior.
For instance, you can develop a Car interface to design the assembly technique. Only then can you create a Basic car and if you want to extend the range, you can also add other features including Sports or Luxury Car.
However, the implementation becomes quite complicated and complex in this case, especially when users will have to establish the car features. Supposing we have ten type of cars, it will be impossible for us to manage the implementation logic by making use of composition and inheritance.
Fortunately, we can develop this?programming function with the help of Java decorator pattern.
Which are the Thumb Rules of the Decorator Pattern?
- A distinct interface is provided by the adapter to its subject. Proxy provides that interface as well, whereas the Decorator provides an improved interface.
- Then, the Decorator expands the responsibilities of the object, while the adapter changes the interface of this object. Therefore, the client sees a more transparent Decorator. In addition to this, the Decorator sustains recursive composition (pure Adapters can?t do this).
- The structure diagrams are very much alike in Decorator and Composite, meaning that both of them depend on recursive composition to establish a number of open-ended objects.
- You may think of a Decorator as a degenerate Composite that has just a component. Even so, it adds a few more responsibilities to aggregate the object.
- Furthermore, a Decorator is focused on helping the user to add responsibilities to objects, whereas a Composite focuses on representation rather than embellishment.
- To allow components access global properties, a Composite can use Chain of Responsibility. However, to override these properties, it can also use a Decorator.
- On the other hand, even if we use Proxy and Decorator for different functions, they have almost twin structures.
- However, a Decorator allows you to change an object?s skin.
Image Source:?Safari Books Online