What are strategic patterns
The policy pattern defines a list of algorithms, encapsulates each algorithm, and makes them interchangeable. The policy pattern lets the algorithm transform independently of the client that uses it.Copy the code
Here’s an example:
Suppose you want to travel, and there are many ways to travel, you can walk, take a train, fly and so on. Without any design patterns, the code would look something like this:
public class TravelStrategy {
enum Strategy{
WALK,PLANE,TRAIN
}
private Strategy strategy;
public TravelStrategy(Strategy strategy){
this.strategy=strategy;
}
public void travel() {if(strategy==Strategy.WALK){
print("walk");
}else if(strategy==Strategy.PLANE){
print("plane");
}else if(strategy==Strategy.TRAIN){
print("train"); } } public static void main(String[] args) { TravelStrategy walk=new TravelStrategy(Strategy.WALK); walk.travel(); TravelStrategy plane=new TravelStrategy(Strategy.PLANE); plane.travel(); TravelStrategy train=new TravelStrategy(Strategy.TRAIN); train.travel(); }}Copy the code
The fatal drawback of this is that once the number of ways to travel increases, you have to add new if else, which violates the open and close principle. If we use policy mode, we can solve the if else problem.
First, define the policy interface
public interface Strategy {
void travel();
}
Copy the code
Then implement the policy interface according to different travel modes
public class WalkStrategy implements Strategy{
@Override
public void travel() {
System.out.println("walk"); }}Copy the code
public class PlaneStrategy implements Strategy{
@Override
public void travel() {
System.out.println("plane"); }}Copy the code
public class TrainStrategy implements Strategy{
@Override
public void travel() {
System.out.println("train"); }}Copy the code
You also need a class that wraps the policy and invokes methods in the policy interface
public class TravelContext {
Strategy strategy;
public Strategy getStrategy() {
return strategy;
}
public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
public void travel() {
if(strategy ! = null) { strategy.travel(); }}}Copy the code
The test code
public class Test { public static void main(String[] args) { TravelContext travelContext=new TravelContext(); travelContext.setStrategy(new PlaneStrategy()); travelContext.travel(); travelContext.setStrategy(new WalkStrategy()); travelContext.travel(); travelContext.setStrategy(new TrainStrategy()); travelContext.travel(); }}Copy the code
As you can see, after applying the policy pattern, if you want to add a new mode of travel, there is no need to modify the existing class, just implement the policy interface, which is the principle of openness to extension in object-oriented. If you add a new way to get around by bike, all you need to do is add a new class.
public class BikeStrategy implements Strategy{
@Override
public void travel() {
System.out.println("bike"); }}Copy the code
And then set the policy
public class Main { public static void main(String[] args) { TravelContext travelContext=new TravelContext(); travelContext.setStrategy(new BikeStrategy()); travelContext.travel(); }}Copy the code