First, the primer
At 3:00 pm on the 18th, Shenyang, just finished a few years of rare snow, the weather is very cold, the bus is struggling on the road "Che Tao", I am sitting in the watch table in time - back to the company The shuttle bus is going to start, I am still far from the place where the car is ... This is the evil weather disrupted my plan! It seems that I have to re-count the plan of the bus: If you can get to the place where the shuttle can be reached, I will buy a "Southern Weekend" in the store next to the book, and I will forced the cold; if If the hateful bus can't be arched, I have to go to the nearby Carrefour to send two hours until the next shuttle bus!
Boot! !
In fact, it mentioned above is the different strategies taken by two different situations. This situation is often encountered in the actual system, then how did you achieve different strategies? Maybe you will add a good choice after watching the strategy mode!
Second, definition
Strategy belongs to an object behavior design mode, mainly to define a series of algorithms, package these algorithms into individual classes with common interfaces, and make them interchangeably. Policy mode makes these algorithms that can change from each other when the client calls them. The algorithm here does not understand the algorithm in the data structure, and it can be understood as different service processing methods.
What kind of benefit does this practice?
It separates the use of the algorithm itself, so that the changed specific algorithm is encapsulated, reducing the coupling of the code, and the system service policy is only a small number of modifications.
The algorithm is extracted so that the algorithm can be reused, which can also consider using the enjoys mode to share the algorithm object to reduce the system overhead (but pay attention to the suggestion conditions of using the enjoys).
Third, structure
First, it is imagined to imagine it: To make the algorithm have a common interface, this is to implement an interface or an abstract class. This is basically outlined, let's take a look:
Policy mode consists of three characters:
1) Algorithm use environmental role: Algorithm is referenced here and some other related operations to complete tasks.
2) Abstract strategic role: The interface required for all specific policy roles is specified. It is usually implemented by an interface or abstract class in Java.
3) Specific policy role: Implement an interface of an abstract policy role definition.
Use a simple class diagram to see the relationship between them:
Fourth, an example
There are many applications for policy modes in the Java language, and we will take an example of a layout manager. There are many sets of layout formats for setting up Container objects in the Java.awt class library, which can be used when creating a software interface. If you don't use the policy mode, there is no possibility of expanding the layout format, because you want to modify the method in the Container, let it know that you have this layout format, which is obviously not feasible.
Let's take a look at the implementation in the Java source code. Let's take a look at the participating classes and the roles they play, using the class diagram is clear.
Here I only put the least part of the ability to explain.
Let's take a look at the code in Container:
LayoutManager Layoutmgr; // Reference to the layout manager interface
// Get the specific layout manager in use
Public LayoutManager getLayout () {
Return Layoutmgr;
}
/ / Set the specific layout manager you want to use
Public void setLayout (LayoutManager Mgr) {
Layoutmgr = mgr;
IF (Valid) {
Invalidate ();
}
}
It can be seen that Container does not care about what specific layout manager you use, which makes the Container will change itself with the increase of the layout manager. Therefore, the strategy mode is a change in the change. Here is the code of the layout manager interface:
Public interface layoutmanager {
Void addLayoutComponent (String Name, Component CoMP);
.........
Dimension MinimumLayoutSize; Container Parent);
Void LayoutContainer (Container Parent);
}
The specific layout manager is the implementation and expansion of the above interface method. I will no longer show it here, I am interested in viewing the JDK source code.
Let's take a look at it, the following is a schematic code:
Public Class FlowLayoutWindow Extends Japplet
{
Public void init ()
{
Containter cp = getContentPane ();
Cp.setLayout (New flowLayout ());
For (int i = 0; i <20; i )
CP.Add (New JButton ("Button" i));
}
......
}
The perception of the specific layout manager until the last customer program is obtained, before it is not concerned. But this cannot be ignored - the customer must know which policies can be used, which also limits its range of use.
V. Use suggestions
Here is some suggestions using policy modes:
1) The system needs to be able to quickly switch in several algorithms.
2) There are some classes in the system to be active, and the policy mode can be refactored.
3) When there is a multiple condition selection statement in the system, it is possible to consider the adoption of policy mode.
However, pay attention to a point, and more than one algorithm can be used simultaneously in the policy mode.
Six, summary
Policy model is a relatively simple model, and its application is also very wide. The above is my understanding of the strategy model. If there is anything wrong, please work with you.