|
| 1 | +--- |
| 2 | +layout: recipe |
| 3 | +title: Builder Pattern |
| 4 | +chapter: Design Patterns |
| 5 | +--- |
| 6 | + |
| 7 | +h2. Problem |
| 8 | + |
| 9 | +You need to prepare a complicated, multi-part object, but you expect to do it more than once or with varying configurations. |
| 10 | + |
| 11 | +h2. Solution |
| 12 | + |
| 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: |
| 16 | + |
| 17 | +{% highlight coffeescript %} |
| 18 | +class TodoTxtBuilder |
| 19 | + constructor: (defaultParameters={ }) -> |
| 20 | + @date = new Date(defaultParameters.date) or new Date |
| 21 | + @contexts = defaultParameters.contexts or [ ] |
| 22 | + @projects = defaultParameters.projects or [ ] |
| 23 | + @priority = defaultParameters.priority or undefined |
| 24 | + newTodo: (description, parameters={ }) -> |
| 25 | + date = (parameters.date and new Date(parameters.date)) or @date |
| 26 | + contexts = @contexts.concat(parameters.contexts or [ ]) |
| 27 | + projects = @projects.concat(parameters.projects or [ ]) |
| 28 | + priorityLevel = parameters.priority or @priority |
| 29 | + createdAt = [date.getFullYear(), date.getMonth()+1, date.getDate()].join("-") |
| 30 | + contextNames = ("@#{context}" for context in contexts when context).join(" ") |
| 31 | + projectNames = ("+#{project}" for project in projects when project).join(" ") |
| 32 | + priority = if priorityLevel then "(#{priorityLevel})" else "" |
| 33 | + todoParts = [priority, createdAt, description, contextNames, projectNames] |
| 34 | + (part for part in todoParts when part.length > 0).join " " |
| 35 | + |
| 36 | +builder = new TodoTxtBuilder(date: "10/13/2011") |
| 37 | + |
| 38 | +builder.newTodo "Wash laundry" |
| 39 | + |
| 40 | +# => '2011-10-13 Wash laundry' |
| 41 | + |
| 42 | +workBuilder = new TodoTxtBuilder(date: "10/13/2011", contexts: ["work"]) |
| 43 | + |
| 44 | +workBuilder.newTodo "Show the new design pattern to Lucy", contexts: ["desk", "xpSession"] |
| 45 | + |
| 46 | +# => '2011-10-13 Show the new design pattern to Lucy @work @desk @xpSession' |
| 47 | + |
| 48 | +workBuilder.newTodo "Remind Sean about the failing unit tests", contexts: ["meeting"], projects: ["compilerRefactor"], priority: 'A' |
| 49 | + |
| 50 | +# => '(A) 2011-10-13 Remind Sean about the failing unit tests @work @meeting +compilerRefactor' |
| 51 | + |
| 52 | +{% endhighlight %} |
| 53 | + |
| 54 | +h2. Discussion |
| 55 | + |
| 56 | +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. |
| 57 | + |
| 58 | +h3. Pre-Construction |
| 59 | + |
| 60 | +Instead of creating a new instance of the needed object from scratch every time, we shift the burden to a separate object that we can then tweak during the object creation process. |
| 61 | + |
| 62 | +{% highlight coffeescript %} |
| 63 | +builder = new TodoTxtBuilder(date: "10/13/2011") |
| 64 | + |
| 65 | +builder.newTodo "Order new netbook" |
| 66 | + |
| 67 | +# => '2011-10-13 Order new netbook' |
| 68 | + |
| 69 | +builder.projects.push "summerVacation" |
| 70 | + |
| 71 | +builder.newTodo "Buy suntan lotion" |
| 72 | + |
| 73 | +# => '2011-10-13 Buy suntan lotion +summerVacation' |
| 74 | + |
| 75 | +builder.contexts.push "phone" |
| 76 | + |
| 77 | +builder.newTodo "Order tickets" |
| 78 | + |
| 79 | +# => '2011-10-13 Order tickets @phone +summerVacation' |
| 80 | + |
| 81 | +delete builder.contexts[0] |
| 82 | + |
| 83 | +builder.newTodo "Fill gas tank" |
| 84 | + |
| 85 | +# => '2011-10-13 Fill gas tank +summerVacation' |
| 86 | +{% endhighlight %} |
| 87 | + |
| 88 | +h3. Exercises |
| 89 | +* Expand the project- and context-tag generation code to filter out duplicate entries. |
| 90 | +** 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. |
| 91 | + |
0 commit comments