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

做网站的集群方案室内装修设计自学教材

做网站的集群方案,室内装修设计自学教材,线上商城运营,网站建设有那些内容文章目录 一、概念二、策略模式的生活场景三、适用场景三、角色构成四、业务场景示例总结优点缺点 一、概念 策略模式#xff08;Strategy Pattern)又叫政策模式#xff08;Policy Pattern),属于行为型模式。通过面向对象的继承和多态机制#xff0c;从而实现同一行为在不同… 文章目录 一、概念二、策略模式的生活场景三、适用场景三、角色构成四、业务场景示例总结优点缺点 一、概念 策略模式Strategy Pattern)又叫政策模式Policy Pattern),属于行为型模式。通过面向对象的继承和多态机制从而实现同一行为在不同场景下具备不同实现。 二、策略模式的生活场景 个人的交税率与他的工资有关。支付方式的选择微信支付支付宝支付银联支付。 三、适用场景 针对同一类型问题有多种处理方式每一种都能独立解决问题算法需要自由切换的场景需要屏蔽算法规则的场景。 三、角色构成 上下文角色Context): 用来操作策略的上下文环境屏蔽高层模块客户端对策略算法的直接访问封装可能存在的变化 抽象策略角色Strategy): 规定策略或算法的行为 具体策略角色ConcreteStrategy): 具体的策略或算法实现 四、业务场景示例 优惠活动 创建促销粗略对象PromotionStrategy /*** p* 促销策略对象* /p** author shiqi* version 1.0.0* createTime 2023-11-16*/ public interface IPromotionStrategy {/*** 执行促销操作*/void doPromotion(); } 创建优惠券抵扣策略对象 /*** p* 优惠券策略* /p** author shiqi* version 1.0.0* createTime 2023-11-16*/ public class CouponStrategy implements IPromotionStrategy{Overridepublic void doPromotion() {System.out.println(使用优惠券抵扣);} }创建返现促销策略对象CashbackStrategy /*** p* 返现策略* /p** author shiqi* version 1.0.0* createTime 2023-11-16*/ public class CashbackStrategy implements IPromotionStrategy{Overridepublic void doPromotion() {System.out.println(返现直接打款到支付宝帐号);} }创建拼团优惠策略对象 /*** p* 团购策略* /p** author shiqi* version 1.0.0* createTime 2023-11-16*/ public class GroupbuyStrategy implements IPromotionStrategy{Overridepublic void doPromotion() {System.out.println(5人成团可以优惠);} }无优惠策略对象 /*** p* 无优惠* /p** author shiqi* version 1.0.0* createTime 2023-11-16*/ public class EmptyStrategy implements IPromotionStrategy{Overridepublic void doPromotion() {System.out.println(无优惠);} }创建促销活动方案 /*** p* 促销活动方案* /p** author shiqi* version 1.0.0* createTime 2023-11-16*/ public class PromotionActivity {private IPromotionStrategy promotionStrategy;SuppressWarnings(all)public PromotionActivity(IPromotionStrategy promotionStrategy) {this.promotionStrategy promotionStrategy;}/*** 执行促销策略*/public void executePromotionStrategy() {promotionStrategy.doPromotion();}}编写客户端测试类 public class Test {public static void main(String[] args) {PromotionActivity promotionActivitynull;// 获取促销策略String promotionKey COUPON;if(StringUtils.equals(promotionKey,COUPON)){promotionActivitynew PromotionActivity(new CouponStrategy());}else if(StringUtils.equals(promotionKey,CASHBACK)){promotionActivitynew PromotionActivity(new CashbackStrategy());}//....promotionActivity.executePromotionStrategy();} }此客户端弊端在于if else太多了随着业务的扩展只会越来越臃肿所以需要改造。 创建促销策略工厂类 public class PromotionStrategyFactory {public static MapString,IPromotionStrategy PROMOTION_STRATEGY_MAP new HashMap();static {PROMOTION_STRATEGY_MAP.put(PromotionKey.EMPTY,new EmptyStrategy());PROMOTION_STRATEGY_MAP.put(PromotionKey.COUPON,new CouponStrategy());PROMOTION_STRATEGY_MAP.put(PromotionKey.CASHBACK,new CashbackStrategy());PROMOTION_STRATEGY_MAP.put(PromotionKey.GROUPBUY,new GroupbuyStrategy());}public interface PromotionKey{String EMPTY EMPTY;String COUPON COUPON;String CASHBACK CASHBACK;String GROUPBUY GROUPBUY;}/*** 获取促销策略的键集合** return 促销策略的键集合*/public static SetString getPromotionStrategyKeys(){return PROMOTION_STRATEGY_MAP.keySet();}/*** 根据促销码获取促销策略* param promotionKey 促销码* return 促销策略*/public static IPromotionStrategy getPromotionStrategy(String promotionKey){return PROMOTION_STRATEGY_MAP.get(promotionKey);}}改写客户端 public class Test {public static void main(String[] args) {// 获取促销策略String promotionKey COUPON;IPromotionStrategy promotionStrategy PromotionStrategyFactory.getPromotionStrategy(promotionKey);promotionStrategy.doPromotion();} }总结 优点 策略模式符合开闭原则避免使用多重条件转移语句如if…else…语句switch语句使用策略模式可以提高算法的保密性和安全性。 缺点 客户端必须知道所有的策略并且自行决定使用哪一个策略类。代码中会产生非常多策略类增加维护难度。
http://www.zqtcl.cn/news/179664/

相关文章:

  • 零基础学pytho 网站开发餐饮公司最好的网站建设
  • 品牌网站建设 蝌蚪5小微信分销怎么做
  • 二级域名建站虚拟主机与云服务器的区别
  • 如何安装网站模板文件网站维护具体做啥
  • 怎么建设官方网站登封网络推广公司
  • 苏州画廊网站建设vs2015 建设微网站
  • 海南网站建设及维护自己创建网站403
  • 网站推广的意义怎样把建好的网站上传到互联网
  • 王店镇建设中学网站seo搜索排名优化是什么意思
  • 北京哪家网站建设公司比较好js页面下载wordpress
  • 网站开发组岗位建设银行官网网站人事
  • 找公司做网站运营怎么样百度推广代运营
  • flask做克隆网站网站放到云服务器上怎么做
  • 有网站怎样做推广精品网站源码资源程序下载
  • 怎么建设淘宝联盟的网站梧州网站设计公司
  • 注册查询官方网站网站建设pad版本是什么
  • 做网站先得注册域名吗网站cdn+自己做
  • 甘肃省建设厅网站非织梦做的网站能仿吗
  • 天元建设集团网站苏州门户网站建设
  • 建设网站需要学习什么语言福州优化搜索引擎
  • 网站开发大致多少钱手机上怎么制作网站吗
  • 重庆网站seo营销模板wordpress学习 知乎
  • 桃子网站logowordpress post meta
  • 做网站一般需要什么青岛网络推广
  • 东莞网站建设 光龙wordpress4.6 nodejs
  • 宁海县建设局网站网站建设行业前景
  • 2003网站的建设谷歌seo新手快速入门
  • 网站建设服务开发网页制作下载链接怎么做
  • 网站更改域名河源建网站
  • 陕西培训网站建设校园网站建设目的