Design and implementation code of reusable event processing in Java

Source: Internet
Author: User
Tags add object exit interface pack reflection string
Design the following is the original code for reusable event handling. At present, object-oriented is the mainstream technology of software system modeling, one of the main indexes of using object-oriented technology to model is reusability. In order to solve the problem of software reusability and scalability better, the design pattern has been paid more and more attention and application. Combining command design mode with the reflection technology of Java language, this paper designs a reusable event processing framework. In object-oriented system design, some aspects of reusability are often overlooked, user interface (username Interface, hereinafter referred to as UI) and event handling is one of them. A complete UI design should include two parts: the UI and its corresponding event-handling mechanism, the UI without an event-handling mechanism is useless, and the reusable design should also be considered for event handling. While it may seem strange, the design is practical-it improves the reusability, robustness, and maintainability of the code. The main design idea of the command design pattern is to encapsulate a request for an object as an object, separating the responsibility for issuing the command from the responsibility to perform the task, delegating it to different objects, and the requesting party does not have to know the interface to receive the requesting party. This introduction of Third-party classes is customary in design patterns and the introduction of third-party classes decoupling tightly coupled objects. In command design mode, third-party classes decouple the object that invokes the operation and the object that knows how to implement the operation, which improves the reusability of the software. JDK 1.1 and later versions introduce reflection (reflection) technology. Reflection is a very prominent dynamic feature in Java, it can load a runtime to know the name of class, get its complete structure, all this is done through the reflection API. UIDemo1
Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*; public class UIDemo1 implements actionlistener{
Private JFrame frame;
Private JButton button;
Private JTextArea area;
private int index =-1;
Private String [] params; Private UIDemo1 (String args[]) {
params = args;
area = new JTextArea (5,25);
button = new JButton ("click here!");
Button.addactionlistener (this); frame = new JFrame (GetClass (). GetName ());
Frame.addwindowlistener (New Reusablewindowadapter ());
Container cont = Frame.getcontentpane ();
JScrollPane ScrollPane = new JScrollPane (area);
Cont.add (Scrollpane,borderlayout.north);
Cont.add (Button,borderlayout.south);
Frame.pack ();
Frame.setvisible (TRUE);
} public void actionperformed (ActionEvent ae) {
Provide equality check to if source is the
button defined above. Useful only if we register
This is actionlistener with multiple sources
if (Ae.getsource (). Equals (button)) {
index = ++index% Params.length;
Area.settext (Params[index]);
}
public static void Main (String args[]) {
if (Args.length > 1) {
UIDemo1 one = new UIDemo1 (args);
}else{
Usage ();
}
}
private static void usage () {
SYSTEM.ERR.PRINTLN ("You could excute this program as below:");
System.err.println ("Java UIDemo1 params1 params2 ...");
System.exit (-1);
}
}//uidemo2
Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;p ublic class uidemo2{
Private JFrame frame;
Private JButton button;
Private JTextArea area;
private int index =-1;
Private String [] params; Private UIDemo2 (String args[]) {
params = args;
area = new JTextArea (5,25);
button = new JButton ("click here!");
Button.addactionlistener (This) (new Semireusableactionlistener); frame = new JFrame (GetClass (). GetName ());
Frame.addwindowlistener (New Reusablewindowadapter ());
Container cont = Frame.getcontentpane ();
JScrollPane ScrollPane = new JScrollPane (area);
Cont.add (Scrollpane,borderlayout.north);
Cont.add (Button,borderlayout.south);
Frame.pack ();
Frame.setvisible (TRUE);
}
void Setmessage (Object source) {
if (source.equals (button)) {
index = ++index% Params.length;
Area.settext (Params[index]);
}
public static void Main (String args[]) {
if (Args.length > 1) {
UIDemo2 two = new UIDemo2 (args);
}else{
Usage ();
}
}
private static void usage () {
SYSTEM.ERR.PRINTLN ("You could excute this program as below:");
System.err.println ("Java UIDemo2 params1 params2 ...");
System.exit (-1);
}
}//semireusableactionlistenerimport java.awt.event.*;p Ublic class Semireusableactionlistener implements actionlistener{
Private UIDemo2 Demo2;
Semireusableactionlistener (UIDemo2 Uidemo) {
Demo2 = Uidemo;
}
public void actionperformed (ActionEvent ae) {
Demo2.setmessage (Ae.getsource ());
}
}//uidemo3
Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;
Import java.lang.reflect.*;p ublic class uidemo3{
Private JFrame frame;
Private JButton button1;
Private JTextArea area;
private int index =-1;
Private String [] params;
Private UIDemo3 (String args[]) {
params = args;
area = new JTextArea (5,25);
button1 = new JButton ("click here!");
Setup required information to use Genericactionlistener
String methodname = "Setmessage";
Class [] paramtypes = {Java.lang.Object.class};
Object [] Methodargs = {button1};
Class clazz = This.getclass ();
try{
Method method = Clazz.getmethod (methodname, paramtypes); Button1.addactionlistener (New
Reusableactionlistener (method, this, Methodargs));
}
catch (Exception e) {
System.out.println ("Could not find" method: "+methodname+" \nnow exiting ...);
System.exit (-1);
frame = new JFrame (GetClass (). GetName ());
Frame.addwindowlistener (New Reusablewindowadapter ());
Container cont = Frame.getcontentpane ();
JScrollPane ScrollPane = new JScrollPane (area);
Cont.add (Scrollpane,borderlayout.north);
Cont.add (Button1,borderlayout.south);
Frame.pack ();
Frame.setvisible (TRUE);
}
public void Setmessage (Object source) {
if (Source.equals (button1)) {
index = ++index% Params.length;
Area.settext (Params[index]);
}
}
public static void Main (String args[]) {
if (Args.length > 1) {
UIDemo3 three = new UIDemo3 (args);
}else{
Usage ();
}
}
private static void usage () {
SYSTEM.ERR.PRINTLN ("You could excute this program as below:");
System.err.println ("Java UIDemo3 params1 params2 ...");
System.exit (-1);
}
}
Reusablewindowadapter import java.awt.*;
Import java.awt.event.*;p ublic class Reusablewindowadapter extends windowadapter{
public void windowclosing (WindowEvent we) {
Object Source = We.getsource ();
if (source instanceof Frame) {
((Frame) source). setvisible (false);
((Frame) source). Dispose ();
System.exit (0);
}
}
}


Related Article

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.