您的位置:建站学院首页 >> 文章 >> JSP技术 >> 设计模式
Commend模式

用途:
   1 将一个请求封装为一个对象,实现请求的顺序控制、选择和时序控制
   2 实现对请求的Undo和Redo操作
   3 需要进行请求的日志操作
结构:

解说:
Client -->  MainApplet
Invoker-->  Button Invoker
Command --> ActionListener Execute--> actionPerformed()
ConcreteCommnand --> Command1,Command2  Execute--> actionPerformed()
Receiver  -->  textField1,textField2 Action--> setText()

Source:
package jdeveloper.patterns.command;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;


/**
* Title:        Jdeveloper's Java Projdect
* Description:  n/a
* Copyright:    Copyright (c) 2001
* Company:      soho
* @author jdeveloper
* @version 1.0
*/

public class MainApplet extends Applet {
  boolean isStandalone = false;
  Button Invoker = new Button();
  FlowLayout flowLayout1 = new FlowLayout();
  TextField textField1 = new TextField();
  TextField textField2 = new TextField();
  /**Get a parameter value*/
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  /**Construct the applet*/
  public MainApplet() {
  }
  /**Initialize the applet*/
  public void init() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  /**Component initialization*/
  private void jbInit() throws Exception {
    Invoker.setLabel("Invoker");
    this.setLayout(flowLayout1);
    textField2.setColumns(8);
    textField2.setEditable(false);

    textField1.setColumns(8);
    textField1.setEditable(false);

    this.add(Invoker, null);
    this.add(textField1, null);
    this.add(textField2, null);
    Command1 cmd1 = new Command1();
    Command2 cmd2 = new Command2();

    Invoker.addActionListener(cmd1);

    Invoker.addActionListener(cmd2);


  }
  /**Start the applet*/
  public void start() {
  }
  /**Stop the applet*/
  public void stop() {
  }
  /**Destroy the applet*/
  public void destroy() {
  }
  /**Get Applet information*/
  public String getAppletInfo() {
    return "Applet Information";
  }
  /**Get parameter info*/
  public String[][] getParameterInfo() {
    return null;
  }
  /**Main method*/
  public static void main(String[] args) {
    MainApplet applet = new MainApplet();
    applet.isStandalone = true;
    Frame frame;
    frame = new Frame() {
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }
      public synchronized void setTitle(String title) {
        super.setTitle(title);
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
      }
    };
    frame.setTitle("Applet Frame");
    frame.add(applet, BorderLayout.CENTER);
    applet.init();
    applet.start();
    frame.setSize(400,320);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
    frame.setVisible(true);
  }

  class Command1 implements ActionListener{
    public void actionPerformed(ActionEvent e){
       textField1.setText("Hello");
    }
  }
  class Command2 implements ActionListener{
    public void actionPerformed(ActionEvent e){
       textField2.setText("World");
    }
  }


Google
 
Web www.cqxw.net