Here is the example code snippet from Bruce Eckel's Thinking In Patterns.
// CommandPattern.java
import java.util.*;
import com.bruceeckel.test.*;
interface Command {
void execute();
}
class Hello implements Command {
public void execute() {
System.out.print("Hello ");
}
}
class World implements Command {
public void execute() {
System.out.print("World! ");
}
}
class IAm implements Command {
public void execute() {
System.out.print("I'm the command pattern!");
}
}
// An object that holds commands:
class Macro {
private List commands = new ArrayList();
public void add(Command c) { commands.add(c); }
public void run() {
Iterator it = commands.iterator();
while(it.hasNext())
((Command)it.next()).execute();
}
}
public class CommandPattern extends UnitTest {
Macro macro = new Macro();
public void test() {
macro.add(new Hello());
macro.add(new World());
macro.add(new IAm());
macro.run();
}
public static void main(String args[]) {
new CommandPattern().test();
}
} ///:~
----- Original Message -----
From: "jayaraman sureshkumar" <[EMAIL PROTECTED]>
To: "JDJList" <[EMAIL PROTECTED]>
Sent: Tuesday, June 18, 2002 4:03 PM
Subject: [jdjlist] Command design pattern
> Hello,
>
> I need to know a simple example explaining the subject design pattern in a
> nut shell.. please help..
>
> Thanks
>
> Suresh
>
> _________________________________________________________________
> Get your FREE download of MSN Explorer at
http://explorer.msn.com/intl.asp.
>
>
> To change your membership options, refer to:
> http://www.sys-con.com/java/list.cfm
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm