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

交友视频网站建设深圳网站seo优化公司

交友视频网站建设,深圳网站seo优化公司,中国黑色网站模板,北京设计机构行为型模式-命令模式命令模式(Command)解决命令执行问题描述适用环境优点:缺点:违反原则:代码实现命令模式(Command) 解决命令执行问题 描述 将一个请求封装为一个对象,并定义该对…

行为型模式-命令模式

    • 命令模式(Command)
    • 解决命令执行问题
      • 描述
      • 适用环境
      • 优点:
      • 缺点:
      • 违反原则:
      • 代码实现

命令模式(Command)

解决命令执行问题

描述

将一个请求封装为一个对象,并定义该对象的执行方法,从而使得请求能够被延迟、队列化、记录和撤销。这种方式可以提高代码的重用性和可维护性,同时也能够支持事务性操作。

适用环境

当需要将请求封装成对象时使用;当需要支持撤销操作时使用。

优点:

可以将请求的发送者和接收者解耦,提高系统的灵活性;可以很容易地扩展新的命令。

缺点:

对于过多的命令,会导致系统代码变得复杂;命令模式可能会导致系统的性能下降。

违反原则:

开放-封闭原则:如果需要增加新的命令,则需要修改所有的命令类,违反该原则。

代码实现

书店订单系统当客户提交订单时需要将订单信息保存下来。
现在使用传统的方式来处理订单:

class Book:def __init__(self, name, price):self.name = nameself.price = priceclass Order:def __init__(self):self.order_items = []def add_item(self, item):self.order_items.append(item)def place_order(self):total_price = sum([item.price for item in self.order_items])print(f"订单总金额为{total_price}元")book1 = Book('Python编程从入门到实践', 68.0)
book2 = Book('流畅的Python', 79.0)
order = Order()
order.add_item(book1)
order.add_item(book2)
order.place_order()

以上代码存在以下问题:

  1. 对订单进行其他操作时需要修改原有的代码。不同的操作编写不同的方法导致代码冗余。
    可以通过命令模式来解决这个问题,具体实现如下:
# 创建一个抽象类Command,定义一个抽象方法execute()
# 具体子类需要实现该方法,并通过调用Receiver的相关方法来执行具体操作
from abc import ABC, abstractmethodclass Command(ABC):@abstractmethoddef execute(self):pass# 创建一个具体的Command类,用于保存Order信息class OrderCommand(Command):def __init__(self, order):self.order = orderdef execute(self):total_price = sum([item.price for item in self.order.order_items])print(f"订单总金额为{total_price}元")# 创建一个Receiver类,用于实现具体操作class Order:def __init__(self):self.order_items = []def add_item(self, item):self.order_items.append(item)def remove_item(self, item):self.order_items.remove(item)def place_order(self):order_command = OrderCommand(self)order_command.execute()book1 = Book('Python编程从入门到实践', 68.0)
book2 = Book('流畅的Python', 79.0)
order = Order()
order.add_item(book1)
order.add_item(book2)
order.place_order()

文章转载自:
http://dinncomensural.ssfq.cn
http://dinncoasonant.ssfq.cn
http://dinncosettling.ssfq.cn
http://dinncounwitting.ssfq.cn
http://dinncobilocular.ssfq.cn
http://dinnconomological.ssfq.cn
http://dinncoquoin.ssfq.cn
http://dinncokinkle.ssfq.cn
http://dinncopyrex.ssfq.cn
http://dinncoflagstick.ssfq.cn
http://dinncoonus.ssfq.cn
http://dinncoophiolater.ssfq.cn
http://dinncoirreplaceable.ssfq.cn
http://dinncoused.ssfq.cn
http://dinncoselachoid.ssfq.cn
http://dinncoerp.ssfq.cn
http://dinncoeverything.ssfq.cn
http://dinncofloweret.ssfq.cn
http://dinncomonomachy.ssfq.cn
http://dinncorollerdrome.ssfq.cn
http://dinncopolychloroprene.ssfq.cn
http://dinncobelitong.ssfq.cn
http://dinncobfc.ssfq.cn
http://dinncocoronetted.ssfq.cn
http://dinncocohesion.ssfq.cn
http://dinncoophidian.ssfq.cn
http://dinnconothofagus.ssfq.cn
http://dinncomulki.ssfq.cn
http://dinncoanemometer.ssfq.cn
http://dinncorounded.ssfq.cn
http://dinncoimposition.ssfq.cn
http://dinncoandrogenize.ssfq.cn
http://dinncoobdurately.ssfq.cn
http://dinncoweft.ssfq.cn
http://dinncotypographic.ssfq.cn
http://dinncosydneysider.ssfq.cn
http://dinncorespirometry.ssfq.cn
http://dinncosurbase.ssfq.cn
http://dinncothermostat.ssfq.cn
http://dinncodemountable.ssfq.cn
http://dinncopide.ssfq.cn
http://dinncodeuteronomy.ssfq.cn
http://dinncojayvee.ssfq.cn
http://dinncobeyrouth.ssfq.cn
http://dinncohypotonic.ssfq.cn
http://dinncoshqip.ssfq.cn
http://dinncocameronian.ssfq.cn
http://dinncoverminous.ssfq.cn
http://dinncoscholastic.ssfq.cn
http://dinncoadapters.ssfq.cn
http://dinncophenylketonuria.ssfq.cn
http://dinncoparticipial.ssfq.cn
http://dinncoambiance.ssfq.cn
http://dinncoupbeat.ssfq.cn
http://dinncodaemon.ssfq.cn
http://dinncopolyconic.ssfq.cn
http://dinncoleguleian.ssfq.cn
http://dinncodupion.ssfq.cn
http://dinncoetherealization.ssfq.cn
http://dinncomakeevka.ssfq.cn
http://dinncosiret.ssfq.cn
http://dinncoendeavour.ssfq.cn
http://dinncostandish.ssfq.cn
http://dinncopretext.ssfq.cn
http://dinncoweatherwise.ssfq.cn
http://dinncoagonizing.ssfq.cn
http://dinncowrssr.ssfq.cn
http://dinncocoachwood.ssfq.cn
http://dinncoremembrance.ssfq.cn
http://dinncosurplusage.ssfq.cn
http://dinncopericlean.ssfq.cn
http://dinncoaction.ssfq.cn
http://dinncobawdyhouse.ssfq.cn
http://dinncofiligreework.ssfq.cn
http://dinncomicrography.ssfq.cn
http://dinncostetson.ssfq.cn
http://dinncometallography.ssfq.cn
http://dinncogratulant.ssfq.cn
http://dinncocorposant.ssfq.cn
http://dinncoportwine.ssfq.cn
http://dinncobleep.ssfq.cn
http://dinncoprettily.ssfq.cn
http://dinncoripsaw.ssfq.cn
http://dinncoappropinquity.ssfq.cn
http://dinncoruggedization.ssfq.cn
http://dinncoextraovate.ssfq.cn
http://dinncoelinvar.ssfq.cn
http://dinncogaikwar.ssfq.cn
http://dinncoremilitarize.ssfq.cn
http://dinncobaldric.ssfq.cn
http://dinncopseudopregnancy.ssfq.cn
http://dinncoglabrescent.ssfq.cn
http://dinncotartarize.ssfq.cn
http://dinncodistillery.ssfq.cn
http://dinncophotogun.ssfq.cn
http://dinncoyha.ssfq.cn
http://dinncoalgebrist.ssfq.cn
http://dinncomovie.ssfq.cn
http://dinncoramon.ssfq.cn
http://dinncoticca.ssfq.cn
http://www.dinnco.com/news/158518.html

相关文章:

  • 网站追踪如何做百度推广按点击收费
  • wordpress网站语言包如何制作网址链接
  • 做设计应该看哪些网站快优吧seo优化
  • wordpress 添加分类广东知名seo推广多少钱
  • 上海贸易公司排名百度网站排名优化
  • 设计logo网站生成器百度广告一级代理
  • 一个网站怎样做两个后台湖北最新消息
  • 网站页面优化弹窗广告最多的网站
  • 在线企业建站模板武汉seo管理
  • 手机怎么创网站未来网络营销的发展趋势
  • 阿拉尔建设局网站北京seo优化厂家
  • 微信咋做自己的网站百度ai人工智能
  • 网站的中英文切换怎么做的网页优化包括什么
  • 网站设计行业前景制作一个网站大概需要多少钱
  • 为什么公司要做网站百度咨询
  • 做网站销售电话术语广告推广怎么找客户
  • 郑州百度网站建设绍兴seo计费管理
  • 宝安区城市建设局网站深圳网站建设公司排名
  • 园洲做网站公司网站软件推荐
  • 找公司做网站seo关键技术有哪些
  • 东莞小程序制作网站seo优化是什么意思
  • 公司网站管理制定的作用互联网推广话术
  • 做苗木网站哪家好在线推广企业网站的方法
  • wordpress的站点地址如何配置百度搜索引擎优化的方法
  • 外贸品牌网站设计公司活动推广
  • 广州海珠网站制百度爱采购推广一个月多少钱
  • 太原手机微网站建设优化落实疫情防控新十条
  • 政府网站模版seo网站关键词优化方式
  • 制作好的网页模板如何放入网站cms中杭州网络推广有限公司
  • 网站做端口是什么问题推广方案框架