这是一个关于策略(Strategy)设计模式应用的需求描述,涉及的实体信息有:
- 类:
Interval
,代表由下界(lower bound
)和上界(upper bound
)定义的区间 - 功能需求:采用不同格式显示区间范围,如
[lower bound.upper bound]
、[lower bound…upper bound]
、[lower bound - upper bound]
,通过策略模式实现
以下是基于Java语言的策略模式实现示例,供参考:
1. 定义策略接口(Strategy)
// 策略接口,定义显示区间的方法
public interface IntervalDisplayStrategy {
String display(int lowerBound, int upperBound);
}
2. 实现具体策略类(Concrete Strategies)
// 策略1:[lower bound.upper bound] 格式
public class DotDisplayStrategy implements IntervalDisplayStrategy {
@Override
public String display(int lowerBound, int upperBound) {
return "[" + lowerBound + "." + upperBound + "]";
}
}
// 策略2:[lower bound…upper bound] 格式
public class EllipsisDisplayStrategy implements IntervalDisplayStrategy {
@Override
public String display(int lowerBound, int upperBound) {
return "[" + lowerBound + "…" + upperBound + "]";
}
}
// 策略3:[lower bound-upper bound] 格式
public class DashDisplayStrategy implements IntervalDisplayStrategy {
@Override
public String display(int lowerBound, int upperBound) {
return "[" + lowerBound + "-" + upperBound + "]";
}
}
3. 定义上下文类(Context)
// 上下文类,用于使用策略
public class IntervalContext {
private IntervalDisplayStrategy strategy;
public IntervalContext(IntervalDisplayStrategy strategy) {
this.strategy = strategy;
}
public void setStrategy(IntervalDisplayStrategy strategy) {
this.strategy = strategy;
}
public String displayInterval(int lowerBound, int upperBound) {
return strategy.display(lowerBound, upperBound);
}
}
4. 测试使用
public class Main {
public static void main(String[] args) {
// 创建上下文,初始使用点分隔策略
IntervalContext context = new IntervalContext(new DotDisplayStrategy());
System.out.println(context.displayInterval(1, 10));
// 切换策略为省略号分隔
context.setStrategy(new EllipsisDisplayStrategy());
System.out.println(context.displayInterval(1, 10));
// 切换策略为破折号分隔
context.setStrategy(new DashDisplayStrategy());
System.out.println(context.displayInterval(1, 10));
}
}
这样就通过策略模式,让 Interval
相关的区间显示功能,能灵活切换不同格式,符合题目中对不同显示样式的需求 。如果是其他编程语言(如Python、C++ 等),也可依据策略模式的思路,用对应语言语法实现,核心是分离算法(不同显示格式逻辑),让其可独立变化和切换。
策略模式是一种行为设计模式,它使你能在运行时改变对象的行为。策略模式定义了一系列算法,将每个算法封装起来,并使它们可以互换。策略模式让算法的变化独立于使用算法的客户。
以下是使用策略模式实现不同区间显示格式的一个简单示例:
- 首先,定义一个策略接口,它声明了一个方法,用于显示区间。
public interface IntervalDisplayStrategy {
void displayInterval(String lowerBound, String upperBound);
}
- 然后,为每种显示格式实现该策略接口。
public class BracketsStrategy implements IntervalDisplayStrategy {
public void displayInterval(String lowerBound, String upperBound) {
System.out.println("[" + lowerBound + ".." + upperBound + "]");
}
}
public class DotBracketsStrategy implements IntervalDisplayStrategy {
public void displayInterval(String lowerBound, String upperBound) {
System.out.println("[" + lowerBound + "." + upperBound + "]");
}
}
public class HyphenStrategy implements IntervalDisplayStrategy {
public void displayInterval(String lowerBound, String upperBound) {
System.out.println("[" + lowerBound + "-" + upperBound + "]");
}
}
- 创建一个上下文类,它使用策略接口来引用具体的策略对象。
public class Interval {
private IntervalDisplayStrategy strategy;
public Interval(IntervalDisplayStrategy strategy) {
this.strategy = strategy;
}
public void setStrategy(IntervalDisplayStrategy strategy) {
this.strategy = strategy;
}
public void display() {
strategy.displayInterval("lower bound", "upper bound");
}
}
- 客户端代码可以根据需要选择不同的策略。
public class Client {
public static void main(String[] args) {
Interval interval = new Interval(new BracketsStrategy());
interval.display(); // 输出: [lower bound..upper bound]
interval.setStrategy(new DotBracketsStrategy());
interval.display(); // 输出: [lower bound..upper bound]
interval.setStrategy(new HyphenStrategy());
interval.display(); // 输出: [lower bound-upper bound]
}
}
在这个示例中,Interval
类是上下文,它使用IntervalDisplayStrategy
接口来引用具体的策略对象。客户端可以根据需要选择不同的策略,从而改变Interval
对象的行为。
这种设计模式的优点是:
- 算法可以独立于使用它的客户而变化。
- 策略模式使你可以定义一系列的算法,把它们一个个封装起来,并且使它们可以互换。
- 策略模式让算法的变化独立于使用算法的客户。
通过使用策略模式,你可以在运行时根据需要选择不同的算法或行为,从而提高代码的灵活性和可维护性。
以电商订单价格计算为例,不同订单类型(普通订单、促销订单、会员订单 )有不同计价策略,用策略模式实现:
1. 策略接口(定义计算价格的规范)
// 价格计算策略接口
public interface PricingStrategy {
double calculatePrice(double originalPrice);
}
2. 具体策略类(实现不同计价逻辑)
// 普通订单策略:原价
public class RegularPricing implements PricingStrategy {
@Override
public double calculatePrice(double originalPrice) {
return originalPrice;
}
}
// 促销订单策略:打8折
public class PromotionPricing implements PricingStrategy {
@Override
public double calculatePrice(double originalPrice) {
return originalPrice * 0.8;
}
}
// 会员订单策略:打7折 + 额外积分抵扣(假设每100积分抵1元,这里简化,积分固定扣10元演示 )
public class MemberPricing implements PricingStrategy {
@Override
public double calculatePrice(double originalPrice) {
double memberPrice = originalPrice * 0.7;
return memberPrice - 10; // 模拟积分抵扣
}
}
3. 上下文类(管理策略,供外部使用)
// 订单上下文,用于应用价格计算策略
public class OrderContext {
private PricingStrategy pricingStrategy;
public OrderContext(PricingStrategy pricingStrategy) {
this.pricingStrategy = pricingStrategy;
}
public void setPricingStrategy(PricingStrategy pricingStrategy) {
this.pricingStrategy = pricingStrategy;
}
public double getFinalPrice(double originalPrice) {
return pricingStrategy.calculatePrice(originalPrice);
}
}
4. 测试场景(模拟不同订单计算价格)
public class EcommerceDemo {
public static void main(String[] args) {
// 普通订单
OrderContext regularOrder = new OrderContext(new RegularPricing());
System.out.println("普通订单价格:" + regularOrder.getFinalPrice(200));
// 促销订单
OrderContext promotionOrder = new OrderContext(new PromotionPricing());
System.out.println("促销订单价格:" + promotionOrder.getFinalPrice(200));
// 会员订单
OrderContext memberOrder = new OrderContext(new MemberPricing());
System.out.println("会员订单价格:" + memberOrder.getFinalPrice(200));
}
}
这个场景里,不同订单类型对应不同计价策略,策略模式让计价逻辑可灵活切换、拓展(比如新增“节日专属策略” ),符合开闭原则,也让代码职责清晰,便于维护和新增需求~