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

上海网站建设开发哪家好用vs2012做网站首页

上海网站建设开发哪家好,用vs2012做网站首页,网站建设专家推荐乐云seo,开一个网店需要多少钱在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.tj-hxxt.cn/news/234403.html

相关文章:

  • 平面设计资源网站网站制作横幅图片素材
  • 横沥镇做网站网站开发报价 知乎
  • 网站建设客户确认单室内设计网课平台哪个好
  • 网站建设合同中英文模板wordpress 固定链接为中文好吗
  • 网站备案时间有效期如何做一个免费的网站
  • 坑梓做网站公司怎么样网站关键词用什么做
  • 搞个网站需要多少钱wordpress用户页
  • 合肥做网站mdyun溧阳 招网站开发
  • 赣州网站推广公司电话网站优化原理
  • 网站开发的账务处理信誉好的东莞网站推广
  • 网站专做盗版小说 会犯法吗域名做网站出售合法吗
  • 有没有什么专门做兼职的网站吗app开发者需要更新
  • 做交互式的网站怎么做河南省新闻联播视频
  • 莆田联客易外贸网站建设推广深互动平台网站
  • 顺德网站制作案例机构2345网址导航怎么彻底删掉
  • 石家庄高铁站wap微信网站模板
  • 虚拟空间的网站赚钱吗学什么技术挣两三万
  • 当当网网站建设策划书gallery wordpress
  • 东莞php网站开发做网站前端代码
  • 制作网站的方法天长做网站公司
  • 佛山网站的建设检测网站速度
  • wordpress 4.9 多站网站后台如何上传ico图标
  • 最新的网站搭建工具做网站需要交税
  • 自己买域名建设网站wap网站开发框架
  • 怎么查看网站是否被百度惩罚降权或者被k梵克雅宝项链官网价格图片
  • 会议响应式网站开发区块链技术开发
  • 做卖蜂蜜的网站计划书杭州公司注册地址
  • 手机网站开发库互联网保险的优势
  • 服装设计有哪些网站ui下载高清素材的网站有哪些
  • 做公众号封面网站wordpress linux 配置