Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

Command Design Pattern

The Command Design Patterns main goal is to decouple the invoker from the Receiver.

The invoker is the one which invokes an action on the Receiver, and the receiver is the one on which the command is executed.

CommandPattern

// The Receiver 
package Command;

public class Movie {

public static void start() {
System.out.println("Start Movie");

}

public static void stop() {
System.out.println("Movie Stopped");
}

}






//Play Command
public class PlayMovieCommand implements CommandIntf {

public void execute() {
Movie.start();
}

}

//Stop Command
public class StopMovieCommand implements CommandIntf {

public void execute() {

Movie.stop();
}

}








//The Invoker
package Command;

public class MoviePlayer {
static CommandIntf play;

static CommandIntf stop;

public static void setPlayCmd(CommandIntf playMovCmd) {
play = playMovCmd;
}

public static void setStopCmd(CommandIntf stopMovCmd) {
stop = stopMovCmd;
}

public static void playMovie() {
play.execute();

}

public static void stopMovie() {
stop.execute();
}

}



No comments:

Post a Comment

Search 24 Bytes