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……");
}
}