From Zhuge Liang's three tips on strategic models In other words, when Dong Wu and Sun Quan borrowed Jingzhou from Liu Bei, he was still worried, but did not expect Mrs. Gan's death. Zhou Lang was eager to ask Sun Quan to marry his sister and Liu Bei and lie to Liu Bei to marry Dong Wu. Liu Bei is not a fool. Of course, he knows the tricks. He immediately said: Neither will he be killed. Zhuge Liang said no, but now Zhao Yun is given three tips, asking him to accompany Liu Bei to Dongwu to get married ...... As we all know, Zhao Yun opened three tips at an appropriate time to resolve the crisis one by one. Zhou Yu only made a joke about how Zhou Lang worked well and lost his troops. In this story, we are interested in three tips. Zhuge Liang must be a master who uses the strategy model. Let's think: Why is Zhuge Liang so troublesome? To make three tips, he can make only one tip, write these three tips on it. But instead of doing so, he correctly applied the strategy model to make three tips. The advantage of doing so is obvious: Zhuge Liang wrote a clever plan, his ideas are very clear, and there will be no confusion between the three plans. Zhao Yun is also very convenient when looking at the tricks. It is very convenient when to look at the tricks. If three tips are mixed together, he will not be so convenient. Now it seems that this story contains the idea of solving the problem in the Policy mode: there are multiple methods or algorithms to solve a problem. For example, Zhuge Liang gave three tips, class providers need to separate these methods or algorithms. For example, Zhuge Liang divides the three tips into three tips. Class users, such as zhao Yun, must follow the appropriate conditions, such as the appropriate time in the story, use the corresponding method or algorithm. As we have mentioned earlier, the command mode encapsulates behaviors. The policy mode is actually the same as its solution, and is the encapsulation of algorithms. Of course, they all share the same interface. The following is an example of a policy pattern. The first example is the sorting of arrays. There are many algorithms for sorting arrays. We want to create an algorithm that allows users to specify an integer array and sort it, we provide the result using this method. We can do this:
Public int [] Sort (INT [] inarray, string type)
{If (type. equals ("A") {int K = 0; For (INT I = 0; I <inarray. length; I ++) {for (Int J = I + 1; j <inarray. length; j ++ ){
If (inarray [I]> inarray [J])
{
K = inarray [I];
Inarray [I] = inarray [J];
Inarray [J] = K;
}}} Else if (type. Equals ("B") {int K = 0; For (INT I = 0; I <inarray. length; I ++ ){
For (Int J = 0; j <inarray. length-i-1; j ++)
{If (inarray [J]> inarray [J + 1]) {k = inarray [J]; inarray [J] = inarray [J + 1]; inarray [J + 1] = K ;}}}after reading the above Code, you may find that there are many problems: first, all algorithms are mixed together, does not meet the single responsibility principle. Second, dependency details do not meet the principle of dependency inversion. Third, poor scalability. If you want to add a new algorithm, you need to modify the method. Next, let's take a look at the solution to the policy model. The first is to abstract an interface to all algorithms to meet the basic scalability conditions. Almost all models must be based on interfaces: public interface sortalgorithm {
Public void sort (INT [] inarr );
} Then the specific implementation of each algorithm, which implements the sortalgorithm interface and completes the separation of concerns to meet the single responsibility principle:
Public class sortone implements sortalgorithm
{
Public void sort (INT [] inarr)
{Int K = 0; For (INT I = 0; I <inarr. length; I ++) {for (Int J = I + 1; j <inarr. length; j ++ ){
If (inarr [I]> inarr [J])
{
K = inarr [I];
Inarr [I] = inarr [J];
Inarr [J] = K;
}}}}}
Public class sorttwo implements sortalgorithm
{
Public void sort (INT [] inarr)
{Int K = 0; For (Int J = 0; j <inarr. length-i-1; j ++ ){
If (inarr [J]> inarr [J + 1])
{K = inarr [J]; inarr [J] = inarr [J + 1]; inarr [J + 1] = K ;}}} then let's look at the client call:
Public int [] Sort (INT [] inarray, string type)
{Sortalgorithm SA; If (type. Equals ("")){
Sa = new sortone ();
}
Else if (type. Equals ("B "))
{
Sa = new srottwo ();
} Else {......} SA. sort (inarray);} above we have completed the solution of the Policy mode to this problem, meeting the single responsibility principle; with some scalability, if you add an algorithm, you can implement the sortalgorithm interface. Of course, we can see from the client code that the policy mode still does not completely remove the client's dependency on the specific implementation class, which makes it necessary to modify the client after a new algorithm is added. Therefore, the policy mode is the same as the command mode, but initially increases the scalability. If you want to completely remove the client's dependency on specific classes, you can further optimize the problem based on the factory model. You can do it yourself. The so-called policy mode is simply an encapsulation of algorithms. In the actual coding process, we will encounter various selection algorithms. At this time, we can use the policy mode. The following example is the end of this article. Suppose we have a pojo class: employee, which stores a lot of user information, as follows: public class employee {
Private int ID;
Private string name;
Private double servedyears;
......
Public void setid (int id)
{This. ID = ID;} public int GETID () {return this. ID ;}
Public void setname (string name)
{This. Name = Name;} Public String getname () {return this. Name ;}
Public void setservedyears (double servedyears)
{This. servedyears = servedyears ;}
Public double getservedyears ()
{Return this. servedyears ;}......} Now we have an array about employee, which needs to be sorted. We know that arrays is used. sort () sorts the array, but when using this method, we must first implement the comparator interface, as shown below:
Arrays. Sort (EMPs, new comparator (){
Public int compare (Object O1, object O2)
{
Return (employee) O1). getservedyears ()-(employee) O2). getservedyears ();
}); The preceding sorting process for pojo objects is implemented. The problem is that we need to sort the IDs, names, and servedyears of the employee class as indexes respectively. We can see that this is obviously a problem with multiple algorithms. You can use the policy mode to solve the problem: the comparator with the ID as the index:
Public class idcomparator implements Comparator
{
Public int compare (Object O1, object O2)
{
Return (employee) O1). GETID ()-(employee) O2). GETID ();
} Name-based comparator:
Public class namecomparator implements Comparator
{
Public int compare (Object O1, object O2)
{
Return (employee) O1). getname ()-(employee) O2). getname ();
} Comparator indexed by servedyears:
Public class servedyearscomparator implements Comparator
{
Public int compare (Object O1, object O2)
{
Return (employee) O1). getservedyears ()-(employee) O2). getservedyears ();
} Then, let's create a factory to obtain the specific comparator: public class factory {
Public static comparator getinstance (string type)
{If (type. equals ("ID") {return New idcomparator ();} else if (type. equals ("name") {return New namecomparator ();} else {return servedyearscomparator () ;}} then we can encode the client:
Public void sort (employee [] EMPs, string type)
{Arrays. sort (EMPs, factory. getinstance (type);} we can see that the problem of multi-algorithm is solved by combining the policy mode of the factory mode: each algorithm is extracted for independent attention, satisfies the single responsibility principle, satisfies the Dependency inversion principle, and has good scalability. And so on. What are you waiting? Hurry up and use these modes flexibly in practice!