📄 command.html
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META name="description" content="Java,JDBC,EJB,Open Source,jdk,rmi">
<meta name="Keywords" content="Java, servlets, Java servlet, Javascript, ActiveX, VRML,
applet, applets, directory, news, jdbc, applications,
Java applications, Java developer, Java development, developer,
classes, Jars.com, Jars, intranet, Java applet, Javabeans,
Java products, JDK, Java development kit, java development environment, JIT,
JavaPlan, enterprise tools, JVM, Java Virtual Machine, Java resources,
SUN, CGI, Perl, database, network, html,
xml, dhtml, rating, ratings, review, jars, cgi, programming,
software review, software rating">
<title>csdn_Command 模式</title>
<style>
.news { BACKGROUND: #007cd3; font-family: "宋体"; font-size: 9pt }
.t { font-family: "宋体"; font-size: 9pt }
.t1 { color:#007cd3; font-family: "宋体"; font-size: 9pt }
.white { font-family: "宋体"; font-size: 9pt;color:#FFFFFF }
.red { font-family: "宋体"; font-size: 9pt;color:#FF0000 }
A {text-decoration: underline}
A:visited {color:#0000FF}
A:hover {color: #ff6666; text-decoration: none}
.text {font-size: 12px; line-height: 160%; font-family: "宋体"}
.text1 {color:#000000; font-size: 12px; line-height: 130%; font-family: "宋体"; text-decoration: none}
.text1:visited {color:#000000}
.text1:hover {color: #000000}
.text2 {color:#000000; font-size: 12px; line-height: 130%; font-family: "宋体"; text-decoration: none}
.text2:visited {color:#000000}
.text2:hover {color: #000000}
.text3 {font-size: 12px; line-height: 100%; font-family: "宋体"; text-decoration: none}
.large {font-size: 14.8px; line-height: 130%}
</style>
</head>
<!--start first table -->
<TR>
<TD WIDTH="20%" VALIGN="TOP">
<TR>
<TD WIDTH="20%" CLASS="t" BGCOLOR="#007cd3" align="Center"></TD>
<TR>
<TR>
<TD WIDTH="20%" CLASS="t" align="center" >
</TD>
<TR>
</td>
<TD WIDTH="100%" VALIGN="TOP" >
<TR>
<TD WIDTH="100%" CLASS="white"></TD>
<TR>
<TR>
<TD WIDTH="50%" bordercolor="#FFFFFF" CLASS="t1" bgcolor="#F0F0F0" nowrap >Command 模式 (2001-04-18)
</TD>
<p> <TD WIDTH="50%" bordercolor="#FFFFFF" CLASS="t1" bgcolor="#F0F0F0" nowrap >作者:jdeveloper
</TD> </p>
<TR>
<TR>
<TD WIDTH="100%" bordercolor="#FFFFFF" CLASS="t" bgcolor="#F0F0F0" colspan=2>
<pre>
<pre>
用途:
1 将一个请求封装为一个对象,实现请求的顺序控制、选择和时序控制
2 实现对请求的Undo和Redo操作
3 需要进行请求的日志操作
结构:
<img src="command.jpg" tppabs="http://www.chinajavaworld.com/doc/dp/images/command.jpg">
例子:
<p align=center>Command 模式 Demo Applet
<APPLET
CODEBASE = "../../tppjava" tppabs="http://www.chinajavaworld.com/doc/dp/"
ARCHIVE = "doc/dp/command.jar" tppabs="http://www.chinajavaworld.com/doc/dp/command.jar"
CODE = "jdeveloper.patterns.command.MainApplet.class"
NAME = "TestApplet"
WIDTH = 400
HEIGHT = 50
HSPACE = 0
VSPACE = 0
ALIGN = top
>
</APPLET>
</p>
解说:
Client --> MainApplet
Invoker--> Button Invoker
Command --> ActionListener Execute--> actionPerformed()
ConcreteCommnand --> Command1,Command2 Execute--> actionPerformed()
Receiver --> textField1,textField2 Action--> setText()
注:--> 表示对应关系
<a href="MainApplet.java" tppabs="http://www.chinajavaworld.com/doc/dp/MainApplet.java">下载源码</a>
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");
}
}
}
<a href="MainApplet.java" tppabs="http://www.chinajavaworld.com/doc/dp/MainApplet.java">下载源码</a>
</pre>
</TD>
<TR>
</TD>
</TR>
<!--end first table -->
</center>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -