【编程思想】【设计模式】【结构模式Structural】3-tier
Pyhon版
https://github.com/faif/python-patterns/blob/master/structural/3-tier.py
#!/usr/bin/env python
# -*- coding: utf-8 -*- """
*TL;DR80
Separates presentation, application processing, and data management functions.
""" class Data(object):
""" Data Store Class """ products = {
'milk': {'price': 1.50, 'quantity': 10},
'eggs': {'price': 0.20, 'quantity': 100},
'cheese': {'price': 2.00, 'quantity': 10}
} def __get__(self, obj, klas):
print("(Fetching from Data Store)")
return {'products': self.products} class BusinessLogic(object):
""" Business logic holding data store instances """ data = Data() def product_list(self):
return self.data['products'].keys() def product_information(self, product):
return self.data['products'].get(product, None) class Ui(object):
""" UI interaction class """ def __init__(self):
self.business_logic = BusinessLogic() def get_product_list(self):
print('PRODUCT LIST:')
for product in self.business_logic.product_list():
print(product)
print('') def get_product_information(self, product):
product_info = self.business_logic.product_information(product)
if product_info:
print('PRODUCT INFORMATION:')
print('Name: {0}, Price: {1:.2f}, Quantity: {2:}'.format(
product.title(), product_info.get('price', 0),
product_info.get('quantity', 0)))
else:
print('That product "{0}" does not exist in the records'.format(
product)) def main():
ui = Ui()
ui.get_product_list()
ui.get_product_information('cheese')
ui.get_product_information('eggs')
ui.get_product_information('milk')
ui.get_product_information('arepas') if __name__ == '__main__':
main() ### OUTPUT ###
# PRODUCT LIST:
# (Fetching from Data Store)
# cheese
# eggs
# milk
#
# (Fetching from Data Store)
# PRODUCT INFORMATION:
# Name: Cheese, Price: 2.00, Quantity: 10
# (Fetching from Data Store)
# PRODUCT INFORMATION:
# Name: Eggs, Price: 0.20, Quantity: 100
# (Fetching from Data Store)
# PRODUCT INFORMATION:
# Name: Milk, Price: 1.50, Quantity: 10
# (Fetching from Data Store)
# That product "arepas" does not exist in the records
Python转载版
【编程思想】【设计模式】【结构模式Structural】3-tier的更多相关文章
- 【编程思想】【设计模式】【结构模式Structural】代理模式Proxy
Python版 https://github.com/faif/python-patterns/blob/master/structural/proxy.py #!/usr/bin/env pytho ...
- 【编程思想】【设计模式】【结构模式Structural】MVC
Python版 https://github.com/faif/python-patterns/blob/master/structural/mvc.py #!/usr/bin/env python ...
- 【编程思想】【设计模式】【结构模式Structural】front_controller
Python版 https://github.com/faif/python-patterns/blob/master/structural/front_controller.py #!/usr/bi ...
- 【编程思想】【设计模式】【结构模式Structural】享元模式flyweight
Python版 https://github.com/faif/python-patterns/blob/master/structural/flyweight.py #!/usr/bin/env p ...
- 【编程思想】【设计模式】【结构模式Structural】门面模式/外观模式Facade
Python版 https://github.com/faif/python-patterns/blob/master/structural/facade.py #!/usr/bin/env pyth ...
- 【编程思想】【设计模式】【结构模式Structural】装饰模式decorator
Python版 https://github.com/faif/python-patterns/blob/master/structural/decorator.py #!/usr/bin/env p ...
- 【编程思想】【设计模式】【结构模式Structural】组合模式composite
Python版 https://github.com/faif/python-patterns/blob/master/structural/composite.py #!/usr/bin/env p ...
- 【编程思想】【设计模式】【结构模式Structural】桥梁模式/桥接模式bridge
Python版 https://github.com/faif/python-patterns/blob/master/structural/bridge.py #!/usr/bin/env pyth ...
- 【编程思想】【设计模式】【结构模式Structural】适配器模式adapter
Python版 https://github.com/faif/python-patterns/blob/master/structural/adapter.py #!/usr/bin/env pyt ...
随机推荐
- rocketmq有序消息的(四)
opic的有序消息已经成为mq的标配.而RocketMQ中是这样区分消息类型的, 普通消息也叫做无序消息,简单来说就是没有顺序的消息,而有序消息就是按照一定的先后顺序的消息类型.举个例子,produc ...
- 【JAVA】笔记(7)--- 数组精讲
数组的静态初始化: 1.一维数组: int [ ] arr = { 1,2,3,4 } ; Object [ ] arr = { new Object ( ) , new Object ( ) , ...
- [cf461D]Appleman and Complicated Task
假设该矩形是aij,那么有a(i,j)=a(i-1,j-1)^a(i-1,j+1)^a(i-2,j),不断递归下去可以发现a(i,j)=a(1,y-x+1)^a(1,y-x+3)^--^a(1,x+y ...
- [hdu5901]Count primes
最简单的是利用Min25筛求$h(n)$的过程,即 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define N 1000005 ...
- [bzoj2756]奇怪的游戏
对棋盘黑白染色后,若n和m都是奇数(即白色和黑色点数不同),可以直接算得答案(根据白-黑不变):若n和m不都是奇数,二分答案(二分的上限要大一点,开$2^50$),最后都要用用网络流来判定.考虑判定, ...
- 保姆级神器 Maven,再也不用担心项目构建搞崩了
今天来给大家介绍一款项目构建神器--Maven,不仅能帮我们自动化构建,还能够抽象构建过程,提供构建任务实现:它跨平台,对外提供了一致的操作接口,这一切足以使它成为优秀的.流行的构建工具,从此以后,再 ...
- 力扣 - 剑指 Offer 55 - I. 二叉树的深度
题目 剑指 Offer 55 - I. 二叉树的深度 思路1(DFS) 后续遍历吧,先遍历到最深(递归到末尾返回0),然后从后面一步一步比较取大的值返回,每次返回层数都加1, 执行流程是怎样的:比如其 ...
- 钓鱼小技巧-XLM
前言 XLM钓鱼不是一项新的技术,自从公开以后,网上有很多对其的分析文章,这里仅仅做一个分享和摸索记录.文章中有问题的地方还请指出. 一个简单的例子 新建一个excel表格,右键选择表,选择插入 插入 ...
- (前端)面试300问之(3)this的指向判断
一.this的相关理解与解读 1.各角度看this. 1)ECMAScript规范: this 关键字执行为当前执行环境的 ThisBinding. 2)MDN: In most cases, the ...
- 微信小程序中途加入云开发之坑
一开始未使用云开发的小程序项目,之后想使用云开发能力时,要先删除对应在开发者工具中的项目(先压缩备份源码!),再用开发者工具重新创建,很多时候都需要用这种方式进行处理