行为型模式 --- 策略模式
作者:
十二找十三 | 来源:发表于
2020-08-25 17:17 被阅读0次package study.org;
public class Demo {
public static void main(String[] args) {
Context context = new Context();
Strategy strategy = new ConcreteStrategyA();
// Strategy strategy = new ConcreteStrategyB();// 客户端根据自己的需要选择不同的策略
context.setStrategy(strategy);
context.strategyMethod();
}
}
interface Strategy {
public void operation();
}
class ConcreteStrategyA implements Strategy {
@Override
public void operation() {
System.out.println("ConcreteStrategyA operation method");
}
}
class ConcreteStrategyB implements Strategy {
@Override
public void operation() {
System.out.println("ConcreteStrategyB operation method");
}
}
class Context {
private Strategy strategy;
public Strategy getStrategy() {
return strategy;
}
public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
public void strategyMethod() {
strategy.operation();
}
}
本文标题:行为型模式 --- 策略模式
本文链接:https://www.haomeiwen.com/subject/jcpzjktx.html
网友评论