Django设计模式
单例模式:
建造者模式:
示例:
from enum import Enum
import time
PizzaProgress = Enum('PizzaProgress', 'queued preparation baking ready')
PizzaDough = Enum('PizzaDough', 'thin thick')
PizzaSauce = Enum('PizzaSauce', 'tomato creme_fraiche')
PizzaTopping = Enum('PizzaTopping', 'mozzarella double_mozzarella bacon ham mushrooms red_onion oregano')
STEP_DELAY = 3
class Pizza:
def __init__(self, name):
self.name = name
self.dough = None
self.sauce = None
self.topping = []
def __str__(self):
return self.name
def prepare_dough(self, dough):
self.dough = dough
print('preparing the {} dough of your {}....'.format(self.dough.name, self))
time.sleep(STEP_DELAY)
print('done with the {} dough'.format(self.dough.name))
class MargaritaBuilder:
def __init__(self):
self.pizza = Pizza('margarita')
self.progress = PizzaProgress.queued
self.baking_time = 5
def prepare_dough(self):
self.progress = PizzaProgress.preparation
self.pizza.prepare_dough(PizzaDough.thin)
def add_sauce(self):
print('adding the tomato sauce to your margarita....')
self.pizza.sauce = PizzaSauce.tomato
time.sleep(STEP_DELAY)
print('done with the tomato sauce')
def add_topping(self):
print('adding the topping (double mozzarella,oregano) to your margarita')
self.pizza.topping.append([i for i in (PizzaTopping.double_mozzarella, PizzaTopping.oregano)])
time.sleep(STEP_DELAY)
print('done with the topping (double mozzarella, oregano)')
def bake(self):
self.progress = PizzaProgress.baking
print('baking your margarita for {} seconds'.format(self.baking_time))
time.sleep(self.baking_time)
self.progress = PizzaProgress.ready
print('your margarita is ready')
class CreamyBaconBuilder:
def __init__(self):
self.pizza = Pizza('creamy bacon')
self.progress = PizzaProgress.queued
self.baking_time = 7
def prepare_dough(self):
self.progress = PizzaProgress.preparation
self.pizza.prepare_dough(PizzaDough.thick)
def add_sauce(self):
print('adding the creme fraiche sauce to your creamy bacon')
self.pizza.sauce = PizzaSauce.creme_fraiche
time.sleep(STEP_DELAY)
print('done with the creme fraiche sauce')
def add_topping(self):
print('adding the topping (mozzarella, bacon, ham, mushrooms,red onion, oregano) to your creamy bacon')
self.pizza.topping.append([t for t in (
PizzaTopping.mozzarella, PizzaTopping.bacon, PizzaTopping.ham, PizzaTopping.mushrooms,
PizzaTopping.red_onion,
PizzaTopping.oregano)])
time.sleep(STEP_DELAY)
print('done with the topping (mozzarella, bacon, ham,mushrooms, red onion, oregano)')
def bake(self):
self.progress = PizzaProgress.baking
print('baking your creamy bacon for {} seconds'.format(self.baking_time))
time.sleep(self.baking_time)
self.progress = PizzaProgress.ready
print('your creamy bacon is ready')
class Waiter:
def __init__(self):
self.builder = None
def construct_pizza(self, builder):
self.builder = builder
[step() for step in (builder.prepare_dough, builder.add_sauce, builder.add_topping, builder.bake)]
@property
def pizza(self):
return self.builder.pizza
def validate_style(builders):
try:
pizza_style = input('what pizza would you like, [m]argarita or [c]reamy bacon?')
builder = builders[pizza_style]()
valid_input = True
except KeyError as err:
print('Sorry, only margarita (key m) and creamy bacon (key c) are available')
return (False, None)
return (True, builder)
def main():
builders = dict(m=MargaritaBuilder, c=CreamyBaconBuilder)
valid_input = False
while not valid_input:
valid_input, builder = validate_style(builders)
print()
waiter = Waiter()
# waiter.construct_pizza(builder)
waiter.construct_pizza(builder)
pizza = waiter.pizza
print()
print('Enjoy your {}!'.format(pizza))
if __name__ == '__main__':
main()
结果一:
what pizza would you like, [m]argarita or [c]reamy bacon?c
preparing the thick dough of your creamy bacon....
done with the thick dough
adding the creme fraiche sauce to your creamy bacon
done with the creme fraiche sauce
adding the topping (mozzarella, bacon, ham, mushrooms,red onion, oregano) to your creamy bacon
done with the topping (mozzarella, bacon, ham,mushrooms, red onion, oregano)
baking your creamy bacon for 7 seconds
your creamy bacon is ready
Enjoy your creamy bacon!
结果二:
what pizza would you like, [m]argarita or [c]reamy bacon?m
preparing the thin dough of your margarita....
done with the thin dough
adding the tomato sauce to your margarita....
done with the tomato sauce
adding the topping (double mozzarella,oregano) to your margarita
done with the topping (double mozzarella, oregano)
baking your margarita for 5 seconds
your margarita is ready
Enjoy your margarita!
建造pizza示例
Django设计模式的更多相关文章
- 6.-Django设计模式及模版层
一.MVC (java等其他语言) MVC代表Model-view-Contorller(模型-视图-控制器)模式 M模型层主要用于对数据库层的封装 V视图层用于向用户展示结果 C控制器用于处理请求. ...
- Django 学习笔记(一) --- Hello Django
人生苦短 ~ Tips:仅适用于 Python 3+(反正差别不大,py2 改改也能用).因为据 Python 之父 Guido van Rossum 说会在 2020 年停止对 Python 2 的 ...
- Django简介(MVC、MTV)
Django简介 MVC Model(模型)- 应用程序中处理数据逻辑部分且与数据库交互,用于存取数据的部分 View(视图)- 用于处理后的数据界面展示,且视图通常是由模型数据创建的,是用户看到并与 ...
- django 的基础设计
一.web程序工作流程 二.django 的基础介绍 目的:了解Django框架的作用和特点 作用: 简便.快速的开发数据库驱动的网站 Django的优势 快速开发 MVT 功能齐全 Django学 ...
- django基础回顾
1,web项目工作流程 1.1 了解web程序工作流程 1.2 django生命周期2,django介绍 目的:了解Django框架的作用和特点 作用: 简便.快速的开发数据库驱动的网站 Django ...
- django基本内容
1,流程 1.1 了解web程序工作流程 1.2 django生命周期 2,django介绍 目的:了解Django框架的作用和特点 作用: 简便.快速的开发数据库驱动的网站 django的优 ...
- day1(Django)
1,web项目工作流程 1.1 了解web程序工作流程 1.2 django生命周期 2,django介绍 目的:了解Django框架的作用和特点作用: 简便.快速的开发数据库驱动的网站 Djan ...
- Python资源大全
The Python Tutorial (Python 2.7.11) 的中文翻译版本.Python Tutorial 为初学 Python 必备官方教程,本教程适用于 Python 2.7.X 系列 ...
- 如何系统地自学 Python?
最近开始系统的学习Python,以及整理的一些资料.github记录着个人自学 Python 的过程,持续更新.欢迎大家一起来完善这个自学Python学习的项目,给后来者一个参考的学习过程.githu ...
随机推荐
- golang编程之我见
golang编程之我见 学习了两个月的golang,语法算是基本掌握了,从一个C++程序员的角度,提出自己的几个看法吧. 1,没有一个好的包管理工具. 我在公司用的是glide的包管理,这个工具的好处 ...
- TextCNN
一.什么是TextCNN? 将卷积神经网络CNN应用到文本分类任务,利用多个不同size的kernel来提取句子中的关键信息(类似于多窗口大小的ngram),从而能够更好地捕捉局部相关性. 二.Tex ...
- InstallShield2015制作安装包----------卸载后删除安装目录和文件
卸载程序后,一般是需要将安装目录清除干净.但是,如果程序运行中有文件生成,这时InstallShield自带的卸载程序,不会卸载这些运行时生成的文件. 卸载不干净,可能会对下次程序的安装,和安装后的运 ...
- ****************VS编码操作实践******************
下面是今天主要练习的内容: 运用到的内容有 {运算符.强制转换.数据类型的运用.转义字符.变量与常量.基本类型的转换等} 1) 首先我们来看 下列的编码是由三大类组成的 ① 定制变量与常量 蓝色部 ...
- fiddler2抓包数据工具使用教程
一款免费且功能强大的数据包抓取软件.它通过代理的方式获取程序http通讯的数据,可以用其检测网页和服务器的交互情况,能够记录所有客户端和服务器间的http请求,支持监视.设置断点.甚至修改输入输出数据 ...
- rpgmakermv(10) GraphicalDesignMode
插件地址:https://github.com/triacontane/RPGMakerMV/blob/master/GraphicalDesignMode.js 原文: メニュー画面や戦闘画面など各 ...
- hdu2295DLX重复覆盖+二分
题目是说 给了n个城市 m个雷达 你只能选择其中的k个雷达进行使用 你可以设置每个雷达的半径,最后使得所有城市都被覆盖,要求雷达的半径尽可能的小(所有雷达的半径是一样的) 二分最小半径,然后每次重新建 ...
- datetime处理日期和时间
datetime.now() # 获取当前datetimedatetime.utcnow() datetime(2017, 5, 23, 12, 20) # 用指定日期时间创建datetime 一.将 ...
- Mysql Federated For Windows
[1]windows环境下打开federated (1)关闭.命令:mysql> net stop mysql (2)添加federated字段.在my.ini文件中添加一个字段,注意位于[my ...
- OpenCV学习笔记(一) - 边界填充、Rect函数
边界填充: c++实现,测试在mac pro里,输入720p时间0.4ms: cv::copyMakeBorder(image, dst, , , , , cv::BORDER_REPLICATE); ...