课程表

CoffeeScript 语法

CoffeeScript 类和对象

CoffeeScript 字符串

CoffeeScript 数组

CoffeeScript 日期和时间

CoffeeScript 数学

CoffeeScript 方法

CoffeeScript 元编程

CoffeeScript jQuery

CoffeeScript 正则表达式

CoffeeScript 网络

CoffeeScript 设计模式

CoffeeScript 数据库

CoffeeScript 测试

工具箱
速查手册

CoffeeScript 生成器模式

当前位置:免费教程 » JS/JS库/框架 » CoffeeScript

问题

你需要准备一个复杂的、多部分的对象,你希望操作不止一次或有不同的配置。

解决方案

创建一个生成器封装对象的产生过程。

Todo.txt 格式提供了一个先进的但还是纯文本的方法来维护待办事项列表。手工输入每个项目有损耗且容易出错,然而 TodoTxtBuilder 类可以解决我们的麻烦:

  1. class TodoTxtBuilder
  2. constructor: (defaultParameters={ }) ->
  3. @date = new Date(defaultParameters.date) or new Date
  4. @contexts = defaultParameters.contexts or [ ]
  5. @projects = defaultParameters.projects or [ ]
  6. @priority = defaultParameters.priority or undefined
  7. newTodo: (description, parameters={ }) ->
  8. date = (parameters.date and new Date(parameters.date)) or @date
  9. contexts = @contexts.concat(parameters.contexts or [ ])
  10. projects = @projects.concat(parameters.projects or [ ])
  11. priorityLevel = parameters.priority or @priority
  12. createdAt = [date.getFullYear(), date.getMonth()+1, date.getDate()].join("-")
  13. contextNames = ("@#{context}" for context in contexts when context).join(" ")
  14. projectNames = ("+#{project}" for project in projects when project).join(" ")
  15. priority = if priorityLevel then "(#{priorityLevel})" else ""
  16. todoParts = [priority, createdAt, description, contextNames, projectNames]
  17. (part for part in todoParts when part.length > 0).join " "
  18. builder = new TodoTxtBuilder(date: "10/13/2011")
  19. builder.newTodo "Wash laundry"
  20. # => '2011-10-13 Wash laundry'
  21. workBuilder = new TodoTxtBuilder(date: "10/13/2011", contexts: ["work"])
  22. workBuilder.newTodo "Show the new design pattern to Lucy", contexts: ["desk", "xpSession"]
  23. # => '2011-10-13 Show the new design pattern to Lucy @work @desk @xpSession'
  24. workBuilder.newTodo "Remind Sean about the failing unit tests", contexts: ["meeting"], projects: ["compilerRefactor"], priority: 'A'
  25. # => '(A) 2011-10-13 Remind Sean about the failing unit tests @work @meeting +compilerRefactor'

讨论

TodoTxtBuilder 类负责所有文本的生成,让程序员关注每个工作项的独特元素。此外,命令行工具或 GUI 可以插入这个代码且之后仍然保持支持,提供轻松、更高版本的格式。

前期建设

并不是每次创建一个新的实例所需的对象都要从头开始,我们将负担转移到一个单独的对象,可以在对象创建过程中进行调整。

  1. builder = new TodoTxtBuilder(date: "10/13/2011")
  2. builder.newTodo "Order new netbook"
  3. # => '2011-10-13 Order new netbook'
  4. builder.projects.push "summerVacation"
  5. builder.newTodo "Buy suntan lotion"
  6. # => '2011-10-13 Buy suntan lotion +summerVacation'
  7. builder.contexts.push "phone"
  8. builder.newTodo "Order tickets"
  9. # => '2011-10-13 Order tickets @phone +summerVacation'
  10. delete builder.contexts[0]
  11. builder.newTodo "Fill gas tank"
  12. # => '2011-10-13 Fill gas tank +summerVacation'

练习

  • 扩大 project- 和 context-tag 生成代码来过滤掉重复的条目。

  • 一些 Todo.txt 用户喜欢在任务描述中插入项目和上下文的标签。添加代码来识别这些标签和过滤器的结束标记。
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号