<人人都懂设计模式>-装饰模式
书上,真的用一个人穿衣打拌来讲解装饰模式的呢。
- 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产生一个对象需要非常繁琐的数据准备货访问权限,这是可以使用原型模式 ...
随机推荐
- Linux性能优化实战学习笔记:第二十一讲
一 内存性能指标 1.系统内存使用情况 共享内存:是通过tmpfs实现的,所以它的大小也就是tmpfs使用的大小了tmpfs其实也是一种特殊的缓存 可用内存:是新进程可以使用的最大内存它包括剩余内存和 ...
- sonatype nexus安装教程
1. 安装nexus前需要先安装maven.(详见jdk安装教程)2. 将nexus-2.0.2.rar放到d:\teamwork中,点击右键,解压到当前文件夹中.其中包含两个文件夹:nexus,so ...
- dataframe 基本操作
package com.jason.example import org.apache.spark.sql.functions.broadcast class DFTest extends Spark ...
- python字符串格式化方法%s和format函数
1.%s方法 一个例子 print("my name is %s and i am %d years old" %("xiaoming",18) 输出结果:my ...
- FFT(快速傅里叶变换)
FFT(快速傅里叶变换) 前置知识 \(1.复数\) \(2.单位根\) \(3.循环结构\) \(4.C++\) 1.复数 \(定义:形如a+bi的数,其中i^2=-1\) \(计算:1.(a+bi ...
- 本周学习总结(原生+Echarts地图)
本周主要想完成工作中大屏地图相关的知识,所以学习的时间不是很长 dsa.js(数据结构) 拖了两个星期还没看,等啥时候继续研究算法和数据结构再看源码 GoJS 有时间要好好研究下 https://gi ...
- sql server锁表、查询被锁表、解锁被锁表的相关语句
MSSQL(SQL Server)在我的印象中很容易锁表,大致原因就是你在一个窗口中执行的DML语句没有提交,然后又打开了一个窗口对相同的表进行CRUD操作,这样就会导致锁表.锁表是一种保持数据一致性 ...
- java poi 读取有密码加密的Excel文件
String excelPath = "Excel文件路徑"; String password = "Excel文件密碼"; Workbook workbook ...
- 脱离 WebView 的通信 JavaScriptCore
JavascriptCore JavascriptCore 一直作为 WebKit 中内置的 JS 引擎使用,在 iOS7 之后,Apple 对原有的 C/C++ 代码进行了 OC 封装,成为系统级的 ...
- 线程之间灵活传递信号(ManualResetEventSlim )
当主程序启动时,首先创建ManualResetEventSlim 类的一个实例.然后启动三个线程,等待事件信号通知它们继续执行. /// <summary> /// 创建 ManualRe ...