The appearance of "Policy Mode" is to provide a set of flexible algorithms that can be flexible, and users can freely select different algorithms to complete logic without affecting the upper-level interface.
The UML diagram of the strategy mode is as follows:
Where the model interface of the algorithm is defined in the "Abstract Policy", each iconic policy implements different strategies. "Consumption API" is a class that calls different algorithms, and the algorithm is selected according to different needs. Sometimes it is necessary to install a geconicity to other classes, then you can use the Simple Factory or "Factory Method) to generate the desired context. Let's simplify the use of the strategy mode after the project I am doing.
The project is a small business website, and the logical layer is implemented by servlet. The user has a requirement, and it is necessary to sort the records after the product information is called from the database, and the record is sorted by name (Name), Standard, Price (Price), and Register Date). Although you can use different SQL statements (ie, different Order By) to query the database in different sorting keywords. However, the network data traffic is too large, and each time I need to retrieve a data set from the database, so I chose to take the dataset to a list of the client at a time, and then on the client. Word sorted by LIST.
The method in the Java.util.Collections class has a method of public static void sort (list list, compare comparator), can be sorted by different COMPARATOR objects, which is quickly sorted, so the efficiency is very high. Java.util.comParerator is an interface:
Package java.util;
Public abstract interface comparator {
Boolean Equals (Object Object);
INT COMPARE (Object Object, Object Object1);
}
Where is the COMPARE method, it returns an integer. If the first parameter is more than the second parameter "big", then a positive number, if "small" returns a negative number, if "equal", return 0 Here, "Big", "Small", "Part" is a comparative standard that is defined by the COMPARE method. This method can not help but think that the function pointer INT * Compare (*, *) in the QSort function in the C language is invited, and the reason why there is no function pointer in Java is to replace the interface (more detailed introduction to "Effective Java" in the 22nd Strip "Use class and interface instead of function pointer").
Obviously, the Comparator interface is our "abstract strategy". The Sort method is our "consumption API", and different "aspect policy" is the class that we are sorted according to different keywords from the COMPARATOR interface.
The UML map of the entire sort model is as follows, in which I will inherit an interface ItemComparator from Comparator, which generally describes the NameComparator class, the PriceComparator class, the RegisterDateComparator class, and the StandardComparator class according to different keywords. This interface does not require this interface in actual use, and four classes can be used directly from Comparator. The itembean is a bean that encapsulates the product information, and these beans are stored. Itemsort and ItemComparator and four specific classes implements the "simple factory" mode and encapsulates the sort method. Below is the specific code (the code of the StandardComparator class is similar to the code of the NameComparator class, not listed here):
NameComparator class:
Package com.lim.designpatterns.strategy;
Public Class NameComparator
Implements itemparator {
Namecomparator () {} // Package the constructor, the class outside the package is only available for the COMPARATOR instance.
Public int Compa (Object O1, Object O2) {
String name1 = (itembean) o1) .getname ();
String name2 = (itembean) o2) .getname ();
Return name1.compareto (name2); // Call the string COMPARETO method
}
}
PriceComparator class
Package com.lim.designpatterns.strategy;
Public Class PriceComparator
Implements itemparator {
PriceComparator () {}
Public int Compa (Object O1, Object O2) {
Double Price1 = New Double ((itembean) O1) .getprice ());
Double Price2 = New Double ((itembean) O2) .Getprice ());
Return Price1.comPareto (Price2); // Call the Double CocPareTo method
}
}
RegisterDateComparator class
Package com.lim.designpatterns.strategy;
Import java.util. *
;
Public Class RegisterDateComparator
Implements itemparator {
RegisterDateComparator () {}
Public int Compa (Object O1, Object O2) {
Date Date1 = ((itembean) O1) .getregisterDate ();
Date Date2 = ((Itembean) O2) .getregisterDate ();
Return Date1.Compareto (Date2); // Call Date's CompareTo method
}
}
Itemsort class
Package com.lim.designpatterns.strategy;
Import java.util. *;
Public class itemsort {
Public Static List Sort (List items, ItemComparator C) {
Collectes.Sort (items, c);
Return Items;
}
Public Static Final ItemComparator Name = New NameComparator (); // Simple Factory
Public Static Final ItemComparator Price = New PriceComparator ();
Public static final itemparator standard = new standardcomparetor ();
Public static final itemcomparator reg_date = new registerdatecomparator ();
}
TestItemsort class
Package com.lim.designpatterns.strategy;
Import java.util. *;
Public class testItemsort {
Public static void main (String [] args) {
List items = new arraylist ();
/ / Add an entry to List
Items = itemsort.sort (items, itemsort.name); // Sort by name
Items = itemsort.sort (items, itemsort.price); // Sort by price
items = itemsort.sort (items, itemsort.reg_date); // Sort by registration date
Items = itemsort.sort (items, itemsort.standard); // Sort by specification
}
}