Decorator Design Pattern �(Structural)
Thanh Le – Software Architect / Technical Consultant
Complexity: 2/3
Popularity: 2/3
Intent
Image source: internet
Motivation
Imagine that you’re working on a notification library which lets other programs notify their users about important events.
The initial version of the library was based on the Notifier class that had only a few fields, a constructor and a single send method. The method could accept a message argument from a client and send the message to a list of emails that were passed to the notifier via its constructor. ����
Image source: internet
�
A program could use the notifier class to send notifications about important events to a predefined set of emails.
Motivation (cont)
One day, users expect more than just email notifications. Many of them would like to receive an SMS about critical issues. Others would like to be notified on Facebook and, of course, the corporate users would love to get Slack notifications.
Besides, they also want to send several notification types at once.
What should we do?
Image source: internet
�
Each notification type is implemented as a notifier’s subclass.
Solution
�
�
Image source: internet
Various notification methods become decorators.
Structure
Image source: internet
��
�
Pros/Cons
Practice
https://github.com/thanhle0212/23GoF-Design-Patterns-CSharp