Skip to content

Commit 50829fe

Browse files
committed
Editorial changes and clarifications.
1 parent 085fc44 commit 50829fe

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

chapters/design_patterns/builder.textile

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ chapter: Design Patterns
66

77
h2. Problem
88

9-
You need to prepare a complicated, multi-part object, more than once or with varying configurations.
9+
You need to prepare a complicated, multi-part object, but you expect to do it more than once or with varying configurations.
1010

1111
h2. Solution
1212

13-
Create a Builder to encapsulate the production process.
13+
Create a Builder to encapsulate the object production process.
14+
15+
The <a href="http://todotxt.com">Todo.txt</a> format provides an advanced but still plain-text method for maintaining lists of to-do items. Typing out each item by hand would provide exhausting and error-prone, however, so a TodoTxtBuilder class could save us the trouble:
1416

1517
{% highlight coffeescript %}
1618
TodoTxtBuilder = (defaultParameters={ }) ->
@@ -24,9 +26,9 @@ TodoTxtBuilder = (defaultParameters={ }) ->
2426
projects = this.projects.concat(parameters.projects or [ ])
2527
priorityLevel = parameters.priority or this.priority
2628
createdAt = [date.getFullYear(), date.getMonth()+1, date.getDate()].join("-")
27-
contextNames = ("@" + context for context in contexts).join(" ")
28-
projectNames = ("+" + project for project in projects).join(" ")
29-
priority = if priorityLevel then "(" + priorityLevel + ")" else ""
29+
contextNames = ("@#{context}" for context in contexts).join(" ")
30+
projectNames = ("+#{project}" for project in projects).join(" ")
31+
priority = if priorityLevel then "(#{priorityLevel})" else ""
3032
[priority, createdAt, description, contextNames, projectNames].reduce (whole, part) ->
3133
if part then (whole and whole + " ") + part else whole
3234

@@ -38,19 +40,19 @@ builder.newTodo "Wash laundry"
3840

3941
workBuilder = new TodoTxtBuilder(date: "10/13/2011", contexts: ["work"])
4042

41-
workBuilder.newTodo "Show the new design pattern to Sean", contexts: ["desk", "xpSession"]
43+
workBuilder.newTodo "Show the new design pattern to Lucy", contexts: ["desk", "xpSession"]
4244

43-
# => '2011-10-13 Show the new design pattern to Sean @work @desk @xpSession'
45+
# => '2011-10-13 Show the new design pattern to Lucy @work @desk @xpSession'
4446

45-
workBuilder.newTodo "Remind Lucy about the failing unit tests", contexts: ["meeting"], projects: ["compilerRefactor"], priority: 'A'
47+
workBuilder.newTodo "Remind Sean about the failing unit tests", contexts: ["meeting"], projects: ["compilerRefactor"], priority: 'A'
4648

47-
# => '(A) 2011-10-13 Remind Lucy about the failing unit tests @work @meeting +compilerRefactor'
49+
# => '(A) 2011-10-13 Remind Sean about the failing unit tests @work @meeting +compilerRefactor'
4850

4951
{% endhighlight %}
5052

5153
h2. Discussion
5254

53-
Based on the <a href="http://todotxt.com">Todo.txt</a> format, the TodoTxtBuilder class takes care of all the heavy lifting of text generation and lets the programmer focus on the unique elements of each todo item. A command line tool tool or GUI could plug into this code and retain support for later, more advanced versions of the format with ease.
55+
The TodoTxtBuilder class takes care of all the heavy lifting of text generation and lets the programmer focus on the unique elements of each to-do item. Additionally, a command line tool or GUI could plug into this code and still retain support for later, more advanced versions of the format with ease.
5456

5557
h3. Pre-Construction
5658

@@ -82,3 +84,7 @@ builder.newTodo "Fill gas tank"
8284
# => '2011-10-13 Fill gas tank +summerVacation'
8385
{% endhighlight %}
8486

87+
h3. Exercises
88+
* Expand the project- and context-tag generation code to filter out duplicate entries.
89+
** Some Todo.txt users like to insert project and context tags inside the description of their to-do items. Add code to identify these tags and filter them out of the end tags.
90+

chapters/design_patterns/decorator.textile

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ h2. Problem
88

99
You have a set of data that you need to process in multiple, possibly varying ways.
1010

11-
1211
h2. Solution
1312

14-
Use the Decorator pattern to structure the application of changes.
13+
Use the Decorator pattern in order to structure how you apply the changes.
1514

1615
{% highlight coffeescript %}
1716
miniMarkdown = (line) ->
@@ -26,7 +25,7 @@ miniMarkdown = (line) ->
2625
''
2726

2827
stripComments = (line) ->
29-
line.replace /\s*\/\/.*$/, ''
28+
line.replace /\s*\/\/.*$/, '' # Removes one-line, double-slash C-style comments
3029

3130
TextProcessor = (processors) ->
3231
processors: processors
@@ -55,11 +54,21 @@ processor.processString exampleText
5554
# => "<h1>A level 1 header</h1>\n<p>A regular line</p>\n\n<h2>A level 2 header</h2>\n<p>A line</p>"
5655
{% endhighlight %}
5756

57+
h3. Results
58+
59+
{% highlight html %}
60+
<h1>A level 1 header</h1>
61+
<p>A regular line</p>
62+
63+
<h2>A level 1 header</h2>
64+
<p>A line</p>
65+
{% endhighlight %}
66+
5867
h2. Discussion
5968

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.
69+
The TextProcessor serves the role of Decorator by binding the individual, specialized text processors together. This frees up the miniMarkdown and stripComments 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.
6170

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:
71+
We can even modify the existing Decorator object on the fly:
6372

6473
{% highlight coffeescript %}
6574
smilies =

chapters/design_patterns/strategy.textile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ chapter: Design Patterns
66

77
h2. Problem
88

9-
You have more than one way to solve a problem but you need to choose (or even switch) between them at run time.
9+
You have more than one way to solve a problem but you need to choose (or even switch) between these methods at run time.
1010

1111
h2. Solution
1212

@@ -69,7 +69,7 @@ sorter.sort unsortedList
6969

7070
h2. Discussion
7171

72-
Like a skilled general, we must prepare to change our plans under constantly-changing circumstances. At the end of the example, we know that only the newest item in the array is out of order. We can then speed the sort up by switching to an algorithm that starts from the end of the array.
72+
"No plan survives first contact with the enemy", nor users, but we can use the knowledge gained from changing circumstances to adapt. Near the end of the example, for instance, the newest item in the array now lies out of order. Knowing that detail, we can then speed the sort up by switching to an algorithm optimized for that exact scenario with nothing but a simple reassignment.
7373

7474
h3. Exercises
7575

wanted-recipes.textile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ h2. Design patterns
133133
** Adapter
134134
** Bridge
135135
** Composite
136-
** Decorator
137136
** Facade
138137
** Flyweight
139138
** Proxy

0 commit comments

Comments
 (0)