当前位置: 首页 > news >正文

基于Python学习《Head First设计模式》第八章 模板方法模式

项目:星巴兹咖啡和茶

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码实现

在这里插入图片描述

from abc import abstractmethodclass CaffeineBeverage:"""咖啡饮料"""def prepare_recipe(self):self.boil_water()self.brew()self.pour_in_cup()self.add_condiments()def boil_water(self):print('将水煮沸')@abstractmethoddef brew(self):print('浸泡或冲泡')def pour_in_cup(self):print('倒入杯子')@abstractmethoddef add_condiments(self):print('添加调料')class Tea(CaffeineBeverage):def brew(self):print('浸泡茶叶')def add_condiments(self):print('添加柠檬')class Coffee(CaffeineBeverage):def brew(self):print('冲泡咖啡')def add_condiments(self):print('添加奶和糖')if __name__ == '__main__':tea = Tea()tea.prepare_recipe()print()coffee = Coffee()coffee.prepare_recipe()"""运行结果:
将水煮沸
浸泡茶叶
倒入杯子
添加柠檬将水煮沸
冲泡咖啡
倒入杯子
添加奶和糖
"""

认识模板方法

在这里插入图片描述
在这里插入图片描述

模板方法定义

在这里插入图片描述
在这里插入图片描述

挂钩模板方法

在这里插入图片描述
在这里插入图片描述

代码实现钩子模板方法

在这里插入图片描述


from abc import abstractmethodclass CaffeineBeverageWithHook:"""咖啡饮料"""def prepare_recipe(self):self.boil_water()self.brew()self.pour_in_cup()if self.customer_wants_condiments():self.add_condiments()def boil_water(self):print('将水煮沸')@abstractmethoddef brew(self):print('浸泡或冲泡')def pour_in_cup(self):print('倒入杯子')@abstractmethoddef add_condiments(self):print('添加调料')def customer_wants_condiments(self):return Trueclass TeaHook(CaffeineBeverageWithHook):def brew(self):print('浸泡茶叶')def add_condiments(self):print('添加柠檬')class CoffeeWithHook(CaffeineBeverageWithHook):def brew(self):print('冲泡咖啡')def add_condiments(self):print('添加奶和糖')def get_user_input(self):answer = input('Would you like milk and sugar with your coffe? (y/n)')return 'no' if not answer else answerdef customer_wants_condiments(self):answer = self.get_user_input()if answer.lower().startswith('y'):return Truereturn Falseif __name__ == '__main__':tea_hook = TeaHook()coffee_hook = CoffeeWithHook()print('制作茶...')tea_hook.prepare_recipe()print('\n制作咖啡...')coffee_hook.prepare_recipe()"""运行结果:
制作茶...
将水煮沸
浸泡茶叶
倒入杯子
添加柠檬制作咖啡...
将水煮沸
冲泡咖啡
倒入杯子
Would you like milk and sugar with your coffe? (y/n)y
添加奶和糖
"""

设计要点

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

总结

在这里插入图片描述

http://www.lqws.cn/news/200719.html

相关文章:

  • 基于Python学习《Head First设计模式》第七章 适配器和外观模式
  • moon服务器引擎-协议生成报错
  • 意识上传伦理前夜:我们是否在创造数字奴隶?
  • Scade 语言概念 - 方程(equation)
  • 1990-2023年 地级市人工智能企业数量-社科经管实证数据
  • Linux 文件系统与 I/O 编程核心原理及实践笔记
  • Python Cookbook-7.12 在 SQLite 中储存 BLOB
  • 华为云Flexus+DeepSeek征文|Dify - LLM 云服务单机部署大语言模型攻略指南
  • 又是一年高考季
  • 台式机电脑CPU天梯图2025年6月份更新:CPU选购指南及推荐
  • 《经济学原理》第9版第6章供给、需求和政府政策
  • 性能优化笔记
  • IT学习方法与资料分享
  • Srping Cloud Gateway 跨域配置 CorsWebFilter
  • 使用 Ansible 在 Windows 服务器上安装 SSL 证书系列之二
  • Qt Quick Test模块功能及架构
  • java_网络服务相关_gateway_nacos_feign区别联系
  • DeepSeek09-open-webui使用
  • 第二十八课:深度学习及pytorch简介
  • 现代C++特性(一):基本数据类型扩展
  • 低功耗MQTT物联网架构Java实现揭秘
  • 基于Spring Boot的云音乐平台设计与实现
  • C++类和对象(上)
  • C++ 信息学奥赛总复习题(第一章)
  • 【HarmonyOS 5】运动健康开发实践介绍以及详细案例
  • K8S认证|CKS题库+答案| 5.日志审计
  • Python Cookbook-7.11 在 PostgreSQL 中储存 BLOB
  • Ubuntu下有关UDP网络通信的指令
  • (每日一道算法题)二叉树剪枝
  • 【选配电脑】CPU核显工作机控制预算5000