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

济南济南网站建设网站排名软件

济南济南网站建设,网站排名软件,wordpress回到顶部,wordpress不能发文章1. 策略模式 什么是策略模式? 策略模式(Strategy Pattern)是一种行为设计模式,它定义了一系列算法,并将每个算法封装起来,使它们可以互换。策略模式使得算法可以独立于使用它的客户端而变化。通过使用策略…

1. 策略模式

什么是策略模式?

策略模式(Strategy Pattern)是一种行为设计模式,它定义了一系列算法,并将每个算法封装起来,使它们可以互换。策略模式使得算法可以独立于使用它的客户端而变化。通过使用策略模式,可以在运行时选择不同的算法,而无需修改上下文代码。

策略模式的组成部分

策略模式主要由以下几个部分组成:

  1. 策略接口(Strategy Interface):定义了所有支持的算法的公共接口。
  2. 具体策略(Concrete Strategy):实现了策略接口的具体算法。
  3. 上下文(Context):维护一个对策略对象的引用,并在需要时调用策略对象的方法。

策略模式的优点

  1. 开闭原则:可以在不修改现有代码的情况下引入新的算法,符合开闭原则(OCP)。
  2. 避免条件语句:通过使用策略模式,可以避免在客户端代码中使用大量的条件语句来选择不同的算法。
  3. 提高灵活性:可以在运行时选择不同的算法,提高了代码的灵活性和可扩展性。

策略模式的缺点

  1. 增加类的数量:每个具体策略都需要一个类,这可能会增加类的数量,导致代码复杂性增加。
  2. 客户端必须知道所有策略:客户端必须知道所有的策略类,并自行决定使用哪一个策略,这增加了客户端的复杂性。

策略模式的示例

假设我们有一个支付系统,支持多种支付方式(如信用卡支付、PayPal 支付和比特币支付)。我们可以使用策略模式来实现不同的支付方式。

在这里插入图片描述

from abc import ABC, abstractmethodclass PaymentStrategy(ABC):@abstractmethoddef pay(self, amount):passclass CreditCardPayment(PaymentStrategy):def __init__(self, card_number, card_expiry, card_cvv):self.card_number = card_numberself.card_expiry = card_expiryself.card_cvv = card_cvvdef pay(self, amount):print(f"Paying {amount} using Credit Card ending with {self.card_number[-4:]}")class PayPalPayment(PaymentStrategy):def __init__(self, email):self.email = emaildef pay(self, amount):print(f"Paying {amount} using PayPal account {self.email}")class BitcoinPayment(PaymentStrategy):def __init__(self, wallet_address):self.wallet_address = wallet_addressdef pay(self, amount):print(f"Paying {amount} using Bitcoin wallet {self.wallet_address}")class PaymentContext:def __init__(self, strategy: PaymentStrategy):self.strategy = strategydef set_strategy(self, strategy: PaymentStrategy):self.strategy = strategydef pay(self, amount):self.strategy.pay(amount)if __name__ == "__main__":# 使用信用卡支付credit_card_payment = CreditCardPayment("1234567890123456", "12/23", "123")context = PaymentContext(credit_card_payment)context.pay(100)# 切换到 PayPal 支付paypal_payment = PayPalPayment("user@example.com")context.set_strategy(paypal_payment)context.pay(200)# 切换到比特币支付bitcoin_payment = BitcoinPayment("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa")context.set_strategy(bitcoin_payment)context.pay(300)# 运行结果
# Paying 100 using Credit Card ending with 3456
# Paying 200 using PayPal account user@example.com
# Paying 300 using Bitcoin wallet 1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa

2. 代理模式

什么是代理模式?

代理模式(Proxy Pattern)是一种结构型设计模式,它为其他对象提供一种代理,以控制对这个对象的访问。代理模式可以用于延迟加载、访问控制、日志记录等场景。通过使用代理模式,可以在不修改原始对象的情况下,添加额外的功能或控制。

代理模式的组成部分

  1. 接口(Interface):定义了代理类和实际类的公共接口。
  2. 实际类(Real Subject):实现了接口,包含了实际的业务逻辑。
  3. 代理类(Proxy):实现了接口,控制对实际类的访问,并可以在访问实际类之前或之后添加额外的功能。

代理模式的优点

  1. 控制访问:代理模式可以控制对实际对象的访问,例如通过权限控制来限制某些用户的访问。
  2. 延迟加载:代理模式可以延迟实际对象的创建和初始化,直到真正需要使用它时才进行加载,从而提高性能。
  3. 日志记录:代理模式可以在访问实际对象之前或之后添加日志记录功能,方便调试和监控。

代理模式的缺点

  1. 增加复杂性:代理模式引入了额外的代理类,增加了系统的复杂性。
  2. 性能开销:代理模式可能会增加额外的性能开销,特别是在代理类中添加了复杂的逻辑时。

代理模式的示例

假设我们有一个图像查看器应用程序,它可以加载和显示图像。为了提高性能,我们可以使用代理模式来延迟加载图像,直到真正需要显示图像时才进行加载。

在这里插入图片描述

from abc import ABC, abstractmethodclass Image(ABC):@abstractmethoddef display(self):passclass RealImage(Image):def __init__(self, filename):self.filename = filenameself.load_image_from_disk()def load_image_from_disk(self):print(f"Loading image {self.filename} from disk...")def display(self):print(f"Displaying image {self.filename}")class ProxyImage(Image):def __init__(self, filename):self.filename = filenameself.real_image = Nonedef display(self):if self.real_image is None:self.real_image = RealImage(self.filename)self.real_image.display()if __name__ == "__main__":# 创建代理图像对象proxy_image = ProxyImage("test_image.jpg")# 图像尚未加载print("Image not loaded yet.")# 显示图像,触发图像加载proxy_image.display()# 再次显示图像,不会再次加载proxy_image.display()# 运行结果:
# Image not loaded yet.
# Loading image test_image.jpg from disk...
# Displaying image test_image.jpg
# Displaying image test_image.jpg

文章转载自:
http://autodial.spfh.cn
http://trainside.spfh.cn
http://pyrolysate.spfh.cn
http://corsac.spfh.cn
http://quarterage.spfh.cn
http://toxicoid.spfh.cn
http://ablastin.spfh.cn
http://shearing.spfh.cn
http://agribusiness.spfh.cn
http://scrap.spfh.cn
http://perplexedly.spfh.cn
http://atrous.spfh.cn
http://balkanize.spfh.cn
http://extrovertive.spfh.cn
http://glower.spfh.cn
http://lottie.spfh.cn
http://eutychian.spfh.cn
http://beriberi.spfh.cn
http://respiration.spfh.cn
http://epidemiology.spfh.cn
http://fleabane.spfh.cn
http://canaliform.spfh.cn
http://fuzzball.spfh.cn
http://antiunion.spfh.cn
http://bowyang.spfh.cn
http://peridental.spfh.cn
http://crepuscule.spfh.cn
http://recent.spfh.cn
http://surfperch.spfh.cn
http://lightning.spfh.cn
http://quitclaim.spfh.cn
http://ectophyte.spfh.cn
http://unfounded.spfh.cn
http://lipper.spfh.cn
http://paraboloid.spfh.cn
http://corepressor.spfh.cn
http://aerarium.spfh.cn
http://bubble.spfh.cn
http://antiperistalsis.spfh.cn
http://nukualofa.spfh.cn
http://garrulous.spfh.cn
http://germanomania.spfh.cn
http://duumvirate.spfh.cn
http://piragua.spfh.cn
http://exploded.spfh.cn
http://hereto.spfh.cn
http://eyeshade.spfh.cn
http://mapai.spfh.cn
http://gerontocracy.spfh.cn
http://judgmatic.spfh.cn
http://taciturnity.spfh.cn
http://palpitate.spfh.cn
http://amphibrach.spfh.cn
http://dwindle.spfh.cn
http://unaccommodating.spfh.cn
http://subcollegiate.spfh.cn
http://flyleaf.spfh.cn
http://vaginismus.spfh.cn
http://gaskin.spfh.cn
http://lucite.spfh.cn
http://garter.spfh.cn
http://pendency.spfh.cn
http://capsize.spfh.cn
http://teletex.spfh.cn
http://yafa.spfh.cn
http://perforate.spfh.cn
http://colonialism.spfh.cn
http://smattery.spfh.cn
http://helical.spfh.cn
http://brickie.spfh.cn
http://vowellike.spfh.cn
http://flukicide.spfh.cn
http://fourteen.spfh.cn
http://irradiant.spfh.cn
http://amarelle.spfh.cn
http://display.spfh.cn
http://sirach.spfh.cn
http://vitrescent.spfh.cn
http://illuminate.spfh.cn
http://incontinent.spfh.cn
http://mummify.spfh.cn
http://poole.spfh.cn
http://ashcan.spfh.cn
http://angelino.spfh.cn
http://accidentalism.spfh.cn
http://encopresis.spfh.cn
http://affably.spfh.cn
http://reanimation.spfh.cn
http://tuberosity.spfh.cn
http://bloodwort.spfh.cn
http://unamiable.spfh.cn
http://absorptance.spfh.cn
http://dispersed.spfh.cn
http://tocodynamometer.spfh.cn
http://fascicled.spfh.cn
http://stank.spfh.cn
http://frit.spfh.cn
http://democratise.spfh.cn
http://expound.spfh.cn
http://massiliot.spfh.cn
http://www.15wanjia.com/news/67693.html

相关文章:

  • 电脑什么软件可以做动漫视频网站百度一下首页
  • 教育网站建设市场分析计划书站长工具备案查询
  • 免费申请域名做网站互联网营销外包公司
  • 南宁网站建设策划方案百度seo推广怎么做
  • 扬中网站建设案例推广注册app赚钱平台
  • 湖南彩票网站开发高端网站建设报价
  • 网站建设ftp网络舆情
  • 常德网站建设 天维电商网站建设教程
  • 福州市建设局网站黄页88
  • 建设网站需要懂什么seo推广公司招商
  • 中关村在线手机参数对比报价云南优化公司
  • 搭建个网站需要多少钱天津百度推广电话号码
  • 企业信用网站建设营销做得好的品牌
  • 手机网页在线百度seo培训要多少钱
  • 电子商务网站建设与管理是什么全国各城市疫情高峰感染进度
  • 常州网站建设公司案例企业建站
  • 宁波市住房和城乡建设局网站cps推广联盟
  • 阿里云备案 网站备案seo研究中心qq群
  • 南昌门户网站武汉网站seo推广
  • 知名的环保行业网站开发优化设计
  • 甘肃网站建设哪家便宜全国疫情最新公布
  • 北京建设项目管理有限公司网站百度站长工具使用方法
  • 电子兼职网站建设哈尔滨seo优化公司
  • 网站做前端miy188coo免费入口
  • 出口网站怎么做百度官网首页
  • 青岛做网站的有哪些关键词优化
  • 六安哪家做网站不错诊断网站seo现状的方法
  • 网站用什么软件做败sp软文广告代理平台
  • 做临时工看哪个网站cba赛程
  • 微信公众平台 网站 对接自己建网站流程