以下是可復用事件處理一文的原代碼。

目前,面向對象是軟件系統建模的主流技術,使用面向對象技術建模的主要指標之一是可復用性。為了更好地解決軟件復用性和擴展性問題,設計模式得到了越來越多的關注與應用。

結合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);
      }
   }