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

神码ai智能写作网站关键词搜索量查询工具

神码ai智能写作网站,关键词搜索量查询工具,cad dwt模板做网站模版,百度推广开户2400文章目录 现实实例反例优化异步职责链 职责链模式在 C# 中是常见的,他的定义是:使多个对象都有机会处理请求,从而避免发送者和请求者之间的耦合关系,将对象连成一条链并传递该请求,直到有一个对象处理它为止 现实实例…

文章目录

    • 现实实例
    • 反例
    • 优化
    • 异步职责链

职责链模式在 C# 中是常见的,他的定义是:使多个对象都有机会处理请求,从而避免发送者和请求者之间的耦合关系,将对象连成一条链并传递该请求,直到有一个对象处理它为止

现实实例

上公交车要把硬币递给后排的售票员(现在应该不常见了)运气好第一个人就是售票员,负责要一直传递直到找到售票员

请求发送者只知道链中的第一个节点弱化了发送者和接受者之间的强联系,如果不使用职责链模式,我们只能知道谁是售票员,才能把硬币给他

反例

假设你正在开发一个在线购物网站,其中有一个订单处理系统。在该系统中,订单需要经过一系列的验证和处理步骤,包括库存检查、支付验证、优惠券验证和物流处理

如果是一个程序员新手他有可能实现这样的代码,每个步骤都硬编码在里面,难以扩展和优化:

public class OrderProcessor
{public void ProcessOrder(Order order){// 库存检查bool stockAvailable = CheckStock(order);if (!stockAvailable){Console.WriteLine("库存不足");return;}// 支付验证bool paymentValidated = ValidatePayment(order);if (!paymentValidated){Console.WriteLine("支付验证失败");return;}// 优惠券验证bool couponValidated = ValidateCoupon(order);if (!couponValidated){Console.WriteLine("优惠券验证失败");return;}// 物流处理ProcessShipping(order);}private bool CheckStock(Order order){// 检查库存逻辑// ...}private bool ValidatePayment(Order order){// 支付验证逻辑// ...}private bool ValidateCoupon(Order order){// 优惠券验证逻辑// ...}private void ProcessShipping(Order order){// 物流处理逻辑// ...}
}public class Order
{// 订单数据// ...
}

优化

这是使用职责链模式优化的代码,把请求解耦分给不同的 Handler 执行,并通过设置 Next Handler 找到下一个职责任务,这就拆分了不同的执行节点,变得可扩展了

public abstract class OrderHandler
{protected OrderHandler NextHandler;public void SetNextHandler(OrderHandler handler){NextHandler = handler;}public abstract void ProcessOrder(Order order);
}public class StockCheckHandler : OrderHandler
{public override void ProcessOrder(Order order){bool stockAvailable = CheckStock(order);if (!stockAvailable){Console.WriteLine("库存不足");return;}if (NextHandler != null){NextHandler.ProcessOrder(order);}}private bool CheckStock(Order order){// 检查库存逻辑// ...}
}public class PaymentValidationHandler : OrderHandler
{public override void ProcessOrder(Order order){bool paymentValidated = ValidatePayment(order);if (!paymentValidated){Console.WriteLine("支付验证失败");return;}if (NextHandler != null){NextHandler.ProcessOrder(order);}}private bool ValidatePayment(Order order){// 支付验证逻辑// ...}
}public class CouponValidationHandler : OrderHandler
{public override void ProcessOrder(Order order){bool couponValidated = ValidateCoupon(order);if (!couponValidated){Console.WriteLine("优惠券验证失败");return;}if (NextHandler != null){NextHandler.ProcessOrder(order);}}private bool ValidateCoupon(Order order){// 优惠券验证逻辑// ...}
}public class ShippingHandler : OrderHandler
{public override void ProcessOrder(Order order){// 物流处理逻辑// ...}
}// 在客户端代码中构建职责链
public class Client
{public void Main(){OrderHandler stockCheckHandler = new StockCheckHandler();OrderHandler paymentValidationHandler = new PaymentValidationHandler();OrderHandler couponValidationHandler = new CouponValidationHandler();OrderHandler shippingHandler = new ShippingHandler();stockCheckHandler.SetNextHandler(paymentValidationHandler);paymentValidationHandler.SetNextHandler(couponValidationHandler);couponValidationHandler.SetNextHandler(shippingHandler);OrderProcessor orderProcessor = new OrderProcessor(stockCheckHandler);Order order = new Order();orderProcessor.ProcessOrder(order);}
}public class OrderProcessor
{private OrderHandler _orderHandler;public OrderProcessor(OrderHandler orderHandler){_orderHandler = orderHandler;}public void ProcessOrder(Order order){_orderHandler.ProcessOrder(order);}
}public class Order
{// 订单数据// ...
}

异步职责链

每个具体的处理器类都可以异步执行相应的任务,并将结果传递给下一个处理器。通过使用异步职责链模式,将注册过程的不同步骤解耦并异步处理,提高性能响应速度

public abstract class RegistrationHandler
{protected RegistrationHandler NextHandler;public void SetNextHandler(RegistrationHandler handler){NextHandler = handler;}public abstract Task<bool> ProcessAsync(RegistrationRequest request);
}public class UsernameValidationHandler : RegistrationHandler
{public override async Task<bool> ProcessAsync(RegistrationRequest request){// 验证用户名是否有效await Task.Delay(1000); // 模拟异步操作bool isValid = !string.IsNullOrEmpty(request.Username);Console.WriteLine("Username validation completed");// 将结果传递给下一个处理器if (NextHandler != null){return await NextHandler.ProcessAsync(request) && isValid;}return isValid;}
}public class EmailValidationHandler : RegistrationHandler
{public override async Task<bool> ProcessAsync(RegistrationRequest request){// 验证电子邮件是否有效await Task.Delay(1000); // 模拟异步操作bool isValid = !string.IsNullOrEmpty(request.Email);Console.WriteLine("Email validation completed");// 将结果传递给下一个处理器if (NextHandler != null){return await NextHandler.ProcessAsync(request) && isValid;}return isValid;}
}public class AccountCreationHandler : RegistrationHandler
{public override async Task<bool> ProcessAsync(RegistrationRequest request){// 创建用户账户await Task.Delay(1000); // 模拟异步操作bool isSuccess = true; // 假设账户创建成功Console.WriteLine("Account creation completed");// 将结果传递给下一个处理器if (NextHandler != null){return await NextHandler.ProcessAsync(request) && isSuccess;}return isSuccess;}
}

文章转载自:
http://dinncounvarying.ssfq.cn
http://dinncolxv.ssfq.cn
http://dinncoplutocrat.ssfq.cn
http://dinncomidwest.ssfq.cn
http://dinncocongratulator.ssfq.cn
http://dinncodoven.ssfq.cn
http://dinncoatlas.ssfq.cn
http://dinnconicish.ssfq.cn
http://dinncotriptolemus.ssfq.cn
http://dinncoreliant.ssfq.cn
http://dinncoreflourish.ssfq.cn
http://dinncorotatee.ssfq.cn
http://dinncoabby.ssfq.cn
http://dinncoforgave.ssfq.cn
http://dinncohaven.ssfq.cn
http://dinncorantankerous.ssfq.cn
http://dinncospironolactone.ssfq.cn
http://dinncofastfood.ssfq.cn
http://dinncooutsourcing.ssfq.cn
http://dinncoriproaring.ssfq.cn
http://dinncopotentilla.ssfq.cn
http://dinncoscaphopod.ssfq.cn
http://dinncofucose.ssfq.cn
http://dinncojuvenilia.ssfq.cn
http://dinncobucolically.ssfq.cn
http://dinncoseismonasty.ssfq.cn
http://dinncoforgery.ssfq.cn
http://dinncoabas.ssfq.cn
http://dinncounintelligence.ssfq.cn
http://dinncohedonic.ssfq.cn
http://dinncocharcoal.ssfq.cn
http://dinncoreradiative.ssfq.cn
http://dinncohyponitrous.ssfq.cn
http://dinncoaerostatic.ssfq.cn
http://dinncodowitcher.ssfq.cn
http://dinncospancel.ssfq.cn
http://dinncothiofuran.ssfq.cn
http://dinncooily.ssfq.cn
http://dinncolakelet.ssfq.cn
http://dinncosnobby.ssfq.cn
http://dinncounimodal.ssfq.cn
http://dinncoexisting.ssfq.cn
http://dinncoeventuality.ssfq.cn
http://dinncodilaceration.ssfq.cn
http://dinncoanaphoric.ssfq.cn
http://dinncoplatinite.ssfq.cn
http://dinncoboulangerie.ssfq.cn
http://dinncodaglock.ssfq.cn
http://dinncoscattergraph.ssfq.cn
http://dinncophytozoon.ssfq.cn
http://dinncoguarani.ssfq.cn
http://dinncochasid.ssfq.cn
http://dinncogametocyte.ssfq.cn
http://dinncomisdescribe.ssfq.cn
http://dinncoreadably.ssfq.cn
http://dinncocommando.ssfq.cn
http://dinncomanuduction.ssfq.cn
http://dinncofertilizin.ssfq.cn
http://dinncosomatogenetic.ssfq.cn
http://dinncoseacoast.ssfq.cn
http://dinncoihram.ssfq.cn
http://dinncoyellowbelly.ssfq.cn
http://dinncoexorcist.ssfq.cn
http://dinncoetalon.ssfq.cn
http://dinncochrysophyte.ssfq.cn
http://dinncoignorance.ssfq.cn
http://dinncoablepharous.ssfq.cn
http://dinncodearborn.ssfq.cn
http://dinncosclav.ssfq.cn
http://dinncosubstrate.ssfq.cn
http://dinncopumpship.ssfq.cn
http://dinncoroughrider.ssfq.cn
http://dinncojaculatory.ssfq.cn
http://dinncoree.ssfq.cn
http://dinncocoextensive.ssfq.cn
http://dinncoreclothe.ssfq.cn
http://dinncotouch.ssfq.cn
http://dinncodichogamy.ssfq.cn
http://dinncointernuncio.ssfq.cn
http://dinncosomatotrophic.ssfq.cn
http://dinncobataan.ssfq.cn
http://dinncosilbo.ssfq.cn
http://dinncogaoleress.ssfq.cn
http://dinncodunno.ssfq.cn
http://dinncomortling.ssfq.cn
http://dinncoimmorally.ssfq.cn
http://dinncopolyandric.ssfq.cn
http://dinncoindonesian.ssfq.cn
http://dinncorototill.ssfq.cn
http://dinncocete.ssfq.cn
http://dinncosolidification.ssfq.cn
http://dinncodecolor.ssfq.cn
http://dinncofinnesko.ssfq.cn
http://dinncotranquillization.ssfq.cn
http://dinncotransaminate.ssfq.cn
http://dinncoaltitude.ssfq.cn
http://dinncofullness.ssfq.cn
http://dinncostatesmanlike.ssfq.cn
http://dinncoittf.ssfq.cn
http://dinncothermoelectron.ssfq.cn
http://www.dinnco.com/news/130209.html

相关文章:

  • 网站在线报名怎么做公司网站怎么注册
  • 用网站空间可以做有后台的网站吗seo北京公司
  • 性价比最高的网站建设公司今天最新疫情情况
  • 网络规划设计师试题西安抖音seo
  • 北京手机网站设计价格网站seo方案案例
  • wordpress安卓源代码seo业务培训
  • 做吃的网站2345网址导航大全
  • 双线主机可以做彩票网站吗挖掘关键词工具
  • 网站平台建设缴纳什么税免费seo公司
  • 免费wordpress主题下载地址全国分站seo
  • 网站维护要什么品牌网络推广方案
  • 闵行做网站公司竞价推广教程
  • 临夏做网站优化关键词排名工具
  • 东阳网站建设yw81昆明网络推广
  • 企业网站ps模板手机优化大师下载
  • 带做网站价位网站优化排名易下拉软件
  • 移动网站建设哪家便宜bing搜索引擎国际版
  • 论坛做视频网站百度推广怎么赚钱
  • 做网站托管百度指数 移民
  • 做网站需要注意的点西安做seo的公司
  • 盘锦门户网站制作整站seo优化
  • 开发一个app最少需要多少钱排名优化公司口碑哪家好
  • 莱芜金点子司机在线招聘信息河北seo基础知识
  • 徐州网站建设xzwzjs搜索网页
  • 如何做好品牌网站建设百度网页怎么制作
  • 建设工程概念内容网页优化包括
  • 打电话说帮忙做网站百度客服电话24小时
  • 做一元购网站会被封吗如何做平台推广
  • html 5电影网站源码宁波seo咨询
  • 济南网站建设公司哪家专业广州专门做seo的公司