【编程思想】【设计模式】【结构模式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 ...
随机推荐
- charles抓包修改请求参数发送新的请求
打开charles -->选择请求右击选择compose---修改参数发送请求
- 设计模式学习-使用go实现适配器模式
适配器模式 定义 代码实现 优点 缺点 适用范围 代理.桥接.装饰器.适配器4种设计模式的区别 参考 适配器模式 定义 适配器模式的英文翻译是Adapter Design Pattern.顾名思义,这 ...
- idea创建maven项目时一直在 Process Running
解决方案: 1.设置maven的配置 File--->Settings(Ctrl+Alt+S)--->Build--->Build Tools--->Maven--->R ...
- CKAD认证中的部署教程
在上一章中,我们已经学会了使用 kubeadm 创建集群和加入新的节点,在本章中,将按照 CKAD 课程的方法重新部署一遍,实际上官方教程的内容不多,笔者写了两篇类似的部署方式,如果已经部署了 kub ...
- (前端)面试300问之(3)this的指向判断
一.this的相关理解与解读 1.各角度看this. 1)ECMAScript规范: this 关键字执行为当前执行环境的 ThisBinding. 2)MDN: In most cases, the ...
- 测试平台系列(83) 前置条件支持Redis语句
大家好~我是米洛! 我正在从0到1打造一个开源的接口测试平台, 也在编写一套与之对应的完整教程,希望大家多多支持. 欢迎关注我的公众号测试开发坑货,获取最新文章教程! 回顾 上节我们打了个野,解决了一 ...
- 计算机系统->Hello World的一生 | 程序如何运行
2021年11月27日准备发在基地微信公众号上的推文. 综合了多篇大佬的博客,以及自己已经知道的知识,对一些疑惑进行了现阶段我认为还算满意的解答. 不过又产生了很多疑问: 内存和磁盘的关系 CPU是如 ...
- Spring扩展点-v5.3.9
Spring 扩展点 **本人博客网站 **IT小神 www.itxiaoshen.com 官网地址****:https://spring.io/projects/spring-framework T ...
- Golang进阶,揉碎数据库中间件,干货满满!
目录 必读 一.Centos7.Mac安装MySQL 二.主从复制原理 2.1.基于binlog_filename + position 2.2.基于GTID 三.my.cnf 四.测试SQL 五.中 ...
- 洛谷 P7155 [USACO20DEC] Spaceship P(dp)
Portal Yet another 1e9+7 Yet another 计数 dp Yet another 我做不出来的题 考虑合法的按键方式长啥样.假设我们依次按下了 \(p_1,p_2,\dot ...