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

威海网站开发公司qq群推广网站

威海网站开发公司,qq群推广网站,商城网站建设多少钱,wordpress评论贴图策略模式 1.策略接口 定义一组算法或操作的通用接口,通常是一个抽象类或接口。该接口声明了策略类所必须实现的方法。 示例: class Strategy {doOperation() {} }2.具体策略 实现策略接口,提供具体的算法实现。每个具体策略类负责处理一…

策略模式

1.策略接口

定义一组算法或操作的通用接口,通常是一个抽象类或接口。该接口声明了策略类所必须实现的方法。

示例:

class Strategy {doOperation() {}
}
2.具体策略

实现策略接口,提供具体的算法实现。每个具体策略类负责处理一种特定的情况或算法。它包含了实际的业务逻辑。

示例:

class ConcreteStrategyA extends Strategy {doOperation() {// 具体的算法实现}
}class ConcreteStrategyB extends Strategy {doOperation() {// 具体的算法实现}
}
3.上下文

维持一个对策略对象的引用,负责将客户端的请求委派给具体的策略。上下文通常会包含一个接口,使得客户端可以动态地切换策略。

示例:

class Context {constructor(strategy) {this.strategy = strategy;}setStrategy(strategy) {this.strategy = strategy;}executeStrategy() {this.strategy.doOperation();}
}
分数等级的案例:

假设我们有一个评分系统,根据用户的得分将其分为不同的等级。我们可以使用策略模式来实现这个例子,根据用户得分的不同区间采用不同的评级策略。以下是一个简单的JavaScript实现:

// 策略接口
class GradeStrategy {getGrade(score) {}
}// 具体策略 - 优秀
class ExcellentGradeStrategy extends GradeStrategy {getGrade(score) {if (score >= 90 && score <= 100) {return '优秀';}return null; // 不在该策略范围内返回null}
}// 具体策略 - 良好
class GoodGradeStrategy extends GradeStrategy {getGrade(score) {if (score >= 80 && score < 90) {return '良好';}return null;}
}// 具体策略 - 及格
class PassGradeStrategy extends GradeStrategy {getGrade(score) {if (score >= 60 && score < 80) {return '及格';}return null;}
}// 上下文
class ScoreContext {constructor() {this.strategies = [];}addStrategy(strategy) {this.strategies.push(strategy);}getGrade(score) {for (const strategy of this.strategies) {const grade = strategy.getGrade(score);if (grade) {return grade;}}return '不及格'; // 默认策略}
}// 使用策略模式
const context = new ScoreContext();const excellentStrategy = new ExcellentGradeStrategy();
const goodStrategy = new GoodGradeStrategy();
const passStrategy = new PassGradeStrategy();context.addStrategy(excellentStrategy);
context.addStrategy(goodStrategy);
context.addStrategy(passStrategy);console.log(context.getGrade(95)); // 输出: 优秀
console.log(context.getGrade(85)); // 输出: 良好
console.log(context.getGrade(70)); // 输出: 及格
console.log(context.getGrade(55)); // 输出: 不及格
http://www.hrbkazy.com/news/16986.html

相关文章:

  • 易思企业网站管理系统搜索引擎网站优化推广
  • 简单的小公司企业简介seo是搜索引擎营销
  • 国外做滑板网站国内seo公司
  • 网站站内优化怎么做百度怎么优化网站关键词
  • 网站后台banner国家培训网官网
  • 网站做seo推广网站数据
  • 家教网站怎么做世界足球排名前十名
  • pc端和移动端的网站区别是什么意思百度的合作网站有哪些
  • 做一元购网站 要多少钱百度首页排名优化公司
  • 自己做自己的私人网站seo优化费用
  • 网站建设添加展示栏seo管理是什么
  • 昆明网站建设平台网站快速收录的方法
  • 福州建设注册中心网站品牌营销做得好的品牌有哪些
  • 30个让人兴奋的视差滚动网站代写软文
  • 网站开发软文全球十大网站排名
  • 做网站每页面费用线上培训课程
  • wordpress 首页 修改一键关键词优化
  • 关于做摄影网站郑州seo外包顾问
  • 怎么做进入网站js特效朝阳seo
  • 深圳网页设计公司谷歌优化培训
  • 做网站发布信息bittorrentkitty磁力猫
  • 做电影网站怎么盈利网页自动点击软件
  • 做眼镜网站草图有什么原则游戏app拉新平台
  • 常见的建站工具网络推广的主要内容
  • 宝鸡网站制作公司爱站网seo工具包
  • 如何将网站内容做chm有免费做网站的吗
  • 秦皇岛手机网站制作费用第一设计
  • 做网站封面素材图自助建站系统模板
  • 长沙免费模板建站体验营销理论
  • 国内做seo公司简介seo项目