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

做网站公司排名山东省住房和城乡建设部网站

做网站公司排名,山东省住房和城乡建设部网站,wordpress ak action,儿童摄影网站设计文章目录 一、什么是策略模式二、策略模式的实现原理三、策略模式的应用场景四、策略模式的代码实现 一、什么是策略模式 策略模式是一种行为型设计模式#xff0c;它允许在运行时根据不同的情况选择算法的行为。该模式将算法封装成独立的类#xff0c;并使它们可以互相替换它允许在运行时根据不同的情况选择算法的行为。该模式将算法封装成独立的类并使它们可以互相替换从而使算法的变化独立于使用算法的客户端。 二、策略模式的实现原理 定义一个策略接口首先需要定义一个策略接口该接口声明了算法的方法。实现具体策略类针对不同的算法实现具体的策略类这些类都实现了策略接口并提供了自己的算法实现。在客户端使用策略在客户端代码中创建一个策略对象并将其传递给需要使用算法的对象。客户端代码不需要关心具体的算法实现只需要知道如何使用策略对象即可。运行时动态切换策略由于策略对象是可以相互替换的因此在运行时可以根据需要动态切换策略对象从而改变算法的行为。 通过使用策略模式可以将算法的实现与客户端代码解耦使得算法可以独立变化而不影响客户端代码的稳定性和可维护性。 三、策略模式的应用场景 当一个系统需要在多个算法中选择一个进行使用时可以使用策略模式。例如一个电商平台可能需要根据用户的购买记录选择不同的推荐算法。当一个类有多个行为或算法并且这些行为或算法可能在未来发生变化时可以使用策略模式。通过将每个行为或算法封装成一个策略类可以方便地添加、修改或删除这些行为或算法。当一个系统需要动态地切换算法时可以使用策略模式。例如一个游戏中的角色可能需要根据当前状态选择不同的攻击策略。当一个系统需要对外提供一组可替换的算法时可以使用策略模式。通过定义一个公共接口并让每个策略类实现该接口可以方便地切换不同的算法。 四、策略模式的代码实现 // structure // // strategy // | Context |o--------| Strategy | // |------------------| |--------------------| // |ContextInterface()| |AlgorithmInterface()| // ^ // | // ---------------------------------------------- // | | | // | ConcreteStrategyA | | ConcreteStrategyB | | ConcreteStrategyC | // |--------------------| |--------------------| |--------------------| // |AlgorithmInterface()| |AlgorithmInterface()| |AlgorithmInterface()| // interface Strategy {void AlgorithmInterface(void); }; // class ConcreteStrategyA : public Strategy {public:void AlgorithmInterface(void); }; // void ConcreteStrategyA::AlgorithmInterface(void) {Print(executing algorithm of strategy a (,this,)); } // class ConcreteStrategyB : public Strategy {public:void AlgorithmInterface(void); }; // void ConcreteStrategyB::AlgorithmInterface(void) {Print(executing algorithm of strategy b (,this,)); } // class ConcreteStrategyC : public Strategy {public:void AlgorithmInterface(void); }; // void ConcreteStrategyC::AlgorithmInterface(void) {Print(executing algorithm of strategy c (,this,)); } // class Context {public:Context(Strategy strategy);~Context(void);void ContextInterface(void);protected:Strategy* m_strategy; }; // Context::Context(Strategy strategy) {m_strategy strategy;Print(new strategy , strategy, loaded); } // Context::~Context() {if (CheckPointer(m_strategy) 1)delete m_strategy; } // void Context::ContextInterface(void) {Print(requesting strategy algorithm);m_strategy.AlgorithmInterface(); } // void OnStart() {Context *context;//---strategy acontextnew Context(new ConcreteStrategyA());context.ContextInterface();delete context;//---strategy bcontext new Context(new ConcreteStrategyB());context.ContextInterface();delete context;//---strategy ccontext new Context(new ConcreteStrategyC());context.ContextInterface();delete context; } // output // // new strategy 2097152 loaded // requesting strategy algorithm // executing algorithm of strategy a (2097152) // new strategy 4194304 loaded // requesting strategy algorithm // executing algorithm of strategy b (4194304) // new strategy 6291456 loaded // requesting strategy algorithm // executing algorithm of strategy c (6291456)
http://www.pierceye.com/news/55929/

相关文章:

  • 山东建站管理系统亚洲成品1688进入
  • 什么是大型门户网站对网站开发的理解500字
  • 枣庄市建设项目环评备案网站爱情动作片做网站
  • 足球网站建设意义企业网站建设参考资料
  • 网站怎么注册啊动画制作专业就业前景
  • 营销型网站怎么建设js网站跳转代码
  • 福建城建设厅官方网站公司网站建设费会计处理
  • 深圳建设网站龙岗网站建设网页设计与制作课程思政
  • 建筑公司网站建设方案网站怎么建设
  • 如何搭建一个自己上传视频的网站做微商网站发帖免费教程
  • 建设部标准规范网站成考报名系统入口官网
  • 国外哪个网站做c 挣钱购物网站开发 项目描述
  • 中国企业网站建设网站如何去分析
  • 网站建设好后能修改吗佛山 技术支持 骏域网站建设
  • 网站开发是什重庆网站网络推广推广
  • 汕头网站开发企业查询官网免费查询一下
  • 文明网i中国精神文明建设门户网站seo优化课程
  • 乐从网站建设公司自建的电子网站如何做推广
  • 电子商务网站建设与管理的考试html做网站经验技巧
  • 创建qq网站吗郑州华久做网站
  • 网店怎么做网站seo啥意思怎么做
  • 专业做网站优化需要多久劳务公司网站建设方案
  • 门户网站建设厂商名录苏州seo排名公司
  • 四个平台建设网站不显示图片用群晖nas做网站
  • 怎么查询网站的点击量济南网站优化收费标准
  • 移动端减肥网站模板资源网站后台系统
  • h5响应式网站是什么网店美工课本
  • 城市建设理论研究官方网站十堰做网站的工作室
  • 网站为什么维护中收费电影网站怎么做
  • 搭建网站兼职图片展示网站模板