像我们这种程(diao)序(si)员(nan),虽然是足够聪明能够知道基本是不存在被女神看上的概率的,但是还是甘于做备胎,对女神唯命是从(最起码女神有事的时候还是挺温柔的……),那么女神(Invoker)的请求自然也就是命令了,而往往程序员会利用身边同样无辜的程序员分工合作达到女神的要求
代码实现如下:
* Created by liulin on 16-4-25.
*/
interface Receiver{
void code();
}
interface Command{
void execute();
}
class DBA implements Receiver{
@Override
public void code() {
System.out.println("Database Ready!");
}
}
class Javaer implements Receiver{
@Override
public void code() {
System.out.println("Background Finish!");
}
}
class Designer implements Receiver{
@Override
public void code() {
System.out.println("Html done!!!");
}
}
class WebAppRequirement implements Command{
private Receiver a = new Designer();
private Receiver b = new Javaer();
private Receiver c = new DBA();
@Override
public void execute() {
a.code();
b.code();
c.code();
System.out.println("The WebApp done!");
}
}
class Invoker{
private Command command;
public void action (){
this.command.execute();
}
public void setCommand(Command command) {
this.command = command;
}
}
public class CommandTest {
public static void main ( String [] args ){
Invoker goddess = new Invoker();
goddess.setCommand( new WebAppRequirement());
goddess.action();
}
}
效果如下:
Tomcat作为一个应用服务器,无疑会收到很多请求,如何分配和执行这些请求是必须的功能,这部分功能主要靠Connector和Container组件实现。
Java设计模式(六) Command(命令模式)及Tomcat引申
原文:http://blog.csdn.net/qq_24451605/article/details/51249670