【无标题】JAVA策略模式代码例子

在Java中,您可以使用面向对象编程中的继承和多态性来实现您的需求。首先,我们定义一个`Good`类,该类包含满减策略和打折策略。然后,我们可以让`Shoe`类和`Cloth`类继承自`Good`类。为了实现不同的折扣或满减策略,可以考虑使用策略模式。

下面是一个简单的实现示例:

1. 定义策略接口

首先,我们需要定义一个策略接口,该接口可以被不同的具体策略实现。

```java

public interface DiscountStrategy {

double applyDiscount(double originalPrice);

}

```

2. 实现具体的策略

接下来,我们实现几个具体的策略,例如满减和打折。

```java

public class FullReductionStrategy implements DiscountStrategy {

private double threshold;

private double reduction;

public FullReductionStrategy(double threshold, double reduction) {

this.threshold = threshold;

this.reduction = reduction;

}

@Override

public double applyDiscount(double originalPrice) {

return originalPrice >= threshold ? originalPrice - reduction : originalPrice;

}

}

public class PercentageDiscountStrategy implements DiscountStrategy {

private double discountRate;

public PercentageDiscountStrategy(double discountRate) {

this.discountRate = discountRate;

}

@Override

public double applyDiscount(double originalPrice) {

return originalPrice * (1 - discountRate);

}

}

```

3. 定义商品基类

现在,我们定义`Good`类,并给它添加一个`DiscountStrategy`类型的字段,以及应用折扣的方法。

```java

public abstract class Good {

protected String name;

protected double price;

protected DiscountStrategy discountStrategy;

public Good(String name, double price, DiscountStrategy discountStrategy) {

this.name = name;

this.price = price;

this.discountStrategy = discountStrategy;

}

public double getPriceAfterDiscount() {

return discountStrategy.applyDiscount(price);

}

// Getters and Setters

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public DiscountStrategy getDiscountStrategy() {

return discountStrategy;

}

public void setDiscountStrategy(DiscountStrategy discountStrategy) {

this.discountStrategy = discountStrategy;

}

}

```

4. 创建子类

最后,创建`Shoe`和`Cloth`类,它们都继承自`Good`类。

```java

public class Shoe extends Good {

public Shoe(String name, double price, DiscountStrategy discountStrategy) {

super(name, price, discountStrategy);

}

}

public class Cloth extends Good {

public Cloth(String name, double price, DiscountStrategy discountStrategy) {

super(name, price, discountStrategy);

}

}

```

5. 使用示例

下面是如何使用这些类的一个简单示例:

```java

public class Main {

public static void main(String\[\] args) {

DiscountStrategy shoeDiscount = new PercentageDiscountStrategy(0.1); // 10% off

DiscountStrategy clothDiscount = new FullReductionStrategy(100, 20); // 满100减20

Shoe shoe = new Shoe("Running Shoe", 150, shoeDiscount);

Cloth cloth = new Cloth("T-shirt", 80, clothDiscount);

System.out.println("Shoe Price After Discount: " + shoe.getPriceAfterDiscount());

System.out.println("Cloth Price After Discount: " + cloth.getPriceAfterDiscount());

}

}

```

这段代码展示了如何通过策略模式灵活地为不同种类的商品设置不同的折扣策略。希望这能帮助您解决问题!如果有任何其他问题或需要进一步的帮助,请随时告诉我。

相关推荐
QianhangQianping10 分钟前
撤销重做不是「存快照」:一个可视化编辑器的Operation 设计
java·前端·数据库·typescript·编辑器·前端架构·撤销重做
海绵宝宝转agent16 分钟前
LeetCodehot100题解
java·笔记·学习·算法·leetcode·深度优先
SunnyDays101117 分钟前
Java 操作 Word 脚注和尾注:添加、读取与删除
java·word
SunnyDays101118 分钟前
Java 为 Word 添加页码:隐藏封面页码、从指定页开始编号、奇偶页页码设置等
java·word·页码
开源推荐官22 分钟前
多门店 O2O 商城选型复盘:从开发视角对比主流开源系统
java·架构·开源·php
吴名氏.23 分钟前
Java转AI Agent教程(第二周)超时重试与错误分类
java·人工智能·python·分类·ai agent
从琳开始9891 小时前
从Socket到UDP回显服务器:Java网络编程完整指南
java·网络·网络协议·udp·java-ee
西瓜拿铁好喝2 小时前
开源和闭源 AI 办公工具,分别适合谁
java·前端·数据库
学长毕设2 小时前
基于SpringBoot的校园礼服租赁系统的设计与实现
java·spring boot·后端
明天也要努力学习2 小时前
深入理解 JVM 垃圾回收(GC)
java·jvm