โ๏ธ ๐ด๐๐๐๐๐๐๐๐๐
Have you ever tried to add responsibility to an object dynamically? Decorator provides a flexible alternative to subclassing. It is known as wrapper.
๐ ๐น๐๐๐-๐พ๐๐๐๐
๐บ๐๐๐๐๐๐๐
Imagine, there is a graphical component that lets you add properties such as: border, scroll bar, and so forth. One way to implement it is using inheritance, but it is not flexible. Since, the border is added statically, and a client cannot control how and when to decorate a component with a border. How can we add the border in a flexible way?
๐ฏ ๐๐จ๐ฅ๐ฎ๐ญ๐ข๐จ๐ง โ ๐๐๐๐๐๐๐๐๐ ๐๐๐ญ๐ญ๐๐ซ๐ง
Decorator encloses the component in another object that adds the border. The decorator conforms to the interface of the component it decorates. So, its presence is transparent to clients. The decorator forwards the request to the component and it does some actions such as adding a boarder before or after forwarding.
๐ง ๐๐ก๐๐ง ๐ญ๐จ ๐๐ฌ๐
โ๏ธAdd a functionality to an object dynamically and transparently with zero effects for other objects.
โ๏ธ For functionalities which can be removed.
โ๏ธWhen we want to extend a functionality with subclassing, but it is impossible, due to the explosion of subclasses.
๐ ๐น๐๐๐๐๐๐
๐ท๐๐๐๐๐๐๐:
โพ ๐๐๐๐ฉ๐ญ๐๐ซ: the adapter grants an object a new interface, but the decorator changes the responsibilities of an object.
โพ๐๐จ๐ฆ๐ฉ๐จ๐ฌ๐ข๐ญ๐: we can consider the decorator as a decadent version of composite since it has only one component.
โพ๐๐ญ๐ซ๐๐ญ๐๐ ๐ฒ: the decorator changes the skin of an object while the strategy modifies mechanism.
๐ ๐๐จ๐๐ ๐๐ฑ๐๐ฆ๐ฉ๐ฅ๐
๐ GitHub - https://lnkd.in/dv_YvFTd
โHave you ever used the ๐๐๐๐๐๐๐๐๐ Pattern in your projects?
Top comments (0)