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

网站后台公告id修改高明建网站服务

网站后台公告id修改,高明建网站服务,中国营销在线,成功的网络营销案例有哪些在Java中#xff0c;策略模式#xff08;Strategy Design Pattern#xff09;用于定义一系列算法#xff0c;并将每个算法封装成单独的类#xff0c;使得它们可以互相替换#xff0c;让客户端在使用算法时不需要知道具体的实现细节。策略模式是一种行为型设计模式#x…在Java中策略模式Strategy Design Pattern用于定义一系列算法并将每个算法封装成单独的类使得它们可以互相替换让客户端在使用算法时不需要知道具体的实现细节。策略模式是一种行为型设计模式它允许客户端选择要使用的算法而无需修改客户端代码。 策略模式包含以下主要角色 环境类Context用于维护一个对策略对象的引用并在需要时调用策略对象的方法。 策略接口Strategy定义一个通用的算法接口所有具体策略类都要实现这个接口。 具体策略类ConcreteStrategy实现策略接口具体定义了不同的算法实现。 下面是一个简单的示例 假设有一个商场销售系统可以根据不同的促销策略计算折扣。 首先我们创建一个策略接口Strategy public interface DiscountStrategy {     double calculateDiscount(double amount); } 然后我们创建不同的具体策略类ConcreteStrategy来实现不同的促销策略 public class RegularCustomerStrategy implements DiscountStrategy {     Override     public double calculateDiscount(double amount) {         return amount * 0.05; // 5% discount for regular customers     } } public class VIPCustomerStrategy implements DiscountStrategy {     Override     public double calculateDiscount(double amount) {         return amount * 0.1; // 10% discount for VIP customers     } } public class SuperVIPCustomerStrategy implements DiscountStrategy {     Override     public double calculateDiscount(double amount) {         return amount * 0.15; // 15% discount for super VIP customers     } } 接下来我们创建环境类Context用于维护策略对象的引用并在需要时调用策略对象的方法 public class ShoppingCart {     private DiscountStrategy discountStrategy; public void setDiscountStrategy(DiscountStrategy discountStrategy) {         this.discountStrategy discountStrategy;     } public double calculateTotalWithDiscount(double amount) {         if (discountStrategy null) {             throw new IllegalStateException(Discount strategy not set!);         }         double discountAmount discountStrategy.calculateDiscount(amount);         return amount - discountAmount;     } } 现在我们可以使用策略模式来计算不同促销策略下的折扣。客户端只需设置合适的策略而不需要直接处理不同的算法细节 public class Main {     public static void main(String[] args) {         ShoppingCart shoppingCart new ShoppingCart(); shoppingCart.setDiscountStrategy(new RegularCustomerStrategy());         double regularCustomerTotal shoppingCart.calculateTotalWithDiscount(100);         System.out.println(Total amount for regular customer: $ regularCustomerTotal); shoppingCart.setDiscountStrategy(new VIPCustomerStrategy());         double vipCustomerTotal shoppingCart.calculateTotalWithDiscount(100);         System.out.println(Total amount for VIP customer: $ vipCustomerTotal); shoppingCart.setDiscountStrategy(new SuperVIPCustomerStrategy());         double superVIPCustomerTotal shoppingCart.calculateTotalWithDiscount(100);         System.out.println(Total amount for Super VIP customer: $ superVIPCustomerTotal);     } } 输出结果 Total amount for regular customer: $95.0 Total amount for VIP customer: $90.0 Total amount for Super VIP customer: $85.0 这个例子展示了策略模式的实现方式。通过创建策略接口、具体策略类和环境类并在环境类中维护策略对象的引用客户端可以选择不同的算法策略而无需直接处理算法细节。策略模式可以使得算法的选择和使用变得灵活和可扩展同时也有助于将算法与客户端代码解耦。
http://www.w-s-a.com/news/68812/

相关文章:

  • c 网站开发框架品牌策划方案范文
  • 儿童摄影作品网站多元网络兰州网站建设
  • 电脑上不了建设厅网站常德网站建设费用
  • 做单页免费模板网站最新办公室装修风格效果图
  • 中国铁路建设投资公司网站熊学军想开网站建设公司
  • 优化一个网站多少钱网站开发北京
  • html教学关键词优化价格
  • 黄冈论坛网站有哪些给wordpress首页添加公告栏
  • 初中做数学题的网站做淘宝必备网站
  • 买拆车件上什么网站谁有那种手机网站
  • 一家专做有机蔬菜的网站万户网络是干嘛的
  • 十堰百度网站建设八宝山做网站公司
  • 地区电商网站系统建筑施工图纸培训班
  • 网站外包维护一年多少钱医院网站 功能
  • 电子商务市场的发展前景seo推广平台服务
  • 乐清网页设计公司哪家好seo推广任务小结
  • 360建筑网是什么pc优化工具
  • 越秀免费网站建设风景区网站建设项目建设可行性
  • 网站建站公司一站式服务学校网站开发招标
  • asp.net mvc 5 网站开发之美电商网站 流程图
  • 室内设计素材网站推荐郑州专业做淘宝网站建设
  • 新建的网站怎么做seo优化模板规格尺寸及价格
  • 平湖网站设计做电子元器件销售什么网站好
  • 可视化网站模板我想建个网站网站怎么建域名
  • 达州网站建设qinsanw南京市建设发展集团有限公司网站
  • django 网站开发实例公司排行榜
  • 韩国做美食网站阳江网站建设 公司价格
  • 网站开发哪里接业务长春高端模板建站
  • 深圳网站制作公司方案dw一个完整网页的代码
  • asp手机网站源码下载做seo推广网站