added strategy example
This commit is contained in:
parent
7c614b0c5c
commit
51f5d352c6
8
src/learn/simple_strategy/ConcreteStrategyA.java
Normal file
8
src/learn/simple_strategy/ConcreteStrategyA.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package learn.simple_strategy;
|
||||||
|
|
||||||
|
public class ConcreteStrategyA implements Strategy {
|
||||||
|
@Override
|
||||||
|
public void algorithm() {
|
||||||
|
System.out.println("nice strategy A");
|
||||||
|
}
|
||||||
|
}
|
8
src/learn/simple_strategy/ConcreteStrategyB.java
Normal file
8
src/learn/simple_strategy/ConcreteStrategyB.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package learn.simple_strategy;
|
||||||
|
|
||||||
|
public class ConcreteStrategyB implements Strategy {
|
||||||
|
@Override
|
||||||
|
public void algorithm() {
|
||||||
|
System.out.println("nice strategy B");
|
||||||
|
}
|
||||||
|
}
|
17
src/learn/simple_strategy/Context.java
Normal file
17
src/learn/simple_strategy/Context.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package learn.simple_strategy;
|
||||||
|
|
||||||
|
public class Context {
|
||||||
|
private Strategy currentStrategy;
|
||||||
|
|
||||||
|
public Context(Strategy currentStrategy) {
|
||||||
|
this.currentStrategy = currentStrategy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCurrentStrategy(Strategy currentStrategy) {
|
||||||
|
this.currentStrategy = currentStrategy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void doSomeJob() {
|
||||||
|
currentStrategy.algorithm();
|
||||||
|
}
|
||||||
|
}
|
11
src/learn/simple_strategy/Main.java
Normal file
11
src/learn/simple_strategy/Main.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package learn.simple_strategy;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Context context = new Context(new ConcreteStrategyB());
|
||||||
|
|
||||||
|
context.doSomeJob();
|
||||||
|
context.setCurrentStrategy(new ConcreteStrategyA());
|
||||||
|
context.doSomeJob();
|
||||||
|
}
|
||||||
|
}
|
5
src/learn/simple_strategy/Strategy.java
Normal file
5
src/learn/simple_strategy/Strategy.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package learn.simple_strategy;
|
||||||
|
|
||||||
|
public interface Strategy {
|
||||||
|
void algorithm();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user