【编程思想】【设计模式】【结构模式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 ...
随机推荐
- Java使用assert断言
Java1.4后新增assert关键字 Idea中开启assert断言 使用 assert boolean表达式 assert boolean表达式 : 错误提示信息 例子 public static ...
- LeetCode 78. 子集 C++(位运算和回溯法)
位运算 class Solution { public: vector<vector<int>> subsets(vector<int>& nums) { ...
- PTA 7-1 邻接矩阵表示法创建无向图 (20分)
PTA 7-1 邻接矩阵表示法创建无向图 (20分) 采用邻接矩阵表示法创建无向图G ,依次输出各顶点的度. 输入格式: 输入第一行中给出2个整数i(0<i≤10),j(j≥0),分别为图G的顶 ...
- pycharm 在flask断点不停止
For me disabling Gevent compatible option in Preferences > Build, Execution, Deployment has helpe ...
- C# 获取PDF中的数字签名证书
PDF中的加数字签名是对文档权威性的有效证明.我们在向PDF文档添加签名时,需要准备可信任的签名证书.同时,对已有的签名,可验证签名是否有效,也可以获取文档中的签名证书信息.下面,以C#代码示例展示如 ...
- 直播预告 | 猪齿鱼V1.1发布,线上新功能详解邀您参加
2021年11月11日,数智化效能平台猪齿鱼 Choerodon发布 V1.1版本,多项功能新增或优化,多管齐下,全面提升团队工作效能! 通过提供体系化方法论和协作.测试.DevOps及容器工具,猪齿 ...
- 18.Java 封装详解/多态详解/类对象转型详解
封装概述 简述 封装是面向对象的三大特征之一. 封装优点 提高代码的安全性. 提高代码的复用性. "高内聚":封装细节,便于修改内部代码,提高可维护性. "低耦合&quo ...
- Springboot 加载配置文件源码分析
Springboot 加载配置文件源码分析 本文的分析是基于springboot 2.2.0.RELEASE. 本篇文章的相关源码位置:https://github.com/wbo112/blogde ...
- win8中让cmd.exe始终以管理员身份运行
最近在学习配置本地服务器,在命令行启动mysql时总是由于权限不足而失败, Win+R -- cmd ,这样总是不能,还要找到cmd.exe右键以管理员身份运行cmd,再 net start mysq ...
- 7.5 实现基于探针对pod中的访问的健康状态检查
1.pod的声明周期 取值 描述 Pending Pod 已被 Kubernetes 系统接受,但有一个或者多个容器尚未创建亦未运行.此阶段包括等待 Pod 被调度的时间和通过网络下载镜像的时间, R ...