零食电子商务网站建设策划书,网站布局是什么,中国互联网中心官网,wordpress相册插件nextgen gallery文章目录 8.1 装饰器模式简介8.2 装饰器模式作用8.3 装饰器模式构成8.3.1 装饰器模式包含以下几个核心角色#xff1a;8.3.2 UML类图 8.4 装饰器模式python代码实现8.4.1 基本装饰器的使用8.4.2 多个装饰器的执行顺序8.4.3 带返回值的装饰器的使用8.4.4 装饰器模式-关联类模式… 文章目录 8.1 装饰器模式简介8.2 装饰器模式作用8.3 装饰器模式构成8.3.1 装饰器模式包含以下几个核心角色8.3.2 UML类图 8.4 装饰器模式python代码实现8.4.1 基本装饰器的使用8.4.2 多个装饰器的执行顺序8.4.3 带返回值的装饰器的使用8.4.4 装饰器模式-关联类模式8.4.5 装饰器模式-无参数8.4.6 装饰器模式-接收原函数参数8.4.7 装饰器模式-装饰器自带函数8.4.8 装饰器模式应用-事务提交与回滚 8.5 装饰器模式优点与缺点 8.1 装饰器模式简介
装饰器模式Decorator Pattern允许向一个现有的对象添加新的功能同时又不改变其结构。这种类型的设计模式属于结构型模式它是作为现有的类的一个包装。 装饰器模式通过将对象包装在装饰器类中以便动态地修改其行为。 这种模式创建了一个装饰类用来包装原有的类并在保持类方法签名完整性的前提下提供了额外的功能。
8.2 装饰器模式作用
动态地给一个对象添加一些额外的职责。就增加功能来说装饰器模式相比生成子类更为灵活。 主要解决的问题主要解决一般的我们为了扩展一个类经常使用继承方式实现由于继承为类引入静态特征并且随着扩展功能的增多子类会很膨胀。
8.3 装饰器模式构成
8.3.1 装饰器模式包含以下几个核心角色
抽象组件Component定义了原始对象和装饰器对象的公共接口或抽象类可以是具体组件类的父类或接口。具体组件Concrete Component是被装饰的原始对象它定义了需要添加新功能的对象。抽象装饰器Decorator继承自抽象组件它包含了一个抽象组件对象并定义了与抽象组件相同的接口同时可以通过组合方式持有其他装饰器对象。具体装饰器Concrete Decorator实现了抽象装饰器的接口负责向抽象组件添加新的功能。具体装饰器通常会在调用原始对象的方法之前或之后执行自己的操作。 装饰器模式通过嵌套包装多个装饰器对象可以实现多层次的功能增强。每个具体装饰器类都可以选择性地增加新的功能同时保持对象接口的一致性。
8.3.2 UML类图 8.4 装饰器模式python代码实现
8.4.1 基本装饰器的使用
import time#装饰器函数
def cont_time(func):def inner():start_time time.time()print(计时开始)func()end_time time.time()print(计时结束耗时{:.2f}秒.format(end_time-start_time))return inner#功能函数
cont_time # 相当于do_work cont_time(do_work)
def do_work():print(do work开始)time.sleep(1)print(do work结束)return work is doneres do_work()
print(res)结果如下
计时开始
do work开始
do work结束
计时结束耗时1.01秒
None8.4.2 多个装饰器的执行顺序
def decorator1(func):print(执行装饰器1)def wrapper():print(在装饰器1中执行前)func()print(在装饰器1中执行后)return wrapper
def decorator2(func):print(执行装饰器2)def wrapper():print(在装饰器2中执行前)func()print(在装饰器2中执行后)return wrapper
decorator1
decorator2
def my_function():print(函数执行)my_function()8.4.3 带返回值的装饰器的使用
import time#装饰器函数
def cont_time(func):def inner():start_time time.time()print(计时开始)res func() #在这里接收end_time time.time()print(计时结束耗时{:.2f}秒.format(end_time-start_time))return resreturn inner#功能函数
cont_time # 相当于do_work cont_time(do_work)
def do_work():print(do work开始)time.sleep(1)print(do work结束)return work is doneres do_work()
print(res)结果如下
计时开始
do work开始
do work结束
计时结束耗时1.01秒
None8.4.4 装饰器模式-关联类模式
# encoding: utf-8
装饰模式包含以下4个角色 Component抽象构件 ConcreteComponent具体构件Decorator抽象装饰类 ConcreteDecorator具体装饰类
# 抽象构建,原有产品的功能抽象
class Component(object):def operation(self):raise NotImplementedError#具体构件就是被装饰的类继承抽象组件
class ConcreteComponent(Component):def operation(self):print(车在地上跑)#抽象装饰类和被装饰的类共同继承抽象组件在这里重写抽象类中的方法改变被装饰类的行为
class Decorator(Component):def __init__(self):self._component Nonedef set_component(self,component):self._component componentdef operation(self):if self._component is not None:self._component.operation()#具体装饰类A给汽车扩展一个水里跑的功能
class ConcreteDecoratorA(Decorator):def __init__(self):super(ConcreteDecoratorA,self).__init__()def operation(self):super(ConcreteDecoratorA,self).operation()print(车在水里跑)
# 具体装饰类B
class ConcreteDecoratorB(Decorator):def operation(self):super(ConcreteDecoratorB,self).operation()self._add_behavior()# print(具体装饰对象B的操作)def _add_behavior(self):print(车在天上跑)if __name__ __main__:# 原有的汽车功能只能地上跑c ConcreteComponent()#被A装饰器装饰后扩展水里跑的功能d1 ConcreteDecoratorA()# 继续被B装饰器装饰后扩展天上跑功能d2 ConcreteDecoratorB()d1.set_component(c)d2.set_component(d1)d2.operation()8.4.5 装饰器模式-无参数
# 装饰器--无参数
import time# 装饰器,记录函数运行时间
def decorator01(fun):def wapper():print(装饰器开始运行)stime time.time()print(开始运行原函数)fun()etime time.time()print(原函数结束)print(原函数运行时间 {TIME}.format(TIMEetime - stime))print(装饰器结束)return wapper # 必须要返回一个函数的内存地址# 使用装饰器装饰某个函数,等价于 test01decorator01(test01),
# 即将test01实际引用变成wapper函数内存地址,所以执行test01实际是执行wapper
decorator01
def test01():time.sleep(2)print(test01 运行)test01() # 不修改代码和调用方式,实现添加记录时间功能
8.4.6 装饰器模式-接收原函数参数 # 装饰器2-带参数
import time# 装饰器,记录函数运行时间
def decorator01(fun):def wapper(*args, **kwargs): # 使用非固定参数,无论参数是什么,都可以传递进来stime time.time()fun(*args, **kwargs)etime time.time()print(fun run time is {TIME}.format(TIMEetime - stime))return wapper # 必须要返回一个函数的内存地址# test01() wapper(), 所以装饰器加参数是给嵌套函数加参数
decorator01
def test01(args1):time.sleep(2)print(参数是 {NAME} .format(NAMEargs1))test01(参数示例) # 不修改代码和调用方式,实现添加记录时间功能8.4.7 装饰器模式-装饰器自带函数
# 装饰器
import time# 如果装饰器有参数,最外层是装饰器的参数
def decorator01(*args, **kwargs):print(装饰器参数:, *args, **kwargs)def out(fun): # 第二层才是接受的函数def wapper(*args, **kwargs): # 使用非固定参数,无论参数是什么,都可以传递进来stime time.time()fun(*args, **kwargs)etime time.time()print(fun run time is {TIME}.format(TIMEetime - stime))return wapper # 必须要返回一个函数的内存地址return out # 要返回装饰函数的内存地址# 装饰器本身带参数,此时 decorator01(arg)out,即相当于 out装饰test01,所以 test01out(fun)wapper
decorator01(5)
def test01(args1):time.sleep(2)print(参数是 {NAME} .format(NAMEargs1))test01(参数示例) # 不修改代码和调用方式,实现添加记录时间功能8.4.8 装饰器模式应用-事务提交与回滚
在事务处理中装饰器模式可以用于在执行数据库操作之前和之后执行一些附加的操作例如日志记录、验证、事务管理等。下面是一个使用装饰器模式实现事务处理的示例
class DatabaseOperation: 假设我们有一个数据库操作类 DatabaseOperation它执行数据库的增、删、改、查操作。我们希望在执行这些操作时先进行事务的开启在操作完成后进行事务的提交或回滚。def __init__(self, operation): self.operation operation def execute(self): try: # 开始事务 self.start_transaction() # 执行数据库操作 self.operation.execute() # 提交事务 self.commit_transaction() except Exception as e: # 发生异常时回滚事务 self.rollback_transaction() def start_transaction(self): # 实现事务开始的逻辑 pass def commit_transaction(self): # 实现事务提交的逻辑 pass def rollback_transaction(self): # 实现事务回滚的逻辑 passclass TransactionDecorator: 定义一个装饰器 TransactionDecorator它接受一个数据库操作对象并返回一个添加了事务处理逻辑的装饰器对象。 def __init__(self, operation): self.operation operation def execute(self): transaction TransactionDecorator() transaction.start_transaction() try: self.operation.execute() transaction.commit_transaction() except Exception as e: transaction.rollback_transaction()#使用装饰器模式来执行带有事务处理的操作
TransactionDecorator
class MyDatabaseOperation(DatabaseOperation): def __init__(self, operation): super().__init__(operation)8.5 装饰器模式优点与缺点
优点装饰类和被装饰类可以独立发展不会相互耦合装饰模式是继承的一个替代模式装饰模式可以动态扩展一个实现类的功能。缺点多层装饰比较复杂。