目前,面向对象是软件系统建模的主流技术,使用面向对象技术建模的主要指标之一是可复用性。为了更好地解决软件复用性和扩展性问题,设计模式得到了越来越多的关注与应用。
结合command设计模式和Java语言的反射技术,本文设计实现了一个可复用的事件处理框架。
在面向对象的系统设计中,有些方面的可复用性经常被忽略了,用户界面(User Interface, 下文简称UI)及其事件处理就是其中之一。一个完整的UI设计应该包括两部分:UI及其相应的事件处理机制,没有事件处理机制的UI是没有用的,对于事件处理,也应该考虑可复用设计。虽然看上去有些奇怪,但是这种设计是有实用价值的——提高了代码的可复用性、健壮性和可维护性。
command设计模式的主要设计思想是把对某一对象的请求封装为一个对象,从而把发出命令的责任和执行任务的责任分开,委派给不同的对象,请求的一方不必知道接收请求一方的接口。
这种引入第三方类的做法是设计模式所惯用的,引入的第三方类解耦了紧耦合对象。command设计模式中,第三方类解耦了调用操作的对象与知道如何实现该操作的对象,提高了软件的可复用性。
JDK 1.1及其以后的版本,引入了反射(reflection)技术。反射是Java中非常突出的动态特征,利用它可以加载一个运行时才得知名字的class,获取其完整结构,这一切是通过反射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 see if source was 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 may 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
(new SemiReusableActionListener(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);
}
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 may 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 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 may 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);
}
}
}