Article 3: representation of a policy using a function object

Source: Internet
Author: User

Function object: class that calls operators. Its objects are often called function objects, that is, they are objects with similar functions.

Some languages support function pointers, proxies, and lambda expressions, or support similar mechanisms.ProgramStore and pass the "ability to call special functions.

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 an object whose methods execute operations on other objects (other objects) that are explicitly passed to these methods.
If a class only exports such a method, its instance is actually equivalent to a pointer to the method. Such an instance is called a function object ).
For example, the following class:

 
Public class stringlengthcomparator {public int compare (string S1, string S2) {return s1.length ()-s2.length ();}}

A reference to the stringlengthcomparator object can be considered as a "function pointer" pointing to the comparator and can be called on any object string. In other words, the stringlengthcomparator instance is a specific strategy for string comparison operations (concrete strategy ).

As a typical specific policy class, the stringlengthcomparator class is stateless (stateless): it has no domain. Therefore, this class is equivalent to all instances in terms of functionality. Therefore, he is very suitable for singleton:

Public class extends {private stringlengthcomparator2 () {} public static final stringlengthcomparator2 instance = new stringlengthcomparator2 (); Public int compare (string S1, string S2) {return s1.length () -s2.length ();}}

To be alternative, define a strategy interface as follows:

 
Public interface comparable <t> {public int compareto (T T1, t T2 );}

(Interface generics improve availability)

Specific policy classes often use anonymous internal classes, such:

 
Arrays. Sort (stringarray, new comparator <string> () {@ overridepublic int compare (string O1, string O2) {return 0 ;}});

However, a new instance is created every time a call is executed. If he is repeatedly executed, consider storing the instance object in a private static final domain and reusing him:

Public class host {Private Static class strlencmp implements comparator <string>, serializable {@ overridepublic int compare (string O1, string O2) {return o1.length ()-o2.length ();}} public static final comparator <string> string_length_comparator = new strlencmp ();}

Function pointers are mainly used to implement the Strategy Mode. It also effectively relies on interfaces.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.