You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chapters/design_patterns/builder.textile
+16-10Lines changed: 16 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,11 +6,13 @@ chapter: Design Patterns
6
6
7
7
h2. Problem
8
8
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.
10
10
11
11
h2. Solution
12
12
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:
workBuilder = new TodoTxtBuilder(date: "10/13/2011", contexts: ["work"])
40
42
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"]
42
44
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'
44
46
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'
46
48
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'
48
50
49
51
{% endhighlight %}
50
52
51
53
h2. Discussion
52
54
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.
54
56
55
57
h3. Pre-Construction
56
58
@@ -82,3 +84,7 @@ builder.newTodo "Fill gas tank"
82
84
# => '2011-10-13 Fill gas tank +summerVacation'
83
85
{% endhighlight %}
84
86
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.
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 miniMarkdownand 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.
61
70
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:
Copy file name to clipboardExpand all lines: chapters/design_patterns/strategy.textile
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ chapter: Design Patterns
6
6
7
7
h2. Problem
8
8
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.
10
10
11
11
h2. Solution
12
12
@@ -69,7 +69,7 @@ sorter.sort unsortedList
69
69
70
70
h2. Discussion
71
71
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.
0 commit comments