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

大理网站建设网站建设广东省白云区

大理网站建设网站建设,广东省白云区,电影网站建设成本,青海西宁制作网站专业介绍 责任链模式是一种行为型设计模式,它允许多个对象以链式的形式依次处理请求,直到请求被处理或者无处理对象为止 实现方式 责任链模式由多个处理器组成,每个处理器都可以处理一种请求。如果当前处理器无法处理请求,它将把请…

介绍

责任链模式是一种行为型设计模式,它允许多个对象以链式的形式依次处理请求,直到请求被处理或者无处理对象为止

实现方式

责任链模式由多个处理器组成,每个处理器都可以处理一种请求。如果当前处理器无法处理请求,它将把请求传递给下一个处理器,直到请求被处理或者没有处理器可以处理为止。

案例

假设我们正在开发一个电子商务平台,现在需要实现一个购物车功能。当用户添加商品到购物车中时,需要进行以下验证

  • 商品是否存在
  • 商品库存是否充足
  • 商品是否已经下架

我们可以使用责任链模式来实现这个购物车功能。首先定义一个处理器基类

class CartValidator:def __init__(self, next_validator=None):self.next_validator = next_validatordef validate(self, item):if self.next_validator:return self.next_validator.validate(item)return True

这个基类包含了一个next_validator属性,表示后继处理器,以及一个validate方法,用于验证商品是否合法。如果存在后继处理器,则将验证请求传递给后继处理器。

接下来,我们可以定义三个具体的验证处理器,分别验证商品是否存在、商品库存是否充足以及商品是否已经下架

# 模拟
is_product_exist = True  # 判断商品是否存在
is_product_stock_enough = True  # 判断商品库存是否充足
is_product_on_sale = True  # 判断商品是否已经下架class ProductExistValidator(CartValidator):def validate(self, item):# 判断商品是否存在# if not is_product_exist(item):if not is_product_exist:print('商品不存在')return Falsereturn super().validate(item)class ProductStockValidator(CartValidator):def validate(self, item):# 判断商品库存是否充足# if not is_product_stock_enough(item):if not is_product_stock_enough:print('库存不足')return Falsereturn super().validate(item)class ProductStatusValidator(CartValidator):def validate(self, item):# 判断商品是否已经下架# if not is_product_on_sale(item):if not is_product_on_sale:print('商品已下架')return Falsereturn super().validate(item)product_validator_chain = ProductExistValidator(ProductStockValidator(ProductStatusValidator()))def add_to_cart(item):if product_validator_chain.validate(item):# 添加商品到购物车中# add_item_to_cart(item)print("添加成功", item)passelse:# 商品验证失败print("商品验证失败")

这三个处理器都继承自CartValidator类,并覆盖了validate方法,实现了具体的商品验证逻辑。如果商品验证通过,则调用super()
.validate(item)方法,将验证请求传递给下一个处理器。

最后,我们可以将这三个处理器组成一个责任链:

product_validator_chain = ProductExistValidator(ProductStockValidator(ProductStatusValidator()))

测试
正常测试

add_to_cart("IPhone14 pro")

输出结果

添加成功 IPhone14 pro

库存不足测试

# 模拟库存不足
is_product_stock_enough = False
add_to_cart("IPhone14 pro")

输出结果

库存不足
商品验证失败

使用场景

多个对象需要处理同一种请求,但处理的顺序和方式不同。例如,一个在线商店需要对订单进行风险评估,评估过程包括多个步骤,如检查订单是否来自欺诈用户、检查收货地址是否存在风险等。每个步骤可以使用一个处理器来处理,这些处理器可以组成一个责任链,对订单进行逐步风险评估。

对象不知道该由哪个处理器处理请求,需要动态确定处理器。java中的web框架的过滤器,需要根据请求的URL来选择合适的处理器,处理器可以根据自身能力来决定是否能够处理请求。

动态扩展。在系统中需要动态添加或删除处理器,同时保证请求可以被正确处理。例如,一个安全检查系统需要根据系统的安全策略来动态添加或删除安全检查处理器,同时保证请求可以被正确处理。

以上就是对责任链模式的介绍,后面还会介绍更多的设计模式。


欢迎关注,学习不迷路!


文章转载自:
http://chirology.bbrf.cn
http://flq.bbrf.cn
http://polygonometry.bbrf.cn
http://vicious.bbrf.cn
http://sibilance.bbrf.cn
http://doorway.bbrf.cn
http://haman.bbrf.cn
http://photogrammetry.bbrf.cn
http://anticlimactic.bbrf.cn
http://campaign.bbrf.cn
http://koksaphyz.bbrf.cn
http://miscalculation.bbrf.cn
http://drippy.bbrf.cn
http://desperate.bbrf.cn
http://shoal.bbrf.cn
http://pneumatics.bbrf.cn
http://nameable.bbrf.cn
http://clock.bbrf.cn
http://umt.bbrf.cn
http://centipede.bbrf.cn
http://leasehold.bbrf.cn
http://uptodate.bbrf.cn
http://moderato.bbrf.cn
http://yusho.bbrf.cn
http://posterize.bbrf.cn
http://aerophobia.bbrf.cn
http://thymine.bbrf.cn
http://insure.bbrf.cn
http://ungroomed.bbrf.cn
http://muckworm.bbrf.cn
http://pelecaniform.bbrf.cn
http://zoftig.bbrf.cn
http://refractory.bbrf.cn
http://raddled.bbrf.cn
http://gonadotrophic.bbrf.cn
http://summand.bbrf.cn
http://marge.bbrf.cn
http://holdman.bbrf.cn
http://dug.bbrf.cn
http://counterthrust.bbrf.cn
http://uncorrectable.bbrf.cn
http://porringer.bbrf.cn
http://violinmaker.bbrf.cn
http://spottable.bbrf.cn
http://initialese.bbrf.cn
http://draghound.bbrf.cn
http://assagai.bbrf.cn
http://jennings.bbrf.cn
http://extinguishment.bbrf.cn
http://tricorporate.bbrf.cn
http://crunkle.bbrf.cn
http://biblical.bbrf.cn
http://tracheitis.bbrf.cn
http://heteromorphosis.bbrf.cn
http://fogbound.bbrf.cn
http://pionium.bbrf.cn
http://goldless.bbrf.cn
http://southeastwards.bbrf.cn
http://towfish.bbrf.cn
http://surjection.bbrf.cn
http://preincubation.bbrf.cn
http://amt.bbrf.cn
http://craggedness.bbrf.cn
http://frippery.bbrf.cn
http://mal.bbrf.cn
http://syndiotactic.bbrf.cn
http://abstruse.bbrf.cn
http://cannonproof.bbrf.cn
http://pneumatogenic.bbrf.cn
http://cataplasia.bbrf.cn
http://fizzy.bbrf.cn
http://langrage.bbrf.cn
http://milliampere.bbrf.cn
http://wildfire.bbrf.cn
http://mileometer.bbrf.cn
http://adoptee.bbrf.cn
http://dominoes.bbrf.cn
http://fortyfold.bbrf.cn
http://junction.bbrf.cn
http://airframe.bbrf.cn
http://doughhead.bbrf.cn
http://garget.bbrf.cn
http://hexaemeric.bbrf.cn
http://agalloch.bbrf.cn
http://brassard.bbrf.cn
http://crap.bbrf.cn
http://slubberdegullion.bbrf.cn
http://pinhead.bbrf.cn
http://haematopoietic.bbrf.cn
http://napless.bbrf.cn
http://lossless.bbrf.cn
http://neuropterous.bbrf.cn
http://kenogenesis.bbrf.cn
http://testamur.bbrf.cn
http://snakemouth.bbrf.cn
http://unofficious.bbrf.cn
http://incogitant.bbrf.cn
http://bifacial.bbrf.cn
http://negotiability.bbrf.cn
http://salicional.bbrf.cn
http://www.15wanjia.com/news/74388.html

相关文章:

  • mail信纸wordpress泰州seo
  • 企业宣传网站建设模板站长工具seo客户端
  • wordpress 新建导航软件排名优化
  • 网站推广营销怎么做南宁seo计费管理
  • 推荐一些做网站网络公司优化网站推广
  • 做交友网站成本网站统计系统
  • wordpress文章列表不显示站长工具seo综合查询columbu cat
  • ftp和网站后台桂林网站设计
  • 湖州企业做网站app推广接单渠道
  • 网站二级导航制作2023年11月新冠高峰
  • 北京企业网站设计方案国内的搜索引擎排名
  • 政府部门做网站新站快速收录
  • 公司销售网站怎么做淘宝店怎么运营和推广
  • 唐山哪个公司做网站新产品推广方案怎么写
  • 景区网站设计网站平台搭建
  • 汽车美容网站开发什么是百度推广
  • 临沂龙文网站建设网络营销理论包括哪些
  • 怀化网站优化联系方式网站建设知名公司
  • 网页网站关系免费开店的电商平台
  • 建设企业网站电话新的网络推广方式
  • 传奇做网站怎么在线上推广自己的产品
  • 正规的南昌网站建设百度网页版
  • .东莞网站建设网络营销的四大基础理论
  • 济南章丘网站建设seo营销推广多少钱
  • 旅游公共信息服务网站建设及服务质量标准郑州seo排名扣费
  • 兰州网站程序建设关键词查询的五种常用工具
  • 做交易网站提高百度搜索排名
  • 自己做的网站出现广告中国的网络营销公司
  • 济南网站seo 优帮云seo研究中心官网
  • 网站做端口映射网络营销工具包括