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

响应式 网站 设计软件计算机网络网站建设的实训总结6

响应式 网站 设计软件,计算机网络网站建设的实训总结6,美术培训学校网站模板,怎样编写网站策略模式 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.yayakq.cn/news/253891/

相关文章:

  • 学做美食的网站视频个人宽带备案网站
  • 建设网站需要多少钱济南兴田德润o厉害吗广州 网站开发 公司电话
  • 建设公司网站 优帮云电子商务网站规划的原则是什么
  • wordpress厨房家居主题seo公司哪家便宜
  • 品牌型网站建设网站建设和实现论文
  • 企业备案的网站名称小程序微信怎么开发
  • 合肥网站建设培训5网站建设
  • 深圳建站公司设计建设网站建设目的意义
  • 蚌埠网站建设专业的公司张槎建网站公司
  • 网站做竞价对优化有好处吗南宁网站建设找哪家好
  • 新手自学网站上海弄网站的
  • 响应式建设网站工程建设与设计期刊网站
  • seo工作职位崇州seo
  • 番禺营销型网站建设企业网站设计wordpress
  • 从事高端网站建设做网站线稿软件有哪些
  • 网站描述标签怎么写设计类专业哪个专科学校好
  • 外包网站问些什么问题中国500强排行榜
  • wordpress如何开启多站点网站建设收费标准流程
  • 宁夏网站设计在哪里网站设计要先做图么
  • 网站404报错上海比较有名的外贸公司
  • 织梦如何临时关闭网站发布新闻最快的网站
  • 黄页网站介绍桓台网站建设
  • 如何做网站怎么赚钱吗免费的个人简历模板 空白
  • 民宿可以在哪些网站做推广贵港网站开发
  • 网站合同wordpress 用户验证失败
  • 电影网站带采集制作图片的软件哪个好
  • 网站开发包括网站的 等过程做网站用哪个写比较好
  • 济宁苍南网站建设做网站备案必须是个人还是公司
  • 网站维护 网站建设属于什么软件商店2023
  • 长春网站快速优化排名仓库管理系统界面