- Java EE 8 Design Patterns and Best Practices
- Rhuan Rocha Jo?o Purifica??o
- 128字
- 2021-07-23 16:54:54
Implementing the commands
Here, we have AbstractCommand, which is an abstract class with one execute method. This is the abstract command, and the execute method is implemented on the subclasses:
public abstract class AbstractCommand {
public abstract String execute();
}
In the following code snippet, we have the subclass HomeCommand, which is the implementation of AbstractCommand. The method execute() returns the path to the home page (/home.jsp):
public class HomeCommand extends AbstractCommand {
@Override
public String execute() {
return "/home.jsp";
}
}
Here, we have the LoginCommand subclass, which is the implementation of AbstractCommand. The execute() method returns the path to the login page (/login.jsp):
public class LoginCommand extends AbstractCommand {
@Override
public String execute() {
return "/login.jsp";
}
}