[Python设计模式] 第6章 衣服搭配系统——装饰模式
github地址:https://github.com/cheesezh/python_design_patterns
题目
设计一个控制台程序,可以给人搭配嘻哈风格(T恤,垮裤,运动鞋)或白领风格(西装,领带,皮鞋)的衣服并展示,类似QQ秀那样的。
基础版本
class Person():
def __init__(self, name):
self.name = name
def wear_T_shirts(self):
print("T恤")
def wear_big_trouser(self):
print("垮裤")
def wear_sneakers(self):
print("运动鞋")
def wear_suit(self):
print("西装")
def wear_tie(self):
print("领带")
def wear_leather_shoes(self):
print("皮鞋")
def show(self):
print("装扮的{}".format(self.name))
客户端代码
def main():
hezhang = Person("张贺")
print("第一种装扮")
hezhang.wear_T_shirts()
hezhang.wear_big_trouser()
hezhang.wear_sneakers()
hezhang.show()
print("第二种装扮")
hezhang.wear_suit()
hezhang.wear_tie()
hezhang.wear_leather_shoes()
hezhang.show()
main()
第一种装扮
T恤
垮裤
运动鞋
装扮的张贺
第二种装扮
西装
领带
皮鞋
装扮的张贺
点评
- 仅实现了基本功能
- 如果添加“超人”装扮,需要如何做?需要在Person类中改代码;
- 如果Person类除了穿衣服,还要支持吃饭,睡觉等功能,需要如何做?需要在Person类中改代码;
- 综上,需要把“服饰”类和“人”类分离。
改进版本1.0——人衣分离
class Person():
def __init__(self, name):
self.name = name
def show(self):
print("装扮的{}".format(self.name))
from abc import ABCMeta, abstractmethod
class Finery(metaclass=ABCMeta):
@abstractmethod
def show(self):
pass
class TShirts(Finery):
def show(self):
print("T恤")
class BigTrouser(Finery):
def show(self):
print("垮裤")
class Sneakers(Finery):
def show(self):
print("运动鞋")
class Suit(Finery):
def show(self):
print("西装")
class Tie(Finery):
def show(self):
print("领带")
class LeatherShoes(Finery):
def show(self):
print("皮鞋")
客户端代码
def main():
hezhang = Person("张贺")
print("第一种装扮")
t_shirts = TShirts()
big_trouser = BigTrouser()
sneakers = Sneakers()
t_shirts.show()
big_trouser.show()
sneakers.show()
hezhang.show()
print("第二种装扮")
suit = Suit()
tie = Tie()
leather_shoes = LeatherShoes()
suit.show()
tie.show()
leather_shoes.show()
hezhang.show()
main()
第一种装扮
T恤
垮裤
运动鞋
装扮的张贺
第二种装扮
西装
领带
皮鞋
装扮的张贺
点评
分析以下代码:
hezhang = Person("张贺")
t_shirts = TShirts()
big_trouser = BigTrouser()
sneakers = Sneakers()
t_shirts.show()
big_trouser.show()
sneakers.show()
hezhang.show()
用自然语言描述就是:
- 先实例化出
hezhang
这个人类,准确的说,是没有穿衣服的人类; - 再实例化并穿上出各种衣服,T恤,垮裤,运动鞋等;
- 再展示出来
hezhang
;
但是服饰和人之间好像没有任何关系,那么如何用代码表示:
- 实例化没穿衣服的人类
- 为没穿衣服的人类穿上T恤
- 为穿着T恤的人类穿上垮裤
- 为穿着T恤,垮裤的人类穿上运动鞋
这需要用到装饰模式。
装饰模式
装饰模式,为了动态的给一个对象添加一些额外的职责,就增加功能而言,装饰模式比生成子类更为灵活[DP]。
装饰模式有以下几个主要组成部分:
- 组件类Component:定义一个对象接口, 可以给这些对象动态的添加职责;
- 具体组件类ConcretComponent:定义了一个具体对象,也可以给这个对象添加一些职责;
- 装饰类Decorator:装饰抽象类,继承Component,从外类来扩展Component类的功能,对于Component而言,无需知道Decorator的存在;
- 具体装饰类ConcretDecorator:具体装饰类,为Component添加职责
用代码表示:
from abc import ABCMeta, abstractmethod
class Component(metaclass=ABCMeta):
"""
组件类Component:定义一个对象接口, 可以给这些对象动态的添加职责
"""
@abstractmethod
def operation(self):
pass
class ConcreteComponent(Component):
"""
具体组件类ConcretComponent:定义了一个具体对象,也可以给这个对象添加一些职责
"""
def operation(self):
print("具体组件的操作")
class Decorator(Component):
"""
装饰类Decorator:装饰抽象类,继承Component,从外类来扩展Component类的功能,对于Component而言,无需知道Decorator的存在;
"""
def __init__(self):
self.component = None
def set_component(self, component):
self.component = component
def operation(self):
if self.component != None:
self.component.operation()
class ConcreteDecratorA(Decorator):
"""
具体装饰类ConcretDecorator:具体装饰类,为Component添加职责
"""
def __init__(self):
self.added_operation = "A:具体装饰类A独有操作"
def operation(self):
super().operation()
print(self.added_operation)
print("A:具体装饰对象A的操作")
class ConcreteDecratorB(Decorator):
"""
具体装饰类ConcretDecorator:具体装饰类,为Component添加职责
"""
def __init__(self):
self.added_operation = "B:具体装饰类B独有操作"
def operation(self):
super().operation()
print(self.added_operation)
print("B:具体装饰对象B的操作")
def main():
component = ConcreteComponent()
decorator_a = ConcreteDecratorA()
decorator_b = ConcreteDecratorB()
decorator_a.set_component(component)
decorator_b.set_component(decorator_a)
decorator_b.operation()
main()
具体组件的操作
A:具体装饰类A独有操作
A:具体装饰对象A的操作
B:具体装饰类B独有操作
B:具体装饰对象B的操作
改进版本2.0——装饰模式
from abc import ABCMeta,abstractmethod
class Person():
"""
人物类(组件类)
"""
def __init__(self, name):
self.name = name
def show(self):
print("装扮的{}".format(self.name))
class Finery(Person):
"""
服饰类(装饰类)
"""
def __init__(self):
self.component = None
def decorate(self, component):
self.component = component
def show(self):
if self.component != None:
self.component.show()
class TShirts(Finery):
"""
具体装饰类
"""
def show(self):
print("T恤")
super().show()
class BigTrouser(Finery):
"""
具体装饰类
"""
def show(self):
print("垮裤")
super().show()
class Sneakers(Finery):
"""
具体装饰类
"""
def show(self):
print("运动鞋")
super().show()
class Suit(Finery):
"""
具体装饰类
"""
def show(self):
print("西装")
super().show()
class Tie(Finery):
"""
具体装饰类
"""
def show(self):
print("领带")
super().show()
class LeatherShoes(Finery):
"""
具体装饰类
"""
def show(self):
print("皮鞋")
super().show()
客户端代码
def main():
hezhang = Person("张贺")
t_shirts = TShirts()
big_trouser = BigTrouser()
sneakers = Sneakers()
t_shirts.decorate(hezhang)
big_trouser.decorate(t_shirts)
sneakers.decorate(big_trouser)
sneakers.show()
main()
运动鞋
垮裤
T恤
装扮的张贺
点评
上述客户端代码可以用自然语言描述为:
- 实例化没穿衣服的人类
- 为没穿衣服的人类穿上T恤
- 为穿着T恤的人类穿上垮裤
- 为穿着T恤,垮裤的人类穿上运动鞋
所以,装饰模型是为已有功能动态的添加更多功能的一种方式,它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为时,客户端代码可以在运行时根据需要有选择的,按顺序的使用装饰功能包装对象。
装饰模式的优点在于,把类中的装饰功能从类中搬移出去,这样可以简化原有的类,有效的把类的核心职责
和装饰功能
区分开了。而且可以去除相关类中重复的装饰逻辑,即一个装饰功能可以给多个不同的类使用。
[Python设计模式] 第6章 衣服搭配系统——装饰模式的更多相关文章
- [Python设计模式] 第21章 计划生育——单例模式
github地址:https://github.com/cheesezh/python_design_patterns 单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式 ...
- [Python设计模式] 第22章 手机型号&软件版本——桥接模式
github地址:https://github.com/cheesezh/python_design_patterns 紧耦合程序演化 题目1 编程模拟以下情景,有一个N品牌手机,在上边玩一个小游戏. ...
- [Python设计模式] 第1章 计算器——简单工厂模式
github地址:https://github.com/cheesezh/python_design_patterns 写在前面的话 """ 读书的时候上过<设计模 ...
- [Python设计模式] 第28章 男人和女人——访问者模式
github地址:https://github.com/cheesezh/python_design_patterns 题目 用程序模拟以下不同情况: 男人成功时,背后多半有一个伟大的女人: 女人成功 ...
- [Python设计模式] 第26章 千人千面,内在共享——享元模式
github地址:https://github.com/cheesezh/python_design_patterns 背景 有6个客户想做产品展示网站,其中3个想做成天猫商城那样的"电商风 ...
- [Python设计模式] 第25章 联合国维护世界和平——中介者模式
github地址:https://github.com/cheesezh/python_design_patterns 题目背景 联合国在世界上就是中介者的角色,各国之间的关系复杂,类似不同的对象和对 ...
- [Python设计模式] 第18章 游戏角色备份——备忘录模式
github地址:https://github.com/cheesezh/python_design_patterns 题目 用代码模拟以下场景,一个游戏角色有生命力,攻击力,防御力等数据,在打Bos ...
- [Python设计模式] 第17章 程序中的翻译官——适配器模式
github地址:https://github.com/cheesezh/python_design_patterns 适配器模式 适配器模式,将一个类的接口转换成客户希望的另外一个接口.Adapte ...
- [Python设计模式] 第14章 老板来了——观察者模式
github地址:https://github.com/cheesezh/python_design_patterns 题目 用程序模拟以下情景,在一个办公室里,当老板进门的时候,前台秘书就偷偷通知办 ...
随机推荐
- 性能测试三十八:Java性能分析神器-JProfiler安装和简单介绍
Jprofiler是一个重量级的工具,需要分别在服务器和windows都装客户端,会损耗性能,用于发现问题后排查问题,而不是常规的监控 JPROFILER工具下载地址:http://www.ej-te ...
- pytest七:assert断言
断言是写自动化测试基本最重要的一步,一个用例没有断言,就失去了自动化测试的意义了.什么是断言呢?简单来讲就是实际结果和期望结果去对比,符合预期那就测试 pass,不符合预期那就测试 failed py ...
- python3笔记(三)if...else、if...elif...else
if语句 if 语句的基本用法如下: if 表达式: 语句块 其中,表达式可以是一个单纯的布尔值或变量,也可以是比较表达式或逻辑表达式(例 如:a > band a != c),如果表达式为真, ...
- 从输入url到显示网页,后台发生了什么?
参考http://igoro.com/archive/what-really-happens-when-you-navigate-to-a-url/ http://www.cnblogs.com/we ...
- python 全栈开发,Day65(索引)
索引 一.索引的介绍 数据库中专门用于帮助用户快速查找数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置吗,然后直接获取. 二 .索引的作用 约束和加速查找 三.常 ...
- 2018-2019-2 网络对抗技术 20165333 Exp1 PC平台逆向破解
1 逆向及Bof基础实践说明 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件.该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串.该程序同 ...
- POJ 1017 Packets【贪心】
POJ 1017 题意: 一个工厂制造的产品形状都是长方体,它们的高度都是h,长和宽都相等,一共有六个型号,他们的长宽分别为 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. 这些产品通常 ...
- 【Java】 剑指offer(56-2) 数组中唯一只出现一次的数字
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个数组中除了一个数字只出现一次之外,其他数字都出现了三次. ...
- 三篇文章带你极速入门php(三)之php原生实现登陆注册
看下成果 ps:纯天然h5,绝不添加任何添加剂(css)以及化学成分(js)(<( ̄ ﹌  ̄)我就是喜欢纯天然,不接受任何反驳) 关于本文 用原生的php和html做了一个登陆注册,大概是可以窥 ...
- C#多线程编程实战(二):线程同步
2.1 简介 竞争条件:多个线程同时使用共享对象.需要同步这些线程使得共享对象的操作能够以正确的顺序执行 线程同步问题:多线程的执行并没有正确的同步,当一个线程执行递增和递减操作时,其他线程需要依次等 ...