Skip to content

Commit f573233

Browse files
committed
Tweak the Decorator Discussion to better explain the utility value of the pattern.
Delete an unintentionally-tracked temporary file. Rename the Decorator functions to use more function-like capitalization.
1 parent 5189199 commit f573233

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed
-12 KB
Binary file not shown.

chapters/design_patterns/decorator.textile

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ h2. Solution
1414
Use the Decorator pattern to structure the application of changes.
1515

1616
{% highlight coffeescript %}
17-
MiniMarkdown = (line) ->
17+
miniMarkdown = (line) ->
1818
if match = line.match /^(#+)\s*(.*)$/
1919
headerLevel = match[1].length
2020
headerText = match[2]
@@ -25,7 +25,7 @@ MiniMarkdown = (line) ->
2525
else
2626
''
2727

28-
StripComments = (line) ->
28+
stripComments = (line) ->
2929
line.replace /\s*\/\/.*$/, ''
3030

3131
TextProcessor = (processors) ->
@@ -48,7 +48,7 @@ exampleText = '''
4848
A line // with a comment
4949
'''
5050

51-
processor = new TextProcessor [StripComments, MiniMarkdown]
51+
processor = new TextProcessor [stripComments, miniMarkdown]
5252

5353
processor.processString exampleText
5454

@@ -57,9 +57,9 @@ processor.processString exampleText
5757

5858
h2. Discussion
5959

60-
With the TextProcessor object ready to wrangle all of the text processors together, the miniMarkdown, stripComments, and any future functions can focus exclusively on handling nothing but a single line of text. Future developers only have to write functions that return a string and add it to the array of processors in order to partake in the process.
60+
The TextProcessor serves the role of Decorator and binds the individual, specialized text processors together. This frees up the miniMarkdown, stripComments, and any future components to focus on handling nothing but a single line of text. Future developers only have to write functions that return a string and add it to the array of processors.
6161

62-
We can even modify the existing Decorator object on the fly:
62+
We can even modify the existing Decorator object on the fly. We could add a processor for transforming text smilies into images, for example, but only when the end user enables the feature:
6363

6464
{% highlight coffeescript %}
6565
smilies =
@@ -70,12 +70,16 @@ smilies =
7070

7171
smilieExpander = (line) ->
7272
if line
73-
(line = line.replace symbol, "{#{text}}") for symbol, text of smilies
73+
(line = line.replace symbol, "<img src='#{text}.png' alt='#{text}' />") for symbol, text of smilies
7474
line
7575

7676
processor.processors.unshift smilieExpander
7777

7878
processor.processString "# A header that makes you :) // you may even laugh"
7979

80-
# => '<h1>A header that makes you {smile}</h1>'
80+
# => "<h1>A header that makes you <img src='smile.png' alt='smile' /></h1>"
81+
82+
processor.processors.shift()
83+
84+
# => "<h1>A header that makes you :)</h1>"
8185
{% endhighlight %}

0 commit comments

Comments
 (0)