栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【python】装饰模式

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【python】装饰模式

【python】装饰模式

【python】简单工厂模式
【python】策略模式
【python】装饰模式


文章目录
  • 【python】装饰模式
  • 摘要
  • 一、装饰模式的官方解释
  • 二、私人解释
  • 三、优缺点
  • 四、图与代码
    • 1.UML类图
    • 2.python代码
      • 输出如下:
    • 3.应用代码
      • 输出如下:
  • 总结


摘要

抽空学习下常见的20几种设计模式,python2.7来设计实现。基本就根据某设计模式书籍来实现python版本。希望能坚持下来全部介绍完。设计模式介绍中可能会涉及到一些UML类图,忘了的可以看一下https://blog.csdn.net/zhaxun/article/details/124048871


一、装饰模式的官方解释

23种设计模式之一,英文叫Decorator Pattern,又叫装饰者模式。装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
(1) 装饰对象和真实对象有相同的接口。这样客户端对象就能以和真实对象相同的方式和装饰对象交互。
(2) 装饰对象包含一个真实对象的引用(reference)
(3) 装饰对象接受所有来自客户端的请求。它把这些请求转发给真实的对象。
(4) 装饰对象可以在转发这些请求以前或以后增加一些附加功能。这样就确保了在运行时,不用修改给定对象的结构就可以在外部增加附加的功能。在面向对象的设计中,通常是通过继承来实现对给定类的功能扩展。

二、私人解释

动态地给一个对象添加一些额外的职责,听起来是不是和hook很像。本质上也是一种hook方式。

三、优缺点

优点
1 Decorator模式与继承关系的目的都是要扩展对象的功能,但是Decorator可以提供比继承更多的灵活性。
2 通过使用不同的具体装饰类以及这些装饰类的排列组合,设计师可以创造出很多不同行为的组合。
缺点
1 这种比继承更加灵活机动的特性,也同时意味着更加多的复杂性。
2 装饰模式会导致设计中出现许多小类,如果过度使用,会使程序变得很复杂。
3 装饰模式是针对抽象组件(Component)类型编程。但是,如果你要针对具体组件编程时,就应该重新思考你的应用架构,以及装饰者是否合适。当然也可以改变Component接口,增加新的公开的行为,实现“半透明”的装饰者模式。在实际项目中要做出最佳选择。

四、图与代码 1.UML类图

2.python代码

代码如下:

# encoding: utf-8

class Component(object):
	def operation(self):
		raise NotImplementedError


class ConcreteComponent(Component):
	def operation(self):
		print '具体对象的操作'


class Decorator(Component):
	def __init__(self):
		self._component = None

	def set_component(self, component):
		self._component = component

	def operation(self):
		if self._component is not None:
			self._component.operation()


class ConcreteDecoratorA(Decorator):
	def __init__(self):
		self._added_state = '本类ConcreteDecoratorA的独有功能,以区别于ConcreteDecoratorB'  # 本类的独有功能,以区别于ConcreteDecoratorB
		super(ConcreteDecoratorA, self).__init__()

	def operation(self):
		super(ConcreteDecoratorA, self).operation()  # 首先运行基类的operation,再执行本类的功能
		print self._added_state
		print '具体装饰对象A的操作', self._added_state


class ConcreteDecoratorB(Decorator):

	def operation(self):
		super(ConcreteDecoratorB, self).operation()  # 首先运行基类的operation,再执行本类的功能
		self._add_behavior()
		print '具体装饰对象B的操作'

	def _add_behavior(self):  # 本类的独有功能,以区别于ConcreteDecoratorA
		print '本类ConcreteDecoratorB的独有功能,以区别于ConcreteDecoratorA'


if __name__ == '__main__':
	c = ConcreteComponent()
	d1 = ConcreteDecoratorA()
	d2 = ConcreteDecoratorB()
	d1.set_component(c)
	d2.set_component(d1)
	d2.operation()
输出如下:

具体对象的操作
本类ConcreteDecoratorA的独有功能,以区别于ConcreteDecoratorB
具体装饰对象A的操作 本类ConcreteDecoratorA的独有功能,以区别于ConcreteDecoratorB
本类ConcreteDecoratorB的独有功能,以区别于ConcreteDecoratorA
具体装饰对象B的操作

3.应用代码

例如用衣服去装饰某一个人,代码如下:

# encoding: utf-8


class Component(object):
	def show(self):
		raise NotImplementedError


# Person类(ConcreteComponent)
class Person(Component):
	def __init__(self, name):
		self._name = name

	def show(self):
		print '装扮的' + self._name


# 服饰类(Decorator)
class Finery(Component):
	def __init__(self,):
		self._component = None

	def decorate(self, component):
		self._component = component

	def show(self):
		self._component and self._component.show()


# 具体服饰类(ConcreteDecorator)
class TShirts(Finery):
	def show(self):
		print '大T恤'
		super(TShirts, self).show()


class BigTrouser(Finery):
	def show(self):
		print '垮裤'
		super(BigTrouser, self).show()


class Cap(Finery):
	def show(self):
		print '帽子'
		super(Cap, self).show()


if __name__ == '__main__':
	person = Person('小李')
	t_shirts = TShirts()
	big_trouser = BigTrouser()
	cap = Cap()

	print 'n第一种装扮:'
	t_shirts.decorate(person)
	cap.decorate(t_shirts)
	cap.show()
	print 'n第二种装扮:'
	cap.decorate(person)
	big_trouser.decorate(cap)
	big_trouser.show()
输出如下:

第一种装扮:
帽子
大T恤
装扮的小李

第二种装扮:
垮裤
帽子
装扮的小李


总结

装饰模式是为已有功能动态地添加更多功能的一种方式。将类中装饰功能搬移出去,可以简化原有的类。
tips:装饰器模式与python装饰器
尽管名字类似,但Python的装饰器不是装饰模式的实现。装饰器模式是在静态类型的面向对象的编程语言中使用的设计模式,以允许在运行时将功能添加到对象。Python装饰器在定义时将功能添加到函数和方法中,因此是比装饰器模式类是更高层次的构造。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1004725.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号