So you might ask
what is the benefit of adding additional functionalities without inheritance.
Well, there certainly are some benefits. To extend an object, sometimes you need
to know many inner things of that class. Sometimes it's not possible to extend
the class without rewriting the existing functionalities. If you want to add the
same functionalities to many types of objects, it is much better to add them using
Decorator pattern instead of extending all of them individually. Otherwise it might
lead you to a horrible maintenance nightmare.
Decorator
php BB
Post
Emoticon
Visualizer
Visualizer
Let us go for a common scenario. For example, imagine that you are building a blog
or message board where all your posts and comments come as separate post and
comment objects. Both of these objects have a common method getContents()
which returns the filtered content of that post or comment.
Now your manager is asking to add functionalities to parse emoticon and BBCode of
those posts and comments. The core code is complex and you don't want to touch it
anymore. Here Decorator pattern comes to save your life.
Chapter 4
[ 89 ]
Let us see our Post and Comment object first.
Pages:
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108