EffectiveJava -- uses function objects to represent policies, and implements tivejava functions.
Some languages support function pointers, proxies, lambda expressions, or similar mechanisms, allowing programs to store and pass the "ability to call special functions. This mechanism is usually used to allow function callers to specify their own behaviors by passing in the second function. The comparator function has two parameters, which are pointer to an element. If the element referred to by the first parameter is smaller than the element referred to by the second parameter, a negative integer is returned. If the two elements are equal, zero is returned. If the element referred to by the first parameter is heavy, returns a positive integer. Different comparator functions can be passed to obtain different sorting orders. This is an example of a policy pattern. The comparator function represents a policy for arranging elements.
Java does not provide function pointers, but you can use object references to implement the same functions. The method on the called object is usually to execute an operation on the object. However, we may also define such an object as its method to perform operations on other objects. If a class only exports such a method, its instance is equivalent to a pointer to the method. Such an instance is called a function object. Consider the following class:
class StringLengthComparator {public int compare(String s1, String s2) {return s1.length() - s2.length();}}
This class exports a method with two string parameters. A reference to the StringLengthComparator object can be considered as a "function pointer" pointing to the comparator and can be called on any pair of strings. In other words, the StringLengthComparator instance is a specific policy for string comparison operations.
As a typical specific policy class, the StringLengthComparator class is stateless: it has no domain, so all instances of this class are functionally equivalent to each other. Therefore, it is very suitable as a Singleton, which can save unnecessary object creation Overhead:
/*** Use the function object to indicate the policy * @ author weishiyao **/public class extends {private StringLengthComparator () {} public static final StringLengthComparator INSTANCE = new StringLengthComparator (); public int compare (String s1, String s2) {return s1.length ()-s2.length ();}}
To pass the StringLengthComparator instance to the method, an appropriate parameter type is required. It is not good to use StringLengthComparator because the client cannot pass any other comparison policies. Instead, we need to define a Comparator interface and modify StringLengthComparator to implement this interface. In other words, when designing a specific policy class, we also need to define a policy interface:
// Strategy interfacepublic interface Comparator<T> {public int compare(T t1, T t2);}
This definition of the Comparator interface also happens to appear in the java. util package, but it is not magical. We can define it ourselves. Comparator interface is of the pattern, so it is suitable for the Comparator of objects other than strings. Its compare parameter type is T, not String. As long as the StringLengthComparator class shown above is declared to do so, you can use it to implement the Comparator <String> interface.
The specific policy class is usually declared using an anonymous class. The following statement sorts A String Array Based on its length:
Arrays.sort(stringArray, new Comparator<T>() {@Overridepublic int compare(String s1, String s2) {// TODO Auto-generated method stubreturn s1.length() - s2.length();}});
However, note that when using anonymous classes in this way, a new instance will be created each time a call is executed. If it is executed repeatedly, consider storing the function object in a private static final domain and reusing it. Another benefit of doing so is that a meaningful domain name can be obtained for this function object.
Because the policy interface is used as the type of all specific policy instances, you do not need to make the policy class public in order to reverse the specific policy. On the contrary, the "host class" can also export public static domains, whose type is policy interface. The specific policy class can be the private prefix class of the host class. The following example uses static member classes instead of anonymous classes to allow specific policy classes to implement the second interface Serializable
/*** Use the function object to indicate the policy * @ author weishiyao **/public class Host {private static class StrLenCmp implements Comparator <String>, serializable {/*****/private static final long serialVersionUID =-5797980299250787300L; public int compare (String s1, String s2) {return s1.length ()-s2.length ();}} // Return comparator is serializablepublic static final Comparator <String> STRING_LENGTH_COMPARATOR = new StrLenCmp (); public static void main (String [] args) {System. out. println (STRING_LENGTH_COMPARATOR.compare ("aaaaaa", "aaaaa "));}}
The String class uses this mode to export a case-insensitive String comparator through its STRING_LENGTH_COMPARATOR field.
All in all, function pointers are mainly used to implement the policy mode. To implement this mode in java, you must declare an interface to represent this policy and declare a class that implements this interface for each policy. When a specific policy is used only once, an anonymous class is usually used to declare and instantiate this specific policy class. When a specific policy class is designed for reuse, its class is usually implemented as a private static member class and exported through the public static final domain, its type is this policy interface.