<> Strategy mode

Policy mode belongs to Java 23 One of the behavior patterns in a design pattern , Let's first look at what is the strategic model .

<>1, What is the strategy model

Definition of policy pattern :

This pattern defines a series of algorithms , And encapsulate each algorithm , So that they can be replaced with each other , And the change of the algorithm will not affect the customers who use the algorithm . The policy pattern belongs to the object behavior pattern , It encapsulates the algorithm , Separate the responsibility of using the algorithm from the implementation of the algorithm , And delegate to different objects to manage these algorithms .

In fact, in real life, we often encounter the situation that there are many strategies to choose to achieve a certain goal , for example , You can travel by plane , By train , Ride a bike or drive your own private car, etc .
Or online shopping , You can choose ICBC , agricultural bank , China Construction Bank, etc , But the algorithms they provide are consistent , Is to pay for you .

A similar situation will be encountered in software development , When implementing a function, there are many algorithms or strategies , We can choose different algorithms or strategies to complete this function according to different environments or conditions .( This is explained in detail in the following case )

graphic :

<>2, Advantages and disadvantages of strategy mode

advantage :

* Multiple conditional statements are not easy to maintain , Using policy mode can avoid using multiple conditional statements
* It provides a series of reusable algorithm families , The proper use of inheritance can transfer the public code of the algorithm family to the parent class , This avoids duplicate code
* Provide different implementations of the same behavior , Customers can choose different according to different time or space requirements
* It provides perfect support for the opening and closing principle , You can do this without modifying the original code , Flexible addition of new algorithms
* Put the use of the algorithm into the environment class , The implementation of the algorithm is moved to the specific policy class , The separation of the two is realized
shortcoming :

* The client must understand the differences between all policy algorithms , So as to select the appropriate algorithm class in time
* Policy patterns result in many policy classes , Increase maintenance difficulty
<>3, Structure of policy pattern

* Abstract policy class : A common interface is defined , Different algorithms implement this interface in different ways , The environment role uses this interface to call different algorithms , It is generally implemented using interfaces or abstract classes .
* Specific policy class : Implements the interface of abstract policy definition , Provide specific algorithm implementation .
* Environment class : Holds a reference to a policy class , Finally, it is called to the client .
Structure diagram :

<>4, code implementation

There are three ducks now : Green feather duck , Red feather duck , Duckling ( The duckling can't fly yet )

Now define a duck's parent class :
There are ways to call , There are ways to display appearance ( Because each one is different , Subclass override is required )
Can fly ( The policy pattern is used here )
public abstract class duck { // Ducks bark : public void quack(){ System.out.println(
" Quack quack "); } // Duck appearance , Because they are different , Therefore, it is implemented by subclasses public abstract void display(); // Use the following policy patterns :
// Hold the interface in the parent class , And the interface replaces the flight behavior ( combination ) private Flying flying; // provide set method public void
setFlying(Flying flying) { this.flying = flying; } public void fly(){ flying.Fly
(); } }
Define a flight interface :
/** * Policy interface : Realized the flying behavior of ducks */ public interface Flying { void Fly(); }
We know that the strategy pattern is to encapsulate the algorithms that need to be used , Two methods of flying and not flying are encapsulated in another package :

Can fly ( Inherit to the above flight interface , Rewrite flight method ):
public class FlyWithWin implements Flying { @Override public void Fly() {
System.out.println(" I can fly "); } }
Can't fly :
public class FlyNoWay implements Flying { @Override public void Fly() { System.
out.println(" I can't fly "); } }
be careful : The above two methods are encapsulated separately , As an algorithm family , Then when the program needs to use one of the algorithms , The program will not be affected by algorithm changes , Because the final effect of the algorithm is consistent

Red haired ducks :
/** * Red duck */ public class RedDuck extends duck{ public RedDuck(){ super();
// Inject the duck with the ability to fly , Here is the flying algorithm in the algorithm family super.setFlying(new FlyWithWin()); } @Override
public void display() { System.out.println(" I'm a Red duck "); } }
Green feather duck :
/** * * Green duck */ public class GreenDuck extends duck{ public GreenDuck(){ super(
); // Inject the duck with the ability to fly , This is also through the flying algorithm in the algorithm family super.setFlying(new FlyWithWin()); } @Override
public void display() { System.out.println(" I am a green duck "); } }
Ducklings ( Can't fly ):
/** * Duckling , Can't fly yet */ public class SamllDuck extends duck{ public SamllDuck(){
super(); // Ducklings can't fly , So the algorithm that can't fly in the algorithm family is used super.setFlying(new FlyNoWay()); } @Override
public void display() { System.out.println(" I'm still a little duck "); }
// Because the cry of a little duck is different from that of a big duck , So rewrite the call method public void quack(){ System.out.println(" Quack ~ Quack ~ Quack "); } }
Test class :
public class Test { public static void main(String[] args) { System.out.println
("*** Test duck program ***"); duck d = null; // Here's the running in turn !!!! d = new RedDuck(); // Test the Red duck d
= new GreenDuck(); // Test the green duck d = new SamllDuck(); // Test ducklings d.display(); d.quack()
; d.fly(); System.out.println("*** Test complete ***"); } }
When the object is Red duck hair :
*** Test duck program *** I'm a Red duck Quack quack I can fly *** Test complete ***
When using the green feather duck as the object :
*** Test duck program *** I am a green duck Quack quack I can fly *** Test complete ***
When using ducklings as objects :
*** Test duck program *** I'm still a little duck Quack ~ Quack ~ Quack I can't fly *** Test complete ***
<>5, Application scenario of policy mode

* When a system needs to dynamically select one of several algorithms , Each algorithm can be encapsulated into a policy class
* A class defines multiple behaviors , And these behaviors appear in the form of multiple conditional statements in the operation of this class , Instead of these conditional statements, each conditional branch can be moved into their respective policy class
* The algorithms in the system are completely independent of each other , And it is required to hide the implementation details of the specific algorithm from the customer
* When the system requires that the customer using the algorithm should not know the data it operates , Policy patterns can be used to hide algorithm related data structures
* Multiple classes differ only in their behavior , You can use policy mode , Dynamically select the specific behavior to be executed at run time

©2019-2020 Toolsou All rights reserved,
evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possible Experiment 4 Automated test tools - software test mysql Export data sql sentence _mysql according to sql Query statement export data Create a thread ——— Javaweb (3) Data structure experiment ( three )—— Stacks and queues TS stay vue2 Writing in the project web Front end signature plug-in _signature_pad Plug in implements electronic signature function docker Where is the image stored Qt Getting Started tutorial 【 Basic controls 】QCalendarWidget calendar control springboot How to get reality in ip address