Adding this to
the existing code logic in the Monitor increases its complexity,
whereas simply pulling out another Strategy object does not.
Remember, we??™re trying to hold ourselves to a new standard:
Whatever you do, don??™t make things worse.
??? What if, at a later date, we sometimes need to send the message to
the display and the pager, or the pager and the log, or some other
combination? As I will demonstrate later, object-oriented approaches
are quite evolve-able, and here we could easily migrate this design
to a Decorator pattern, if we adhere to a few simple practices. More
on this later.
One Among Many
Another circumstance we often find ourselves in is the need to pick one
behavior from a set of possibilities. Often, we base this decision on some
external value or condition, and in the procedural tradition, would use a
switch/case statement to organize the behavior, passing in the value or
condition, and make the proper selection, as shown in Listing 4.5.
Listing 4.5 Incorporating a Switch/Case to Organize Behavior (Pseudo-Code)
do some non-optional stuff
switch (some external value or condition)
case 1:
do option one
break
case 2:
do option two
break
case 3:
do option three
break
endcase
do some more non-optional stuff
66 Chapter 4 ??? Evolution in Code: Stage 1
Sometimes, we could also end the stack of cases with a default case, one
that ???fires??? if none of the other cases apply.
Pages:
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133