<人人都懂设计模式>-装饰模式
书上,真的用一个人穿衣打拌来讲解装饰模式的呢。
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
<人人都懂设计模式>-装饰模式的更多相关文章
- 《Android源码设计模式》学习笔记之ImageLoader
微信公众号:CodingAndroid cnblog:http://www.cnblogs.com/angel88/ CSDN:http://blog.csdn.net/xinpengfei521 需 ...
- 《Android源码设计模式》--抽象工厂模式
No1: 4种MediaPlayer Factory分别会生成不同的MediaPlayer基类:StagefrightPlayer.NuPlayerDriver.MidiFile和TestPlayer ...
- 《Android源码设计模式》--Builder模式
No1: 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示 No2: 在Android源码中,最常用到的Builder模式就是AlertDialog.Builder No3: ...
- 《Android源码设计模式》--装饰模式
No1: Activity继承于ContextThemeWrapper,继承于ContextWrapper,继承于Context. No2: Context中方法的所有实现均由ContextImpl类 ...
- 《Android源码设计模式》--模板方法模式
No1: 模板方法模式包括:抽象类(其中定义了一系列顺序方法).具体实现类A.具体实现类B 如果子类有实现不一样的细节,重写父类的某个方法即可 No2: AsyncTask对象调用execute方法后 ...
- 《Android源码设计模式》--状态模式--责任链模式--解释器模式--命令模式--观察者模式--备忘录模式--迭代器模式
[状态模式] No1: Wifi设置界面是一个叫做WifiSetting的Fragment实现的 No2: 在不同的状态下对于扫描Wifi这个请求的处理是完全不一样的.在初始状态下扫描请求被直接忽略, ...
- 《Android源码设计模式》--享元模式
No1: 享元模式是对象池的一种实现.享元模式用来尽可能减少内存使用量,它适合用于可能存在大量重复对象的场景,来缓存可共享的对象,达到对象共享.避免创建过多对象的效果,这样一来就可以提升性能.避免内存 ...
- 《Android源码设计模式》--策略模式
No1: 定义:策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化. No2: 使用场景: 1)针对同一类型问题的多种处理方式,仅 ...
- 《Android源码设计模式》--工厂方法模式
No1: 对于一个应用程序来说,其真正的入口是在ActivityThread类中,ActivityThread中含有我们熟悉的main方法.ActivityThread是一个final类,不能被继承. ...
- 《Android源码设计模式》--原型模式
No1: 原型模式使用场景: 1)类初始化需要消耗非常多的资源,这个资源包括数据.硬件资源等,通过原型复制避免这些消耗 2)通过new产生一个对象需要非常繁琐的数据准备货访问权限,这是可以使用原型模式 ...
随机推荐
- 知识点总结 HTML+CSS部分
1.html+css经典布局 ①左列定宽,右列自适应 .left{width:100px;float:left;} .right{overflow:hidden;} ②两列顶宽,中栏自适应 .left ...
- [LeetCode] 450. Delete Node in a BST 删除二叉搜索树中的节点
Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...
- [LeetCode] 370. Range Addition 范围相加
Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- Solidity智能合约语言
语言本身 ethereum官网 https://ethereum.org/zh/ 笔记 uint[] result = new uint[](3); uint[] memory result = ...
- 如何备份开拓者TBQuant的策略文件
备份 "C:\TBQuant_V1.1.0.9_X64\users\你的用户名\Strategy\data\strategy.bin" 这个文件即可.
- GNU Wget 1.14 用法
GNU Wget 1.14,非交互式的网络文件下载工具.用法: wget [选项]... [URL]... 长选项所必须的参数在使用短选项时也是必须的. 启动: -V, --version ...
- 纯 css 打造一个小提示 tooltip
最后编辑:2019/11/26 前 无意间在寻找资料时候,发现一个不错的小提示,查看源码竟然是纯手工 css 编写(文章底部参考链接). 效果 使用的特性 css2 中的 attr 函数,所以现在(2 ...
- layer弹出框,zIndex不断增加的问题
针对layer弹出框每次进行弹出操作时z-index不断加1的问题,手动设置过zIndex值不管用,每次关闭时清空layer对象也不管用. 解决办法: 修改layer.js,,将红框代码改为绿框代码, ...
- windows环境中hbase源码编译遇到的问题
转载请注明出处 问题一 [ERROR] Failed to execute goal org.codehaus.mojo:findbugs-maven-plugin:3.0.0:findbugs (d ...