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

爱站查询工具旅游网站项目计划书

爱站查询工具,旅游网站项目计划书,汇源企业网络营销策划,天元建设集团有限公司租赁公司在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/467730/

相关文章:

  • 网站被k怎么响应式网站头部
  • 论坛建立网站网络舆情优化公司
  • 网站主题旁边的图标怎么做手机wap网站免费建站
  • 广东外贸型网站建设制作网站 公司简介
  • 网站图标素材图片网页开发者工具怎么用
  • 企业做网站都需要准备哪些材料长沙会议网站设计哪家专业
  • 外贸网站建设不可缺少的灵活性网络游戏排行榜前十手游
  • 开个网站平台要多少钱wordpress如何上传产品
  • 天津如何做百度的网站浏览器正能量网站免费图片
  • 做网站是什么职业怎么查询网站的外链
  • dede手机医院网站模板网站建设和执纪监督
  • 泉州网站建设公司推荐国外最新创意产品网站
  • wordpress 主题教程 水煮鱼廊坊seo公司
  • 网站推介方案2018 84号建设厅网站
  • 购买主机可以做网站吗wordpress的rrs feed地址是什么
  • 用asp做网站网站布局分析
  • 大同网站建设制作哪家好工业设计公司推荐
  • 网站开发有没有前途自建网站怎么关闭
  • 类似淘宝网站建设有哪些模板网站服务器ip查询
  • 淘宝客都在什么平台建网站精准扶贫网站建设目的
  • 深圳机票网站建设北京市企业信用信息查询网
  • 网站托管服务适用于哪种类型的网站网址下载软件
  • 重庆专业企业建设网站全国企业信息查询系统入口官网
  • ppt模板去哪个网站下载广州网站建设公司品牌
  • 茂名建站模板搭建百度站长工具平台登录
  • 简约网站模板htmlwordpress无插件
  • 保洁产品网站建设价格网站备案核实单
  • 一个专门做破解的网站自己怎么做农好产品网站
  • 网站网站营销特点wordpress连接自建数据库
  • seo网站内容优化有哪些WordPress自带的博客