Anonymous internal class-callback-Closure

Source: Internet
Author: User

First, define the concept of closure: A code segment is used as a parameter of the method.
Java does not directly use a method as a parameter for another method. Java uses an anonymous internal class to simulate this situation.

Anonymous internal classes are often implemented as an internal class (interface. There are some template methods in some platform classes. The template method contains a fixed process. Some of these steps call some methods in the internal class (interface. However, the platform class delays the implementation of these methods to the business class. The business class calls the template method of the platform class, but passes in the implementation of the anonymous internal class as the parameter of the template method.

Example:

 

 Package callback;

Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. sqlexception;

Public class anonymousbusinesstemplateexample2 {

// The Internal interface is also a callback interface. Only abstract methods are defined.
Private interface callback {
Object doit (connection conn) throws sqlexception;
}

// Template method (Abstract)
Private object execute (callback) throws sqlexception {
Connection conn = openconnection ();
Try {
Return callback. doit (conn );
} Finally {
Closeconnection (conn );
}
}

// Business method (specific)
Public object sqlquery (final string SQL) throws sqlexception {
// The anonymous internal class is used as the parameter of the template method to simulate the closure.
Return execute (New callback (){
Public object doit (connection conn) throws sqlexception {
Return conn.createstatement(cmd.exe cutequery (SQL );
}
});
}

Public connection openconnection () throws sqlexception {
Return drivermanager. getconnection ("", null );
}

Public void closeconnection (connection conn) throws sqlexception {
If (Conn! = NULL &&! Conn. isclosed ()){
Conn. Close ();
}
}
}

 

 

 

Generally, more internal interfaces are used than internal classes. Methods In internal classes can be implemented by default. When an anonymous internal class is passed in as a parameter of the business method, the default override method is used. Internal interfaces are not implemented by default. The specific implementation is completely handed over to the anonymous internal class.

The idea of anonymous internal classes is callback, that is, the docking principle. One benefit of callback is decouple. The client only needs to care about the business (such as the specific implementation of anonymous internal classes), instead of concerning the release of some resource connections, which are handed over to the template method in the platform class. The ruby closure also supports operations on each element in the array, each row in the file, and each record in the result set. Using Java to implement such iterations and operate on the elements is very troublesome. It seems that many partial template methods are used in Java, that is, some processes are fixed in the logic, and some resources are initialized and released.

 

 

Recommended: http://www.ibm.com/developerworks/cn/java/j-cb01097.html

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.