单例模式:


建造者模式:

示例:

  1. from enum import Enum
  2. import time
  3.  
  4. PizzaProgress = Enum('PizzaProgress', 'queued preparation baking ready')
  5. PizzaDough = Enum('PizzaDough', 'thin thick')
  6. PizzaSauce = Enum('PizzaSauce', 'tomato creme_fraiche')
  7. PizzaTopping = Enum('PizzaTopping', 'mozzarella double_mozzarella bacon ham mushrooms red_onion oregano')
  8.  
  9. STEP_DELAY = 3
  10.  
  11. class Pizza:
  12. def __init__(self, name):
  13. self.name = name
  14. self.dough = None
  15. self.sauce = None
  16. self.topping = []
  17.  
  18. def __str__(self):
  19. return self.name
  20.  
  21. def prepare_dough(self, dough):
  22. self.dough = dough
  23. print('preparing the {} dough of your {}....'.format(self.dough.name, self))
  24. time.sleep(STEP_DELAY)
  25. print('done with the {} dough'.format(self.dough.name))
  26.  
  27. class MargaritaBuilder:
  28. def __init__(self):
  29. self.pizza = Pizza('margarita')
  30. self.progress = PizzaProgress.queued
  31. self.baking_time = 5
  32.  
  33. def prepare_dough(self):
  34. self.progress = PizzaProgress.preparation
  35. self.pizza.prepare_dough(PizzaDough.thin)
  36.  
  37. def add_sauce(self):
  38. print('adding the tomato sauce to your margarita....')
  39. self.pizza.sauce = PizzaSauce.tomato
  40. time.sleep(STEP_DELAY)
  41. print('done with the tomato sauce')
  42.  
  43. def add_topping(self):
  44. print('adding the topping (double mozzarella,oregano) to your margarita')
  45. self.pizza.topping.append([i for i in (PizzaTopping.double_mozzarella, PizzaTopping.oregano)])
  46. time.sleep(STEP_DELAY)
  47. print('done with the topping (double mozzarella, oregano)')
  48.  
  49. def bake(self):
  50. self.progress = PizzaProgress.baking
  51. print('baking your margarita for {} seconds'.format(self.baking_time))
  52. time.sleep(self.baking_time)
  53. self.progress = PizzaProgress.ready
  54. print('your margarita is ready')
  55.  
  56. class CreamyBaconBuilder:
  57. def __init__(self):
  58. self.pizza = Pizza('creamy bacon')
  59. self.progress = PizzaProgress.queued
  60. self.baking_time = 7
  61.  
  62. def prepare_dough(self):
  63. self.progress = PizzaProgress.preparation
  64. self.pizza.prepare_dough(PizzaDough.thick)
  65.  
  66. def add_sauce(self):
  67. print('adding the creme fraiche sauce to your creamy bacon')
  68. self.pizza.sauce = PizzaSauce.creme_fraiche
  69. time.sleep(STEP_DELAY)
  70. print('done with the creme fraiche sauce')
  71.  
  72. def add_topping(self):
  73. print('adding the topping (mozzarella, bacon, ham, mushrooms,red onion, oregano) to your creamy bacon')
  74. self.pizza.topping.append([t for t in (
  75. PizzaTopping.mozzarella, PizzaTopping.bacon, PizzaTopping.ham, PizzaTopping.mushrooms,
  76. PizzaTopping.red_onion,
  77. PizzaTopping.oregano)])
  78. time.sleep(STEP_DELAY)
  79. print('done with the topping (mozzarella, bacon, ham,mushrooms, red onion, oregano)')
  80.  
  81. def bake(self):
  82. self.progress = PizzaProgress.baking
  83. print('baking your creamy bacon for {} seconds'.format(self.baking_time))
  84. time.sleep(self.baking_time)
  85. self.progress = PizzaProgress.ready
  86. print('your creamy bacon is ready')
  87.  
  88. class Waiter:
  89. def __init__(self):
  90. self.builder = None
  91.  
  92. def construct_pizza(self, builder):
  93. self.builder = builder
  94. [step() for step in (builder.prepare_dough, builder.add_sauce, builder.add_topping, builder.bake)]
  95.  
  96. @property
  97. def pizza(self):
  98. return self.builder.pizza
  99.  
  100. def validate_style(builders):
  101. try:
  102. pizza_style = input('what pizza would you like, [m]argarita or [c]reamy bacon?')
  103. builder = builders[pizza_style]()
  104. valid_input = True
  105. except KeyError as err:
  106. print('Sorry, only margarita (key m) and creamy bacon (key c) are available')
  107. return (False, None)
  108. return (True, builder)
  109.  
  110. def main():
  111. builders = dict(m=MargaritaBuilder, c=CreamyBaconBuilder)
  112. valid_input = False
  113. while not valid_input:
  114. valid_input, builder = validate_style(builders)
  115. print()
  116. waiter = Waiter()
  117. # waiter.construct_pizza(builder)
  118. waiter.construct_pizza(builder)
  119. pizza = waiter.pizza
  120. print()
  121. print('Enjoy your {}!'.format(pizza))
  122.  
  123. if __name__ == '__main__':
  124. main()
  125.  
  126. 结果一:
  127.  
  128. what pizza would you like, [m]argarita or [c]reamy bacon?c
  129.  
  130. preparing the thick dough of your creamy bacon....
  131. done with the thick dough
  132. adding the creme fraiche sauce to your creamy bacon
  133. done with the creme fraiche sauce
  134. adding the topping (mozzarella, bacon, ham, mushrooms,red onion, oregano) to your creamy bacon
  135. done with the topping (mozzarella, bacon, ham,mushrooms, red onion, oregano)
  136. baking your creamy bacon for 7 seconds
  137. your creamy bacon is ready
  138.  
  139. Enjoy your creamy bacon!
  140.  
  141. 结果二:
  142.  
  143. what pizza would you like, [m]argarita or [c]reamy bacon?m
  144.  
  145. preparing the thin dough of your margarita....
  146. done with the thin dough
  147. adding the tomato sauce to your margarita....
  148. done with the tomato sauce
  149. adding the topping (double mozzarella,oregano) to your margarita
  150. done with the topping (double mozzarella, oregano)
  151. baking your margarita for 5 seconds
  152. your margarita is ready
  153.  
  154. Enjoy your margarita!

建造pizza示例

Django设计模式的更多相关文章

  1. 6.-Django设计模式及模版层

    一.MVC (java等其他语言) MVC代表Model-view-Contorller(模型-视图-控制器)模式 M模型层主要用于对数据库层的封装 V视图层用于向用户展示结果 C控制器用于处理请求. ...

  2. Django 学习笔记(一) --- Hello Django

    人生苦短 ~ Tips:仅适用于 Python 3+(反正差别不大,py2 改改也能用).因为据 Python 之父 Guido van Rossum 说会在 2020 年停止对 Python 2 的 ...

  3. Django简介(MVC、MTV)

    Django简介 MVC Model(模型)- 应用程序中处理数据逻辑部分且与数据库交互,用于存取数据的部分 View(视图)- 用于处理后的数据界面展示,且视图通常是由模型数据创建的,是用户看到并与 ...

  4. django 的基础设计

    一.web程序工作流程 二.django  的基础介绍 目的:了解Django框架的作用和特点 作用: 简便.快速的开发数据库驱动的网站 Django的优势 快速开发 MVT 功能齐全 Django学 ...

  5. django基础回顾

    1,web项目工作流程 1.1 了解web程序工作流程 1.2 django生命周期2,django介绍 目的:了解Django框架的作用和特点 作用: 简便.快速的开发数据库驱动的网站 Django ...

  6. django基本内容

    1,流程 1.1 了解web程序工作流程 1.2 django生命周期 2,django介绍 ​ 目的:了解Django框架的作用和特点 ​ 作用: 简便.快速的开发数据库驱动的网站 django的优 ...

  7. day1(Django)

    1,web项目工作流程 1.1 了解web程序工作流程 1.2 django生命周期   2,django介绍 目的:了解Django框架的作用和特点作用: 简便.快速的开发数据库驱动的网站 Djan ...

  8. Python资源大全

    The Python Tutorial (Python 2.7.11) 的中文翻译版本.Python Tutorial 为初学 Python 必备官方教程,本教程适用于 Python 2.7.X 系列 ...

  9. 如何系统地自学 Python?

    最近开始系统的学习Python,以及整理的一些资料.github记录着个人自学 Python 的过程,持续更新.欢迎大家一起来完善这个自学Python学习的项目,给后来者一个参考的学习过程.githu ...

随机推荐

  1. word2vec模型评估方案

    1.word2vec参数详解 · sentences:可以是一个·ist,对于大语料集,建议使用BrownCorpus,Text8Corpus或·ineSentence构建.· sg: 用于设置训练算 ...

  2. NO Route to Host 连接mysql数据库

    显然是请求被服务器的防火墙给拦截了 1,vi /etc/sysconfig/iptables 2,在倒数第三行以前添加 -A INPUT -p tcp -m state --state NEW -m  ...

  3. 如何用Win7远程链接ubuntu14.04桌面

    如何用Win7远程链接ubuntu14.04桌面 采用vnc技术 参考:http://blog.csdn.net/hnjztyx/article/details/69739137

  4. 如何比sketch和axure更方便地给原型做交互?

    在快速的工作环境中,我们现在都希望在工作的各个环节中提高效率.有些产品设计师们做产品原型时,会感觉sketch或者axure添加交互的方式不够快捷.下面就提供一种解决方案. 使用工具:墨刀. 交互链接 ...

  5. js动态规划---背包问题

    //每种物品仅有一件,可以选择放或不放 //即f[i][w]表示前i件物品恰放入一个容量为w的背包可以获得的最大价值. //则其状态转移方程便是:f[i][w]=max{f[i-1][w],f[i-1 ...

  6. 机器学习算法的调试---梯度检验(Gradient Checking)

    梯度检验是一种对求导结果进行数值检验的方法,该方法可以验证求导代码是否正确. 1. 数学原理   考虑我们想要最小化以 θ 为自变量的目标函数 J(θ)(θ 可以为标量和可以为矢量,在 Numpy 的 ...

  7. [洛谷]p1996约瑟夫环 &xdoj1311

    https://www.luogu.org/problemnew/show/P1996 约瑟夫环这个问题一直以来都是用循环链表写的,今天才知道有循环队列的写法.以下是要点: 1.循环队列实现环的思想, ...

  8. shell基础:多命令顺序执行与管道符

    有些命令的前后关系正是需要这样的关系来实现. 如在软件包的安装中: 第三个是个简单的判断:

  9. mint-ui Picker的使用

    <template> <div v-bind:style="{minHeight:clientHeight + 'px'}" id="recive-mi ...

  10. NetSpeed

    NetSpeed公司提供的NOC包括三部分,可以通过NocStudio进行配置生成. 1)NetSpeed Orion,面向快速SoC design的可综合平台. 2)Linley NetSpeed ...