目前
結合command設計模式和Java語言的反射技術
在面向對象的系統設計中
command設計模式的主要設計思想是把對某一對象的請求封裝為一個對象
JDK
//UIDemo
import java
import java
import javax
public class UIDemo
private JFrame frame;
private JButton button;
private JTextArea area;
private int index =
private String [] params;
private UIDemo
params = args;
area = new JTextArea(
button = new JButton(
button
frame = new JFrame(getClass()
frame
Container cont = frame
JScrollPane scrollPane = new JScrollPane(area);
cont
cont
frame
frame
}
public void actionPerformed(ActionEvent ae){
//provide equality check to see if source was the
//button defined above
//this ActionListener with multiple sources
if(ae
index = ++index % params
area
}
}
public static void main(String args[]){
if(args
UIDemo
}else{
usage();
}
}
private static void usage(){
System
System
System
}
}
//UIDemo
import java
import java
import javax
public class UIDemo
private JFrame frame;
private JButton button;
private JTextArea area;
private int index =
private String [] params;
private UIDemo
params = args;
area = new JTextArea(
button = new JButton(
button
frame = new JFrame(getClass()
frame
Container cont = frame
JScrollPane scrollPane = new JScrollPane(area);
cont
cont
frame
frame
}
void setMessage(Object source){
if(source
index = ++index % params
area
}
}
public static void main(String args[]){
if(args
UIDemo
}else{
usage();
}
}
private static void usage(){
System
System
System
}
}
//SemiReusableActionListener
import java
public class SemiReusableActionListener implements ActionListener{
private UIDemo
SemiReusableActionListener(UIDemo
demo
}
public void actionPerformed(ActionEvent ae){
demo
}
}
//UIDemo
import java
import java
import javax
import java
public class UIDemo
private JFrame frame;
private JButton button
private JTextArea area;
private int index =
private String [] params;
private UIDemo
params = args;
area = new JTextArea(
button
//setup required information to use GenericActionListener
String methodName =
Class [] paramTypes = {java
Object [] methodArgs = { button
Class clazz = this
try{
Method method = clazz
button
ReusableActionListener(method
}
catch(Exception e){
System
System
}
frame = new JFrame(getClass()
frame
Container cont = frame
JScrollPane scrollPane = new JScrollPane(area);
cont
cont
frame
frame
}
public void setMessage(Object source){
if(source
index = ++index % params
area
}
}
public static void main(String args[]){
if(args
UIDemo
}else{
usage();
}
}
private static void usage(){
System
System
System
}
}
//ReusableWindowAdapter
import java
import java
public class ReusableWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent we){
Object source = we
if(source instanceof Frame){
((Frame)source)
((Frame)source)
System
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26408.html