Save each class in a separate file with the class name and execute the Main.java program. Some classes are not used in the main execution. In later versions of this design I will include them.
----------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;
//This class(Main.java) is client part of command design pattern
public class Main {
public static void main(String args[]) {
CommandManager mgr = new CommandManager();
// CommandManager is Invoker part of command design pattern
Command tomcat = new TomcatStartCommand();
// In TomcatStartCommand class I used Tomcat class. Here Tomcat class is
// the receiver part of command design pattern
Command serviceMix = new ServiceMixStartCommand();
mgr.addCommand(tomcat);
mgr.addCommand(serviceMix);
mgr.runCommands();
}
}
-----------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;
public interface Command {
public void execute();
}
-----------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;
public class CommandException extends Exception {
private String message;
public CommandException(String message){
super(message);
this.message=message;
}
public CommandException(){
super();
}
public void setMessage(String message){
this.message = message;
}
public String getMessage(){
return message;
}
}
-----------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;
import java.util.ArrayList;
public class CommandManager {
private List<Command> commandList = new ArrayList();
public void runCommands() {
for (Command c : commandList) {
c.execute();
}
}
public void addCommand(Command command) {
commandList.add(command);
}
}
class TomcatStartCommand implements Command {
Tomcat tomcat = new Tomcat();
@Override
public void execute() {
tomcat.startTomcat();
}
}
class TomcatStopCommand implements Command {
Tomcat tomcat = new Tomcat();
@Override
public void execute() {
tomcat.stopTomcat();
}
}
class ServiceMixStopCommand implements Command {
ServiceMix serviceMix = new ServiceMix();
@Override
public void execute() {
serviceMix.stopServiceMix();
}
}
class ServiceMixStartCommand implements Command {
ServiceMix serviceMix = new ServiceMix();
@Override
public void execute() {
serviceMix.startServiceMix();
}
}
----------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;
public class ServiceMix {
public boolean stopServiceMix(){
System.out.println("Service Mix is stopped");
return true;
}
public boolean startServiceMix(){
System.out.println("Service Mix is started");
return true;
}
}
------------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;
public class Status {
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
------------------------------------------------------------------------------------------------------------
package org.learn.designpatterns.command;
public class Tomcat {
public boolean stopTomcat(){
System.out.println("Tomcat Stopped");
return true;
}
public boolean startTomcat(){
System.out.println("Tomcat Started");
return true;
}
}
------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment