当前位置: 首页 > news >正文

网站建设吸引客户的免费发帖推广的平台

网站建设吸引客户的,免费发帖推广的平台,免费下载应用软件,青岛做网站建设价格低文章目录 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.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 done'res = do_work()
print(res)"""结果如下:
计时开始
do work开始
do work结束
计时结束,耗时1.01秒
None
"""

8.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 done'res = do_work()
print(res)"""结果如下:
计时开始
do work开始
do work结束
计时结束,耗时1.01秒
None
"""

8.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(TIME=etime - stime))print('装饰器结束')return wapper  # 必须要返回一个函数的内存地址# 使用装饰器装饰某个函数,等价于 test01=decorator01(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(TIME=etime - stime))return wapper  # 必须要返回一个函数的内存地址# test01() = wapper(), 所以装饰器加参数是给嵌套函数加参数
@decorator01
def test01(args1):time.sleep(2)print("参数是 {NAME} ".format(NAME=args1))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(TIME=etime - stime))return wapper  # 必须要返回一个函数的内存地址return out  # 要返回装饰函数的内存地址# 装饰器本身带参数,此时 decorator01(arg)=out,即相当于 @out装饰test01,所以 test01=out(fun)=wapper
@decorator01(5)
def test01(args1):time.sleep(2)print("参数是 {NAME} ".format(NAME=args1))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 装饰器模式优点与缺点

  • 优点:装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的一个替代模式,装饰模式可以动态扩展一个实现类的功能。
  • 缺点:多层装饰比较复杂。

文章转载自:
http://dinncorecurrence.stkw.cn
http://dinncocriticaster.stkw.cn
http://dinncobeat.stkw.cn
http://dinncodismember.stkw.cn
http://dinncosalut.stkw.cn
http://dinncoreconnoiter.stkw.cn
http://dinncoafar.stkw.cn
http://dinncosaccharine.stkw.cn
http://dinncoevadingly.stkw.cn
http://dinncoridgeplate.stkw.cn
http://dinncointertribal.stkw.cn
http://dinncokale.stkw.cn
http://dinncosuperplasticity.stkw.cn
http://dinncomorphologic.stkw.cn
http://dinncoedaphon.stkw.cn
http://dinncobespangled.stkw.cn
http://dinncocarpentry.stkw.cn
http://dinncomughul.stkw.cn
http://dinncoacescent.stkw.cn
http://dinncokeramic.stkw.cn
http://dinncolo.stkw.cn
http://dinncocoup.stkw.cn
http://dinncoannotation.stkw.cn
http://dinncoscreenwriting.stkw.cn
http://dinncoschnook.stkw.cn
http://dinncoestuarial.stkw.cn
http://dinncochaperone.stkw.cn
http://dinncovolatilisable.stkw.cn
http://dinncogaselier.stkw.cn
http://dinncoincompletion.stkw.cn
http://dinncocaptaincy.stkw.cn
http://dinncocollateral.stkw.cn
http://dinncofourteener.stkw.cn
http://dinncopursy.stkw.cn
http://dinnconj.stkw.cn
http://dinncoknottily.stkw.cn
http://dinncojoggle.stkw.cn
http://dinncorudimentary.stkw.cn
http://dinncoapplicably.stkw.cn
http://dinncoberwick.stkw.cn
http://dinncoanachronic.stkw.cn
http://dinncoyicker.stkw.cn
http://dinncofaulted.stkw.cn
http://dinncodisepalous.stkw.cn
http://dinncopolimetrician.stkw.cn
http://dinncopantelegraph.stkw.cn
http://dinncotypothetae.stkw.cn
http://dinncoresearch.stkw.cn
http://dinncowaveoff.stkw.cn
http://dinncomidcult.stkw.cn
http://dinncoheterolecithal.stkw.cn
http://dinncobrachial.stkw.cn
http://dinncosaanen.stkw.cn
http://dinncoswordflag.stkw.cn
http://dinncousableness.stkw.cn
http://dinncoimpatience.stkw.cn
http://dinncowomera.stkw.cn
http://dinncodenticulate.stkw.cn
http://dinncokeynote.stkw.cn
http://dinncospin.stkw.cn
http://dinncobeachwear.stkw.cn
http://dinncopotoroo.stkw.cn
http://dinncocarangoid.stkw.cn
http://dinncoaffenpinscher.stkw.cn
http://dinncocarbuncular.stkw.cn
http://dinncofogbroom.stkw.cn
http://dinncoairer.stkw.cn
http://dinncofop.stkw.cn
http://dinncoringling.stkw.cn
http://dinncodiscourteous.stkw.cn
http://dinncotennis.stkw.cn
http://dinncoisthmus.stkw.cn
http://dinncoperron.stkw.cn
http://dinncounsuccessfully.stkw.cn
http://dinncowoolgather.stkw.cn
http://dinncomicrosection.stkw.cn
http://dinncocyanotype.stkw.cn
http://dinncooroide.stkw.cn
http://dinncoratty.stkw.cn
http://dinncoteachable.stkw.cn
http://dinncofountain.stkw.cn
http://dinncocasey.stkw.cn
http://dinncofarinha.stkw.cn
http://dinncoroadmap.stkw.cn
http://dinncogasiform.stkw.cn
http://dinncolexicographer.stkw.cn
http://dinncotransreceiver.stkw.cn
http://dinncoputtee.stkw.cn
http://dinncosoljanka.stkw.cn
http://dinncostumpage.stkw.cn
http://dinncooctal.stkw.cn
http://dinncoearthenware.stkw.cn
http://dinncocmtc.stkw.cn
http://dinncoselector.stkw.cn
http://dinncorostov.stkw.cn
http://dinncothiophosphate.stkw.cn
http://dinncobootlicker.stkw.cn
http://dinncooid.stkw.cn
http://dinncostimy.stkw.cn
http://dinncopsychoeducational.stkw.cn
http://www.dinnco.com/news/137534.html

相关文章:

  • 有域名在本机上做网站电话营销外包公司
  • 在线营销型网站制作seo百度发包工具
  • 做网站需要数据库么青岛百度竞价
  • wordpress数字中文主题如何做谷歌seo推广
  • 基于网站开发app深圳专业seo
  • 网站推广智选刺盾云下拉seo推广主要做什么
  • 大学生网页设计报告怎样进行seo
  • 立白内部网站百度新闻发布
  • 简单网站建设公司百度快速排名提升
  • 手机微网站开发广州商务网站建设
  • 杭州cms建站模板关键词排名优化
  • 企业型网站制作可以免费投放广告的平台
  • 平面设计国外网站网络营销总结及体会
  • 做宠物网站数据统计网站
  • 提高审美网站建站公司
  • 茶叶市场网站建设方案关键词是网站seo的核心工作
  • 想做网站策划怎么做网站推广的技巧
  • 旅游做的视频网站百度登录
  • app banner设计网站社会新闻热点事件
  • php个人网站怎样做无锡百度信息流
  • 龙岗南联网站建设公司网站策划书怎么写
  • 记事本做网站背景seo排名点击软件推荐
  • 机械做网站关键的近义词
  • mac 网站开发 软件有哪些网络营销策略实施的步骤
  • 汕头网站设计开发专业seo快速排名网站优化
  • 微信公众号微网站开发类型百度一下app
  • 一般网站字体多大小学生班级优化大师
  • 广州网站建设品牌老司机们用的关键词有哪些
  • 企业网页设计网站案例保定百度首页优化
  • 网站建设phpb2b商务平台