The policy pattern belongs to the behavior pattern of the object. The intention is to encapsulate each algorithm in a separate class with a common interface for a set of algorithms so that they can be replaced by each other. The policy pattern allows the algorithm to change without affecting the client.
Structure of the policy pattern
The strategy pattern is the wrapper of the algorithm, which separates the responsibility of the algorithm and the algorithm itself, and assigns it to different object management. A policy pattern typically wraps a series of algorithms into a series of policy classes as subclasses of an abstract policy class. In a word, it is: "Prepare a set of algorithms and encapsulate each algorithm so that they can be interchanged." The following is a schematic implementation of the structure of the instance of the policy pattern.
This pattern involves three roles:
- Environment (context) Role: holds a strategy reference.
- Abstract policy (Strategy) role: This is an abstract role, usually implemented by an interface or abstract class. This role gives all the interfaces required by the specific policy class.
- Specific policy (concretestrategy) Role: Wraps the associated algorithm or behavior.
Source
Environment Role Class
The public class
context {///holds a specific policy object
private strategy strategy;
/**
* constructor, passing in a specific policy object
* @param strategy specific policy Object
/public context
(strategy strategy) {
This.strategy = strategy;
}
/**
* Policy method */public
void Contextinterface () {
strategy.strategyinterface ()
}
}
Abstract Policy class
Public interface Strategy {
/**
* Policy Method
*
/public void Strategyinterface ();
}
Specific policy classes
Public class Concretestrategya implements strategy {
@Override public
void Strategyinterface () {
//related business c11/>}
} public
class Concretestrategyb implements strategy {
@Override public
Void Strategyinterface () {
//related business
}
} public
class CONCRETESTRATEGYC implements strategy {
@ Override public
void Strategyinterface () {
//related business
}
}
Analyze Java source in policy mode
statement: Here reference Java source Analysis-the strategy pattern in Java Collection Framework implementation code embodiment
In the Java Collection framework, a policy pattern is used when constructing a map or set to pass in the comparator comparer, or when creating a comparer in a static method of the collections class in which the parameter of the method is collection sorted.
Simple calling Code:
Import java.util.*;
public class Testcomparator {public
static void Main (String args[]) {
linkedlist<string> list = new Linkedl Ist<string> ();
List.add ("Wangzhengyi");
List.add ("Bululu");
Create a reverse comparator
comparator<string> r = Collections.reverseorder ();
Sort
Collections.sort (list, R) by reverse-contrast comparator;
SYSTEM.OUT.PRINTLN (list);
}
After you implement a comparer using the Collections.reverseorder () method, and then call Collections.sort (list, R) to sort the comparer into the method, look at the code in sort (list, R) below:
public static <T> void sort (list<t> List, comparator< super t> c) {
object[] a = List.toarray ();
arrays.sort (A, (Comparator) c);
Listiterator i = List.listiterator ();
for (int j=0; j<a.length; J + +) {
i.next ();
I.set (A[j]);
}
Array.Sort (A, (Comparator) c); This sentence continues to pass the comparator into the process, following is the specific operation of Array.Sort (A, (Comparator) c):
public static <T> void sort (t[] A, comparator< Super t> c) {
if (legacymergesort.userrequested)
Lega Cymergesort (A, c);
else
Timsort.sort (A, c);
}
static <T> void sort (t[] A, comparator< Super t> c) {
sort (A, 0, a.length, c);
}
/** to is removed in a future release. *
private static <T> void Legacymergesort (t[] A, comparator< Super t> c) {
t[] aux = A.clone ();
if (c==null)
mergesort (aux, A, 0, a.length, 0);
else
mergesort (aux, A, 0, a.length, 0, c);
Go on, keep going.
private static void MergeSort (object[] src, object[] dest, int low, int high, int o
FF, Comparator c) {int length = high-low; Insertion sort on smallest arrays if (length < Insertionsort_threshold) {for (int i=low; i
Select the code to use the comparator:
If list is already sorted, just copy from Src to dest. This is a
//optimization that results into faster sorts for nearly ordered.
if (C.compare (Src[mid-1], Src[mid]) <= 0) {
system.arraycopy (src, Low, dest, destlow, length);
return;
}
The Compare method here is also defined in the comparator interface:
Public interface comparator<t> {
int compare (t O1, T O2);
Since this is the generic implementation of the comparator, the actual execution, according to the comparator's specific implementation class call to the implementation code, which is the reverse comparison created above compare method, the implementation of the following methods:
public int Compare (comparable<object> C1, comparable<object> C2) {return
C2.compareto (c1);
}