【java设计模式】【行为模式Behavioral Pattern】策略模式Strategy Pattern

 package com.tn.策略模式;

 public class Client {
private Strategy strategy;
public void setStrategy(Strategy strategy){
this.strategy=strategy;
}
public void exeAlgorithm() {
strategy.exeAlgorithm();
} public static void main(String[]args){
Strategy strategy1=new ConcreteStrategy1();
Strategy strategy2=new ConcreteStrategy2();
Strategy strategy3=new ConcreteStrategy3(); Client c=new Client(); c.setStrategy(strategy1);
c.exeAlgorithm(); c.setStrategy(strategy2);
c.exeAlgorithm(); c.setStrategy(strategy3);
c.exeAlgorithm();
}
}
interface Strategy{
void exeAlgorithm();
}
class ConcreteStrategy1 implements Strategy{
@Override
public void exeAlgorithm() {
System.out.println("执行算法1……");
}
}
class ConcreteStrategy2 implements Strategy{
@Override
public void exeAlgorithm() {
System.out.println("执行算法2……");
}
}
class ConcreteStrategy3 implements Strategy{
@Override
public void exeAlgorithm() {
System.out.println("执行算法3……");
}
}
上一篇:QQ输入法使用「智能英文」模式(CTRL+SHIFT+E),快速输入英文单词


下一篇:excel怎么批量删除多余的空行(浅水魚 20220226)