Application of Adapter mode in J2SE event processing
1. Adapter mode theory
Adapter mode is also known as transformer mode, please refer to other books or information about this mode; here we only discuss the difference between the two implementations of this mode: based on class-based Adapter mode and object-based Adapter mode.
The general structure based on class-based Adapter mode is as follows: Adaptee class is the parent class of Adapter, the Adaptee class is the source, the adaptive target (interface) is also the parent class of Adapter; based on class-based Adapter mode is suitable for adapter want to modify Adaptee The situation of some methods. The general structure of an object-based Adapter mode is as follows: Adaptee class object is dependent on Adapter, the adaptive target (interface) is the parent class of Adapter; the object-based Adapter mode is suitable for adapter to add new methods to Adaptee. However, in the case where the Adaptee class is different from the Adapter class, we generally use object-based Adapter mode. In J2SE event processing, we use a large number of object-based Adapter mode.
2. Event processing in J2SE
The event processing code in J2SE is, for example, below:
Public class WebformDesigner extends jframe {
?? void jbinit () throws exception {
Text.addActionListener (New WebformDesigner_Text_ActionAdapter (this));
}
Void text_actionperformed (ActionEvent E) {
??? Textdialog CreateText = New TextDialog ();
??? CreateText.setVisible (TRUE);
??? CreateText.Settitle ("text");
??? CreateText.SetBounds (100, 100, 500, 300);
??? CreateText.SETMODAL (TRUE);
??? CreateText.show ();
?
}
Class WebFormDesigner_text_actionadapter import.Ava.awt.event.ActionListener {
WebFormDesigner adaptee;
? WebformDesigner_text_actionadapter (WebFormDesigner adaptee) {
??? this.adaptee = adaptee;
?
PUBLIC VOID ACTIONPERMMED (ActionEvent E) {
??? adaptee.text_actionperformed (e);
?
}
When actually programming, the text_actionperformed method is implemented by the programmer (popped up new dialog), and the WebFormDesigner_text_actionAdapter class is automatically generated by the development environment based on object-based Adapter mode.
3. Adapter mode and code analysis
From the above analysis, WebFormDesigner is adaptee in this example, and WebFormDesigner_text_actionAdapter is Adapter in this example, and java.awt.event.ActionListener is an adaptive target in this example.
The adaptation target is an interface, the code is as follows:
Public Interface ActionListener Extends EventListener {??? / **
?? * Invoked When an action Occurs.
???? * /
??? public void actionperformed (ActionEvent E);
}
There is only one method in the adapter target: ActionPerformed (). Since WebFormDesigner_Text_ActionAdapter implements java.awt.event.ActionListener, the actionPerformed () method is required. WebFormDesigner_text_actionAdapter Implementation ActionPerformed () method is completed by reference to WebFormDesigner this Adaptee. As can be seen from the code, WebFormDesigner is a parameter for the method of WebFormDesigner_text_actionAdapter, so WebFormDesigner_text_actionAdapter relies on WebFormDesigner.
The Adaptee class (text_actionperformed) implements the same functionality with the method of the ADAPTER class (webformdesigner_text_actionadapte), which is the object-based ADAPTER mode we analyzed.
?
HONGBO781202, also known as hongsoft, professional programmer, research area: 1) Based on workflow 2) Java-based information security
Technology. Welcome to discuss all aspects of Java related aspects of technology and market issues hongbosoftware@163.com
?