书上,真的用一个人穿衣打拌来讲解装饰模式的呢。

from abc import ABCMeta, abstractmethod

class Person(metaclass=ABCMeta):

    def __init__(self, name):
        self._name = name

    @abstractmethod
    def wear(self):
        print("着装。。。")

class Engine(Person):

    def __init__(self, name, skill):
        super().__init__(name)
        self.__skill = skill

    def get_skill(self):
        return self.__skill

    def wear(self):
        print("我是{}工程师{}。".format(self.get_skill(), self._name))
        super().wear()

class Teacher(Person):

    def __init__(self, name, title):
        super().__init__(name)
        self.__title = title

    def get_title(self):
        return self.__title

    def wear(self):
        print("我是{}{}。".format(self._name, self.get_title() ))
        super().wear()

class ClothingDecorator(Person):

    def __init__(self, person):
        self._decorated = person

    def wear(self):
        self._decorated.wear()
        self.decorate()

    @abstractmethod
    def decorate(self):
        pass

class CasualPantDecorator(ClothingDecorator):

    def __init__(self, person):
        super().__init__(person)

    def decorate(self):
        print("一条卡其色休闲裤")

class BeltDecorator(ClothingDecorator):

    def __init__(self, person):
        super().__init__(person)

    def decorate(self):
        print("一条银色针扣头的黑色腰带")

class LeatherShoeDecorator(ClothingDecorator):

    def __init__(self, person):
        super().__init__(person)

    def decorate(self):
        print("一双深色休闲皮鞋")

class KnittedSweaterDecorator(ClothingDecorator):

    def __init__(self, person):
        super().__init__(person)

    def decorate(self):
        print("一件紫红色针织毛衣")

class WhiteShirtDecorator(ClothingDecorator):

    def __init__(self, person):
        super().__init__(person)

    def decorate(self):
        print("一件白色衬衫")

class GlassesDecorator(ClothingDecorator):

    def __init__(self, person):
        super().__init__(person)

    def decorate(self):
        print("一副方形黑框眼镜")

def test_decorator():
    tony = Engine("Tony", "客户端")
    pant = CasualPantDecorator(tony)
    belt = BeltDecorator(pant)
    shoes = LeatherShoeDecorator(belt)
    shirt = WhiteShirtDecorator(shoes)
    sweater = KnittedSweaterDecorator(shirt)
    glasses = GlassesDecorator(sweater)
    glasses.wear()

    print()
    decorator_teacher = GlassesDecorator(WhiteShirtDecorator(LeatherShoeDecorator(Teacher("wells", "教授"))))
    decorator_teacher.wear()

test_decorator()
C:\Python36\python.exe C:/Users/Sahara/PycharmProjects/test1/test.py
我是客户端工程师Tony。
着装。。。
一条卡其色休闲裤
一条银色针扣头的黑色腰带
一双深色休闲皮鞋
一件白色衬衫
一件紫红色针织毛衣
一副方形黑框眼镜

我是wells教授。
着装。。。
一双深色休闲皮鞋
一件白色衬衫
一副方形黑框眼镜

Process finished with exit code 

<人人都懂设计模式>-装饰模式的更多相关文章

  1. 《Android源码设计模式》学习笔记之ImageLoader

    微信公众号:CodingAndroid cnblog:http://www.cnblogs.com/angel88/ CSDN:http://blog.csdn.net/xinpengfei521 需 ...

  2. 《Android源码设计模式》--抽象工厂模式

    No1: 4种MediaPlayer Factory分别会生成不同的MediaPlayer基类:StagefrightPlayer.NuPlayerDriver.MidiFile和TestPlayer ...

  3. 《Android源码设计模式》--Builder模式

    No1: 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示 No2: 在Android源码中,最常用到的Builder模式就是AlertDialog.Builder No3: ...

  4. 《Android源码设计模式》--装饰模式

    No1: Activity继承于ContextThemeWrapper,继承于ContextWrapper,继承于Context. No2: Context中方法的所有实现均由ContextImpl类 ...

  5. 《Android源码设计模式》--模板方法模式

    No1: 模板方法模式包括:抽象类(其中定义了一系列顺序方法).具体实现类A.具体实现类B 如果子类有实现不一样的细节,重写父类的某个方法即可 No2: AsyncTask对象调用execute方法后 ...

  6. 《Android源码设计模式》--状态模式--责任链模式--解释器模式--命令模式--观察者模式--备忘录模式--迭代器模式

    [状态模式] No1: Wifi设置界面是一个叫做WifiSetting的Fragment实现的 No2: 在不同的状态下对于扫描Wifi这个请求的处理是完全不一样的.在初始状态下扫描请求被直接忽略, ...

  7. 《Android源码设计模式》--享元模式

    No1: 享元模式是对象池的一种实现.享元模式用来尽可能减少内存使用量,它适合用于可能存在大量重复对象的场景,来缓存可共享的对象,达到对象共享.避免创建过多对象的效果,这样一来就可以提升性能.避免内存 ...

  8. 《Android源码设计模式》--策略模式

    No1: 定义:策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化. No2: 使用场景: 1)针对同一类型问题的多种处理方式,仅 ...

  9. 《Android源码设计模式》--工厂方法模式

    No1: 对于一个应用程序来说,其真正的入口是在ActivityThread类中,ActivityThread中含有我们熟悉的main方法.ActivityThread是一个final类,不能被继承. ...

  10. 《Android源码设计模式》--原型模式

    No1: 原型模式使用场景: 1)类初始化需要消耗非常多的资源,这个资源包括数据.硬件资源等,通过原型复制避免这些消耗 2)通过new产生一个对象需要非常繁琐的数据准备货访问权限,这是可以使用原型模式 ...

随机推荐

  1. 【BZOJ3529】[SDOI2014] 数表(莫比乌斯反演)

    点此看题面 大致题意: 规定一个\(n*m\)数表中每个数为\(\sum_{d|i,d|j}d\),求数表中不大于\(a\)的数之和. 不考虑限制 我们先不考虑限制,来推一波式子. 首先,易知数表中第 ...

  2. [LeetCode] 367. Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  3. 【洛谷】P4594 [COCI2011-2012#5] BLOKOVI

    本来已经有一个专门记录洛谷题目的博客了,但这个题之毒瘤...... 为你专门写一篇总行了吧...... 传送门 先说一句,这个题每次摆放都靠到最右边不一定是最优的 因为它可以这个亚子 就是说上面那个块 ...

  4. python总结十

    1.代码int('20',8)的返回结果是:16 2.日志的统计和记录对于程序开发来说非常重要,python提供了非常好用的日志模块logging 3.元祖修改 4.python内置映射类型称为字典 ...

  5. linux查看每秒的网络流量

    import os import time cmd = 'ifconfig | grep "RX bytes" | tail -1 | awk -F":" \' ...

  6. 基于 Docker 实现 DevOps 的一些探索

    DevOps 介绍 DevOps(Deveplopment 和 Operations 的简称),中译为开发运维一体化,可定义为是一种过程.方法.文化.运动或实践,主要是为了通过一条高度自动化的流水线来 ...

  7. springboot mybatis使注解和xml两种方式同时生效

    声明:该博客参考了:https://www.jianshu.com/p/53762ac6d31c 如果上面这个博客中的内容已经解决了你的问题,那就不用往下看了,如何按照上面的配置一直报这个异常: or ...

  8. linux webbench测试高并发方法

    linux webbench测试高并发方法由于ab小工具 测试高并发 会出错 具体原因http://newmiracle.cn/?p=594所以采用webbench这个来测试<pre> w ...

  9. Ubuntu16 安装 wireshark

    添加源 sudo apt-add-repository ppa:wireshark-dev/stable 更新 sudo apt-get update 安装 sudo apt-get install ...

  10. Wait… What Happens When my React Native Application Starts? — An In-depth Look Inside React Native

    Discover how React Native functions internally, and what it does for you without you knowing it. Dis ...