首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Linux频道
中国IT教育
Google
首页 资讯动态 认证考试 新手入门 核心技术 高级技术 J2EE J2ME Java&XML 开源技术 其他技术 RSS订阅 论坛 专题
您现在的位置: 中国IT实验室 >> Java >> 新手入门 >> 基础入门 >> 正文

Java可复用事件处理的设计与实现解析

       目前,面向对象是软件系统建模的主流技术,使用面向对象技术建模的主要指标之一是可复用性。为了更好地解决软件复用性和扩展性问题,设计模式得到了越来越多的关注与应用。

  结合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);   }   }   }
【责编:Zenghui】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 ·Java入门 Tomcat的配置技巧精华专题…
 ·Oracle RMAN物理备份技术详解…
 今日更新
 社区讨论
 博客论点
 频道精选
 Java 频道导航