The "Strategy_Pattern" composites the interface object so as to invoke the different subclasses which implement that interface in an easy manner.
The specific implementation is as follows:
package strategy_pattern;
public interface Strategy {
public abstract void AlgorithmInterface();
}
package strategy_pattern;
public class ConcreteStrategyA implements Strategy {
public void AlgorithmInterface() {
System.out.println("The algorithm A is running!");
}
}
package strategy_pattern;
public class ConcreteStrategyB implements Strategy {
public void AlgorithmInterface() {
System.out.println("The algorithm B is running!");
}
}
package strategy_pattern;
public class Context {
Strategy strategy;
public Context() {
this.strategy = null;
}
public Context(Strategy strategy) {
this.strategy = strategy;
}
public void ContextInterfece() {
strategy.AlgorithmInterface();// dynamic binding.
}
// combine the simple factory pattern.
public void setStrategy(String type) {
switch (type) {
case "A":
strategy = new ConcreteStrategyA();
break;
case "B":
strategy = new ConcreteStrategyB();
break;
default:
break;
}
}
}
package strategy_pattern;
public class Main {
public static void main(String args[]) {
Context c = new Context();
c.setStrategy("A");
c.ContextInterfece();
c.setStrategy("B");
c.ContextInterfece();
}
}
This is a general introduction to the 23 design patterns:
https://blog.csdn.net/GZHarryAnonymous/article/details/81567214