Application of adapter mode in j2se event processing

Source: Internet
Author: User
Application of adapter mode in j2se event processing

1. Adapter Pattern Theory

The adapter mode is also called the transformer mode. For details about the basic concepts of this mode, see other books or materials. Here we will only roughly discuss the differences between the two methods: class-based adapter mode and Object-based adapter mode.

The general structure of the class-based adapter mode is as follows: The adaptee class is the parent class of the adapter, The adaptee class is the adaptation source, and the adaptation target (Interface) is also the parent class of the adapter; the class-based adapter mode is suitable for some methods that the adapter wants to modify adaptee. The structure of the object-based adapter mode is as follows: The adaptee class object depends on the adapter, and the adaptation target (Interface) is the parent class of the adapter; the object-based adapter mode is applicable when the adapter wants to add a new method to the adaptee. However, when the method of the adaptee class is different from the method of the adapter class to implement the same function, we generally use the object-based adapter mode. In j2se event processing, the object-based adapter mode is widely used.

2. event handling in j2se

An example of event processing code in j2se is as follows:

Public class webformdesigner extends jframe {

?? Void jbinit () throws exception {

Text. addactionlistener (New webformdesigner_text_actionadapter (this ));

}

Void text_actionreceivmed (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 implements java. AWT. event. actionlistener {

? Webformdesigner adaptee;

? Webformdesigner_text_actionadapter (webformdesigner adaptee ){

??? This. adaptee = adaptee;

? }

? Public void actionreceivmed (actionevent e ){

??? Adaptee. text_actionreceivmed (E );

? }

}

In actual programming, the text_actionreceivmed method is implemented by the programmer (a new dialog box is displayed), while the webformdesigner_text_actionadapter class is automatically generated by the development environment based on the object-based adapter mode.

3. Adapter mode and code analysis

As shown in the preceding analysis, webformdesigner is adaptee in this example, and webformdesigner_text_actionadapter is adapter in this example. java. AWT. event. actionlistener is the adaptation target in this example.

The adaptation target is an interface, and the code is as follows:

Public interface actionlistener extends eventlistener {

??? /**

?? ?? * Invoked when an action occurs.

???? */

??? Public void actionreceivmed (actionevent E );

}

There is only one method in the adaptation target: actionreceivmed (). Because webformdesigner_text_actionadapter implements java. AWT. event. actionlistener, it is required to implement the actionreceivmed () method. Webformdesigner_text_actionadapter implements the actionreceivmed () method by referencing the webformdesigner adaptee. The Code shows that webformdesigner is a parameter of webformdesigner_text_actionadapter, so webformdesigner_text_actionadapter depends on webformdesigner.

The adaptee class (webformdesigner) method (text_actionshortmed) and the adapter class (webformdesigner_text_actionadapte) method (actionshortmed) implement the same function with different names, which is the object-based adapter mode we analyzed above.

?

Hongbo781202, also known as hongsoft, professional programmer, field of study: 1) workflow-based BPM System Research 2) Java-based Information Security

Technology. Welcome to discuss Java related technology and market issues hongbosoftware@163.com

?

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.