当前位置 : 主页 > 编程语言 > java >

策略模式Java版

来源:互联网 收集:自由互联 发布时间:2022-07-07
//策略类 public abstract class Strategy { public abstract void algorithmInterface (); } //策略A 和 策略B public class StrategyA extends Strategy { @Override public void algorithmInterface () { System . out . println ( "算法A 的思想
//策略类
public abstract class Strategy {
public abstract void algorithmInterface();
}


//策略A 和 策略B
public class StrategyA extends Strategy{

@Override
public void algorithmInterface() {
System.out.println("算法A 的思想");
}
}


// 策略B
public class StrategyB extends Strategy{

@Override
public void algorithmInterface() {
System.out.println("算法B 的思想");
}
}


// 策略的执行对象
public class Context {

Strategy strategy;

public Context(Strategy strategy){
this.strategy = strategy;
}

public void contextInterFace(){
strategy.algorithmInterface();
}
}

//客户端
public class Client {


public static void main(String[] args) {
Context context;
context = new Context(new StrategyA());
context.contextInterFace();
context = new Context(new StrategyB());
context.contextInterFace();
}

}


上一篇:Date转成LocalDate
下一篇:没有了
网友评论