總述
其實(shí)這個(gè)模式很好理解,非常簡(jiǎn)單。就是前端一股腦地接收了一大堆請(qǐng)求,然后它根據(jù)各個(gè)請(qǐng)求的不同進(jìn)行相應(yīng)的分派,聽上去有點(diǎn)像路由器的意思,實(shí)際上也差不多。
類圖

類圖.png
調(diào)用
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
FrontController controller = new FrontController();
controller.receiveRequest(0);
controller.receiveRequest(1);
controller.receiveRequest(2);
controller.receiveRequest(3);
}
}
輸出
視圖1處理了請(qǐng)求
視圖2處理了請(qǐng)求
視圖3處理了請(qǐng)求
請(qǐng)求無(wú)效
Process finished with exit code 0
前端控制器
package com.company;
public class FrontController {
private Dispatcher dispatcher = new Dispatcher();
public void receiveRequest(int requestID) {
this.dispatcher.dispatchRequest(requestID);
}
}
分派器
package com.company;
public class Dispatcher {
private ViewInterface[] views = {new View1(),new View2(),new View3()};
public void dispatchRequest(int requestIndex) {
if (requestIndex > -1 && requestIndex < views.length) {
this.views[requestIndex].processRequest();
} else {
System.out.println("請(qǐng)求無(wú)效");
}
}
}
視圖接口
package com.company;
public interface ViewInterface {
public void processRequest();
}
視圖1
package com.company;
public class View1 implements ViewInterface {
@Override
public void processRequest() {
System.out.println("視圖1處理了請(qǐng)求");
}
}
視圖2
package com.company;
public class View2 implements ViewInterface {
@Override
public void processRequest() {
System.out.println("視圖2處理了請(qǐng)求");
}
}
視圖3
package com.company;
public class View3 implements ViewInterface {
@Override
public void processRequest() {
System.out.println("視圖3處理了請(qǐng)求");
}
}
多謝捧場(chǎng)
如果您覺得我的文章有價(jià)值,那么賞臉打賞一個(gè),鄙人感激不盡。不過,不打賞看看也是好的,如果有不對(duì)的地方,還請(qǐng)您多多指正。