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 the If source is the
//button defined above.
useful only if we register
//this 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.*;
public 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);
}
}
//semireusableactionlistener
import java.awt.event.*;
public 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.*;
public 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 =
Clazz.getmethod (methodname, paramtypes);
Button1.addactionlistener (New
Reusableactionlistener
(method, this, Methodargs));
}
catch (Exception e) {
System.out.println ("Could not
Find the 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.*;
public 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);
}
}
}