您现在的位置是:首页 >

tcp复用分用 Java中可复用事件处理的设计与实现代码

火烧 2022-05-23 01:33:36 1064
Java中可复用事件处理的设计与实现代码   目前 面向对象是软件系统建模的主流技术 使用面向对象技术建模的主要指标之一是可复用性 为了更好地解决软件复用性和扩展性问题 设计模式得到了越来越多的关注与

Java中可复用事件处理的设计与实现代码  

tcp复用分用 Java中可复用事件处理的设计与实现代码
  目前 面向对象是软件系统建模的主流技术 使用面向对象技术建模的主要指标之一是可复用性 为了更好地解决软件复用性和扩展性问题 设计模式得到了越来越多的关注与应用     结合mand设计模式和Java语言的反射技术 本文设计实现了一个可复用的事件处理框架     在面向对象的系统设计中 有些方面的可复用性经常被忽略了 用户界面(User Interface 下文简称UI)及其事件处理就是其中之一 一个完整的UI设计应该包括两部分 UI及其相应的事件处理机制 没有事件处理机制的UI是没有用的 对于事件处理 也应该考虑可复用设计 虽然看上去有些奇怪 但是这种设计是有实用价值的——提高了代码的可复用性 健壮性和可维护性     mand设计模式的主要设计思想是把对某一对象的请求封装为一个对象 从而把发出命令的责任和执行任务的责任分开 委派给不同的对象 请求的一方不必知道接收请求一方的接口 这种引入第三方类的做法是设计模式所惯用的 引入的第三方类解耦了紧耦合对象 mand设计模式中 第三方类解耦了调用操作的对象与知道如何实现该操作的对象 提高了软件的可复用性     JDK 及其以后的版本 引入了反射(reflection)技术 反射是Java中非常突出的动态特征 利用它可以加载一个运行时才得知名字的class 获取其完整结构 这一切是通过反射API完成的     //UIDemo   import java awt *;  import java awt event *;  import javax swing *;    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! );  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 > ){  UIDemo one = new UIDemo (args);  }else{  usage();  }  }  private static void usage(){  System err println( You may excute this program as below: );  System err println( java UIDemo params params );  System exit( );  }  }    //UIDemo   import java awt *;  import java awt event *;  import javax swing *;    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! );  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 > ){  UIDemo o = new UIDemo (args);  }else{  usage();  }  }  private static void usage(){  System err println( You may excute this program as below: );  System err println( java UIDemo params params );  System exit( );  }  }    //SemiReusableActionListener    import java awt event *;    public class SemiReusableActionListener implements ActionListener{  private UIDemo demo ;  SemiReusableActionListener(UIDemo uiDemo){  demo = uiDemo;  }  public void actionPerformed(ActionEvent ae){  demo setMessage(ae getSource());  }  }    //UIDemo   import java awt *;  import java awt event *;  import javax swing *;  import java lang reflect *;    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 = {java lang Object class};  Object [] methodArgs = { button };  Class clazz = this getClass();  try{  Method method = clazz getMethod(methodName paramTypes);    button addActionListener(new  ReusableActionListener(method this methodArgs));  }  catch(Exception e){  System out println( Could not find the method: +methodName+ nNow Exiting );  System exit( );  }    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 setMessage(Object source){  if(source equals(button )){  index = ++index % params length;  area setText(params[index]);  }  }  public static void main(String args[]){  if(args length > ){  UIDemo three = new UIDemo (args);  }else{  usage();  }  }  private static void usage(){  System err println( You may excute this program as below: );  System err println( java UIDemo params params );  System exit( );  }  }    //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( );  }  }  } lishixinzhi/Article/program/Java/hx/201311/26408  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码