Five ‘s Weblog

November 14, 2007

Command Pattern

Filed under: Programming — by powerdream5 @ 10:04 pm
Tags:

       I am not going to introduce the definition of Command Pattern, as well as the advantage and disadvantage of it. You can find many articles about that on the internet. Here, I just wanna give an example and realiz it by command pattern. (command pattern in wiki)

       There are two cleaner. One is responsible for sweeping the floor, and the other one is responsible for rubbing the table.  However, both of them are lazy. They never do their job until the supervisor commands them.

       There is no doubt it is suitable for command pattern to realize this problem. The following is my code:

//the command interface
public interface Command{
       public void executeCommand();
}

//concrete command 1
public class RubTableCommand implements Command{
       private Worker worker;
       public RubTableCommand(Worker worker){
             this.worker = worker;
       }

       public void executeCommand(){
            worker.doJob();
       }
}

//concrete command 2
public class SweepFloorCommand implements Command{
       private Worker worker;
       public SweepFloorCommand(Worker worker){
             this.worker = worker;
       }

       public void executeCommand(){
             worker.doJob();
       }
}

//the receiver interface
public interface Worker{
       public void doJob();

//receiver 1
public class CleanerForRubTable implements Worker{
 
      public void doJob(){
          System.out.println(“Rubbing floor”);
      }
}

//receiver 2
public class CleanerForSweepFloor implements Worker{
 
      public void doJob(){
         System.out.println(“Sweeping floor”);
      }
}

//the invoker
public class InvokeCommand{
       private Command command;

       public void setCommand(Command command){
            this.command = command;
       }

       public void sendCommand(){
            command.executeCommand();
       }
}

 //the client
public class Supervisor{
 
       public static void main(String[] args){
             Worker cleanerForSweepFloor = new CleanerForSweepFloor();
             Worker cleanerForRubTable = new CleanerForRubTable();
  
             InvokeCommand ic = new InvokeCommand();
             Command command = new RubTableCommand(cleanerForRubTable);
             ic.setCommand(command);
             ic.sendCommand();

            command = new SweepFloorCommand(cleanerForSweepFloor);
            ic.setCommand(command);
            ic.sendCommand();  
       }
}

11_14_1_2007.jpg

Advertisements

1 Comment »

  1. Are you a photographer too ?
    just curious 🙂

    Comment by kkevin13 — December 26, 2008 @ 4:06 pm |Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: