熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java中可復用事件處理的設計與實現代碼

2022-06-13   來源: Java核心技術 

  目前面向對象是軟件系統建模的主流技術使用面向對象技術建模的主要指標之一是可復用性為了更好地解決軟件復用性和擴展性問題設計模式得到了越來越多的關注與應用
  
  結合command設計模式和Java語言的反射技術本文設計實現了一個可復用的事件處理框架
  
  在面向對象的系統設計中有些方面的可復用性經常被忽略了用戶界面(User Interface 下文簡稱UI)及其事件處理就是其中之一一個完整的UI設計應該包括兩部分UI及其相應的事件處理機制沒有事件處理機制的UI是沒有用的對於事件處理也應該考慮可復用設計雖然看上去有些奇怪但是這種設計是有實用價值的——提高了代碼的可復用性健壯性和可維護性
  
  command設計模式的主要設計思想是把對某一對象的請求封裝為一個對象從而把發出命令的責任和執行任務的責任分開委派給不同的對象請求的一方不必知道接收請求一方的接口這種引入第三方類的做法是設計模式所慣用的引入的第三方類解耦了緊耦合對象command設計模式中第三方類解耦了調用操作的對象與知道如何實現該操作的對象提高了軟件的可復用性
  
  JDK 及其以後的版本引入了反射(reflection)技術反射是Java中非常突出的動態特征利用它可以加載一個運行時才得知名字的class獲取其完整結構這一切是通過反射API完成的
  
  //UIDemo
  import javaawt*;
  import javaawtevent*;
  import javaxswing*;
  
  public class UIDemo implements ActionListener{
  private JFrame frame;
  private JButton button;
  private JTextArea area;
  private int index = ;
  private String [] params;
  
  private UIDemo(String args[]){
  params = args;
  area = new JTextArea();
  button = new JButton(Click here!);
  buttonaddActionListener(this);
  
  frame = new JFrame(getClass()getName());
  frameaddWindowListener(new ReusableWindowAdapter());
  Container cont = framegetContentPane();
  JScrollPane scrollPane = new JScrollPane(area);
  contadd(scrollPaneBorderLayoutNORTH);
  contadd(buttonBorderLayoutSOUTH);
  framepack();
  framesetVisible(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(aegetSource()equals(button)){
  index = ++index % paramslength;
  areasetText(params[index]);
  }
  }
  
  public static void main(String args[]){
  if(argslength > ){
  UIDemo one = new UIDemo(args);
  }else{
  usage();
  }
  }
  private static void usage(){
  Systemerrprintln(You may excute this program as below:);
  Systemerrprintln(java UIDemo params params );
  Systemexit();
  }
  }
  
  //UIDemo
  import javaawt*;
  import javaawtevent*;
  import javaxswing*;
  
  public class UIDemo{
  private JFrame frame;
  private JButton button;
  private JTextArea area;
  private int index = ;
  private String [] params;
  
  private UIDemo(String args[]){
  params = args;
  area = new JTextArea();
  button = new JButton(Click Here!);
  buttonaddActionListener(new SemiReusableActionListener(this));
  
  frame = new JFrame(getClass()getName());
  frameaddWindowListener(new ReusableWindowAdapter());
  Container cont = framegetContentPane();
  JScrollPane scrollPane = new JScrollPane(area);
  contadd(scrollPaneBorderLayoutNORTH);
  contadd(buttonBorderLayoutSOUTH);
  framepack();
  framesetVisible(true);
  }
  void setMessage(Object source){
  if(sourceequals(button)){
  index = ++index % paramslength;
  areasetText(params[index]);
  }
  }
  
  public static void main(String args[]){
  if(argslength > ){
  UIDemo two = new UIDemo(args);
  }else{
  usage();
  }
  }
  private static void usage(){
  Systemerrprintln(You may excute this program as below:);
  Systemerrprintln(java UIDemo params params );
  Systemexit();
  }
  }
  
  //SemiReusableActionListener
  
  import javaawtevent*;
  
  public class SemiReusableActionListener implements ActionListener{
  private UIDemo demo;
  SemiReusableActionListener(UIDemo uiDemo){
  demo = uiDemo;
  }
  public void actionPerformed(ActionEvent ae){
  demosetMessage(aegetSource());
  }
  }
  
  //UIDemo
  import javaawt*;
  import javaawtevent*;
  import javaxswing*;
  import javalangreflect*;
  
  public class UIDemo{
  private JFrame frame;
  private JButton button;
  private JTextArea area;
  private int index = ;
  private String [] params;
  private UIDemo(String args[]){
  params = args;
  area = new JTextArea();
  button = new JButton(Click here!);
  //setup required information to use GenericActionListener
  String methodName = setMessage;
  Class [] paramTypes = {javalangObjectclass};
  Object [] methodArgs = { button };
  Class clazz = thisgetClass();
  try{
  Method method = clazzgetMethod(methodName paramTypes);
  
  buttonaddActionListener(new
  ReusableActionListener(method this methodArgs));
  }
  catch(Exception e){
  Systemoutprintln(Could not find the method: +methodName+\nNow Exiting);
  Systemexit();
  }
  
  frame = new JFrame(getClass()getName());
  frameaddWindowListener(new ReusableWindowAdapter());
  Container cont = framegetContentPane();
  JScrollPane scrollPane = new JScrollPane(area);
  contadd(scrollPaneBorderLayoutNORTH);
  contadd(buttonBorderLayoutSOUTH);
  framepack();
  framesetVisible(true);
  }
  public void setMessage(Object source){
  if(sourceequals(button)){
  index = ++index % paramslength;
  areasetText(params[index]);
  }
  }
  public static void main(String args[]){
  if(argslength > ){
  UIDemo three = new UIDemo(args);
  }else{
  usage();
  }
  }
  private static void usage(){
  Systemerrprintln(You may excute this program as below:);
  Systemerrprintln(java UIDemo params params );
  Systemexit();
  }
  }
  
  //ReusableWindowAdapter
  
  import javaawt*;
  import javaawtevent*;
  
  public class ReusableWindowAdapter extends WindowAdapter{
  public void windowClosing(WindowEvent we){
  Object source = wegetSource();
  if(source instanceof Frame){
  ((Frame)source)setVisible(false);
  ((Frame)source)dispose();
  Systemexit();
  }
  }
  }
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26408.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.