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转载版

最新文章

  1. 地图中插入表格——ArcMap篇
  2. python 类定义 继承
  3. 新浪微博UWP UI意见征求
  4. Springlake-01 介绍&功能&安装
  5. DTD - XML Building Blocks
  6. Python网页爬虫(一)
  7. Windows Phone 9再见了!
  8. [SignalR]配置路由
  9. 201521123055《Java程序设计》第1周学习总结
  10. BeginInvoke()使用
  11. nagios的安装
  12. 第三次C语言作业
  13. 记一个万金油开源框架JHipster
  14. MongoDB分片群集的部署(用心描述,详细易懂)!!
  15. robot framework 提示‘pybot 不是内部命令’
  16. [UE4]Overlap Event 碰撞事件
  17. android发布新版忘记keystore(jks)密码终极解决方案
  18. Ocelot简易教程(一)之Ocelot是什么
  19. 洛谷3707 [SDOI2017] 相关分析 【线段树】
  20. [UE4]Window Title Bar Area

热门文章

  1. git push超过100M文件处理方法
  2. SSH服务器拒绝了密码。请再试一次。怎么改都不行
  3. 史上最全的Excel导入导出之easyexcel
  4. Django笔记&教程 5-2 进阶查询——Queryset
  5. 数据库学习笔记 - MySQL基础知识
  6. JSON实现序列化dump和dumps方法,JSON实现反序列化loads和load方法
  7. ubuntu 首次登陆设置root密码
  8. SpringCloud升级之路2020.0.x版-37. 实现异步的客户端封装配置管理的意义与设计
  9. [gym102511K]Traffic Blights
  10. [cf516E]Drazil and His Happy Friends