可以给一个具体的实例吗
时间: 2024-03-17 17:43:28 浏览: 22
好的,我来举一个简单的例子。假设我们有一个电商网站,需要针对不同的用户计算折扣价格。我们可以使用策略模式来实现这个功能。
首先,我们定义一个折扣计算的接口DiscountStrategy,包含一个calculateDiscount方法,用于计算折扣价格。然后,我们定义不同的折扣策略类,如VIP用户折扣策略类、普通用户折扣策略类、新用户折扣策略类等。这些策略类都实现了DiscountStrategy接口,并且实现了自己的calculateDiscount方法。
在程序运行时,我们根据用户的类型选择不同的折扣策略类,来计算折扣价格。例如,当用户登录时,我们可以根据用户的VIP等级来选择不同的VIP用户折扣策略类,然后调用它的calculateDiscount方法来计算折扣价格。
具体代码实现如下(Java语言):
```java
// 定义折扣计算接口
public interface DiscountStrategy {
double calculateDiscount(double price);
}
// 定义VIP用户折扣策略类
public class VipDiscountStrategy implements DiscountStrategy {
private int level;
public VipDiscountStrategy(int level) {
this.level = level;
}
@Override
public double calculateDiscount(double price) {
// 根据VIP等级计算折扣价格
double discount = 0.0;
if (level == 1) {
discount = 0.9;
} else if (level == 2) {
discount = 0.8;
} else if (level == 3) {
discount = 0.7;
}
return price * discount;
}
}
// 定义普通用户折扣策略类
public class NormalDiscountStrategy implements DiscountStrategy {
@Override
public double calculateDiscount(double price) {
// 普通用户没有折扣
return price;
}
}
// 定义新用户折扣策略类
public class NewUserDiscountStrategy implements DiscountStrategy {
@Override
public double calculateDiscount(double price) {
// 新用户有10元优惠券
return price > 10 ? price - 10 : 0;
}
}
// 在程序运行时,根据用户类型选择不同的折扣策略类
public class DiscountCalculator {
public static void main(String[] args) {
int userType = 2; // 假设当前用户是VIP用户
double price = 100.0; // 商品原价
DiscountStrategy discountStrategy;
if (userType == 1) {
discountStrategy = new VipDiscountStrategy(1);
} else if (userType == 2) {
discountStrategy = new VipDiscountStrategy(2);
} else if (userType == 3) {
discountStrategy = new VipDiscountStrategy(3);
} else if (userType == 4) {
discountStrategy = new NewUserDiscountStrategy();
} else {
discountStrategy = new NormalDiscountStrategy();
}
double discountPrice = discountStrategy.calculateDiscount(price);
System.out.println("折扣价格:" + discountPrice);
}
}
```
在上面的代码中,我们定义了三个折扣策略类,分别是VipDiscountStrategy、NormalDiscountStrategy和NewUserDiscountStrategy。在程序运行时,我们根据用户类型选择不同的折扣策略类,然后调用它的calculateDiscount方法来计算折扣价格。这样一来,我们就可以轻松地添加新的折扣策略,而不需要改变原有的代码逻辑。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)