标签:custom customer imp [] mic public interface alc print
public interface Strategy {
double calculatePrice(double prePrice);
}
public class Customer implements Strategy {
@Override
public double calculatePrice(double prePrice) {
return prePrice * 0.9;
}
}
/**
* 负责和具体的策略类交互,使得算法和客户端分离,通过此类调用策略类的策略方法,策略类本身不能自动发送策略信息
*/
public abstract class Context{
//当前采用的算法对象
private Strategy strategy;
//通过构造器注入(组合)
public Context(Strategy strategy) {
this.strategy = strategy;
}
public abstract void calculatePrice(double price){
System.out.println("价格:" + strategy.calculatePrice(price));
}
}
public class Client{
public static void main(String[] args) {
Strategy strategy = new Customer();
Context context = new Context(strategy);
context.calculatePrice(99);
}
}
标签:custom customer imp [] mic public interface alc print
原文地址:https://www.cnblogs.com/mabaoqing/p/10691598.html