Sunday, November 4, 2012

Command Design Pattern

The UML diagram describing relations between o...The UML diagram describing relations between objects of the Command design pattern. (Photo credit: Wikipedia)
The UML diagram which describes the structure ...The UML diagram which describes the structure of the Command design pattern. (Photo credit: Wikipedia)


public interface Computer {
   void start();
   void shutdown();
}

public class Laptop implements Computer {
   public void start() {
       System.out.print("Laptop is started");
    }

    public void shutdown() {
       System.out.print("Laptop is shutdown");
    }

}

public interface Command {
   void execute();
   void undo();
}

public class StartLaptop implements Command {
   Computer theComputer;
   
    public StartLaptop (Computer newComputer ) {
        theComputer = newComputer;
    }

    public void execute() {
        theComputer.start();
    }
    
    public void undo() {
        theComputer.shutdown();
    }         
}

Below is the video tutorial about the Command pattern. 


No comments:

Post a Comment