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

做设计有哪些接私活的网站广州微信网站开发

做设计有哪些接私活的网站,广州微信网站开发,coding wordpress博客,室内设计小白怎么入行在Java中,策略模式(Strategy Design Pattern)用于定义一系列算法,并将每个算法封装成单独的类,使得它们可以互相替换,让客户端在使用算法时不需要知道具体的实现细节。策略模式是一种行为型设计模式&#x…

在Java中,策略模式(Strategy Design Pattern)用于定义一系列算法,并将每个算法封装成单独的类,使得它们可以互相替换,让客户端在使用算法时不需要知道具体的实现细节。策略模式是一种行为型设计模式,它允许客户端选择要使用的算法,而无需修改客户端代码。

策略模式包含以下主要角色:

  1. 环境类(Context):用于维护一个对策略对象的引用,并在需要时调用策略对象的方法。

  2. 策略接口(Strategy):定义一个通用的算法接口,所有具体策略类都要实现这个接口。

  3. 具体策略类(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.yayakq.cn/news/158568/

相关文章:

  • 企业网站建设套餐 网络服务温岭专业营销型网站建设地址
  • 湖北 个人网站备案时间网络编程技术基础
  • 17zwd一起做业网站wordpress响应时间长
  • 2k屏幕的网站怎么做泉州企业自助建站系统
  • 服装订单接单网站凡科互动网页版
  • 网站开发全科班怎么做网站出肉狗
  • 网站建设客户需要提供什么科目windows优化大师是系统软件吗
  • 北京建站公司推荐首推万维科技2022最火营销方案
  • 深圳网站建设黄浦网络-骗子黑龙江微信网站开发
  • 10m光纤做网站做网站公司苏州
  • 下载素材第三方网站是怎么做进去了
  • 微信互动平台网站建设做设计网站
  • 什么网站可以做pie chart网站建设与维护试题含答案
  • 新手学做网站手机施工企业自建自用的工程可以不进行招标是否正确
  • 会泽做网站seo网站排名全选
  • 西安网络公司网站建设免费客户销售管理软件
  • 怎样制作网站平台重庆整站优化的电话销售
  • 网站开发实用技术第2版文档怎么做自己的网站平台
  • 网站婚礼服务态网站建设论文新开传奇网站大全
  • 汕头网站优化哪家好网站官网建设的价格
  • 建设网站域名多用户版商城系统
  • 网站的结构快速提升关键词排名软件
  • 用jsp做的网站代码中咨城建设计有限公司 网站
  • 安康微网站建设西宁大型网站建设
  • 东阳哪里可以做网站ui设计培训多长时间能学会
  • 如何修改网站模板地推网
  • php做的网站h5app开发
  • 做签名照的网站成都自适应网站建设
  • 容县建设工程交易中心网站个人网站设计论文范文
  • 河北建站科技网络公司中国企业信息公示网登录