Before we talk about the strategy mode, I will give you an example of daily life, from the capital International Airport to the XXX hotel, how to pass? 1 Hotel pick-up service, direct drive to pick up. 2) Take a taxi to the past. 3 Airport Express Rail + Subway 4 Airport bus 5 bus 6) walk past (not exhausted) and so on. Using the method, we can reach from the airport to the XXX hotel, right. So the way I list from airport to XXX Hotel is the strategy we can choose.
Another example is that when we use WCF, we tend to avoid extending it, such as authorization, and we can extend WCF through custom authorization. Here we can extend it by customizing Authorizationpolicy and ServiceAuthorizationManager, which is a real application of the strategy pattern.
1. Overview
It defines the algorithm family and encapsulates them so that they can be replaced by each other, and this pattern allows the algorithm to change without affecting the client using the algorithm.
2. Role in the pattern
2.1 Policy Class (Stratege): A public interface that defines all supported algorithms.
2.2 Specific policy class (concrete Stratege): Encapsulates a specific algorithm or behavior that inherits from the Stratege class.
2.3 Context class: Configure with a concretestratege to maintain a reference to the Stratege object.
Compare the opening examples to analyze the roles in this pattern:
In this example from the airport to the XXX hotel, it is necessary to include the Gotohotel method in the policy class. and the specific policy class should implement or inherit the policy class, and its implementation is needless to say. Context class, this class is important and interesting, because it needs to choose which policy to use, for example this up and down I am I, I want to from the airport to xxx hotel, 1 I am not bad money, the hotel also provides pick-up service, then I must choose the hotel pick-up AH; 2 If the hotel does not provide pick-up I will choose to play. 3 If I am shy, I can choose public transport. 4 If I have spent all my money now, even if I have no money to eat, then I can only choose to walk the past, and I have to beg in half way.
3. Pattern Interpretation
3.1 Generalized class diagram of strategy pattern
3.2 Code implementation of the policy pattern
<summary>///Policy class, which defines the public interface for all supported algorithms///</summary> publicly abstract class Stratege {///<summary&
Gt
The algorithms that are supported in the policy class, of course, can be more, and only one is defined here.
</summary> public abstract void algorithm (); ///<summary>///Specific strategy A, implemented a specific algorithm///</summary> public class Concretestrategea:stratege {///& Lt;summary>///Specific algorithm///</summary> public override void Algorithm () {//policy a implemented algorithm}}///<su
Mmary>///Specific strategy B, implemented a specific algorithm///</summary> public class Concretestrategeb:stratege {///<summary> Specific algorithms///</summary> public override void Algorithm () {///policy B implemented in the algorithm}}///<summary>/
Context, maintaining a reference to the Stratege object///</summary> public class Contexts {private Stratege m_stratege; <summary>///the specific policy into///</summary>///<param name= "Stratege" ></param> publi when initializing the context C context (Stratege stratege) {M_stratege = StratEge; ///<summary>///calls its algorithm///</summary> public void Executealgorithm () {m_stratege.al According to the specific policy object)
Gorithm ();
}
}
4. Model Summary
4.1 Advantages
4.1.1 Strategy pattern is a method of defining a series of algorithms, conceptually, all the algorithms accomplish the same work, but the implementation is different, it can call all algorithms in the same way, reduce the coupling between the various algorithms and the use of algorithm classes.
The Stratege class of the 4.1.2 Policy pattern defines a series of algorithms or behaviors that can be reused for the context. Inheritance helps to extract the public functionality of these algorithms.
4.1.3 Policy mode each algorithm has its own class, which can be tested individually by its own interface. Thus, unit testing is simplified.
The 4.1.4 policy pattern encapsulates specific algorithms or behaviors into the Stratege class, which eliminates conditional branching (avoiding different behaviors piling into a class).
4.2 Disadvantages
Give the client the responsibility to select a specific policy and transfer it to the context object
4.3 Applicable scenarios
4.3.1 When implementing a feature that requires different algorithm requirements
4.3.2 when different business rules are applied at different times
5. Example: Sorting is the algorithm we often touch, there are many ways to sort an array, that is, different strategies can be used. The following is a solution to the policy pattern of the sorting function.
5.1 Implementation Class diagram
5.2 Code Implementation
<summary>///sorting algorithm strategy///</summary> public abstract class Sortstratege {///<summary>/// Sort///</summary>///<param name= "Array" ></param>///<returns></returns> public A
Bstract int[] Sort (int[] array);
///<summary>///Bubble sort///</summary> public class Bubblesort:sortstratege {///<summary> Bubble sort algorithm (incremental sort)///</summary>///<param name= "Array" ></param>///<returns></returns&
Gt public override int[] sort (int[] array) {//implementation bubble sort algorithm for (int i = 0; i < array. Length; i++) {for (int j = i + 1; j < Array.) Length;
J + +) {if (Array[i] > Array[j]) {int temp = array[j];
ARRAY[J] = Array[i];
Array[i] = temp;
}} return array; }///<summary>///insert Sort///</summary> public class Insertsort:sortstratege {///<SUMMARY&G
T Insert Sort algorithm (ascending sort)///≪/summary>///<param name= "Array" ></param>///<returns></returns> public override int[
] Sort (int[] array) {//implementation insert sort algorithm int temp;
int I, j, N; n = array.
Length;
for (i = 1; i < n; i++) {temp = Array[i];
for (j = i; j > 0; j--) {if (temp < array[j-1]) array[j] = array[j-1];
else break;
ARRAY[J] = temp;
} return null;
} public class Sortcontext {private int[] m_array;
Private Sortstratege M_stratege;
<summary> the array and sort policy to be sorted is passed to the context///</summary>///<param name= "Array" ></param>///initialization <param name= "Stratege" ></param> public Sortcontext (int[) array, Sortstratege Stratege) {M_array
= array;
M_stratege = Stratege;
///<summary>///Call sort algorithm///</summary>///<returns></returns> public int[] sort ()
{int[] result = M_stratege.sort (This.m_array); return result;
}
}
5.3 Client Code
public class program
{public
static void Main (object[] args)
{
int[] array = new int[] {12, 8, 9, 18, 22 };
Use the bubble sort algorithm to sort
sortstratege Sortstratege = new Bubblesort ();
Sortcontext sorter = new Sortcontext (array, sortstratege);
Int[] result = Sorter. Sort ();
Use the Insert Sort algorithm to sort
sortstratege sortStratege2 = new Insertsort ();
Sortcontext sorter2 = new Sortcontext (array, sortStratege2);
int[] Result2 = Sorter. Sort ();
}
}
The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.