北辰

c/c++/java程序

java设计模式进阶_chain

这里写图片描述



public enum RequestType {

    DEFEND_CASTLE,
    TORTURE_PRISONER,
    COLLECT_TAX
}



//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : RequestHandler.java
//  @ Date : 2016/8/29
//  @ Author : 
//
//




public abstract class RequestHandler {
    private RequestHandler next;
    public RequestHandler(RequestHandler next) {
        this.next = next;
    }

    public void handleRequest(Request request) {
        if(next != null)
        {
            next.handleRequest(request);
        }
    }

    protected void printHandling(Request request) {
        System.out.println(this + " handling request \"" + request + "\"");
    }

    public abstract String toString();
}


//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : OrcCommander.java
//  @ Date : 2016/8/29
//  @ Author : 
//
//




public class OrcCommander extends RequestHandler {
    public OrcCommander(RequestHandler next) {
        super(next);
    }

    public void handleRequest(Request request) {
        if(request.getRequestType().equals(RequestType.DEFEND_CASTLE)){
            printHandling(request);
        } else {
            super.handleRequest(request);
        }
    }

    public String toString() {
        return "Orc commander";
    }
}
//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : OrcOfficer.java
//  @ Date : 2016/8/29
//  @ Author : 
//
//




public class OrcOfficer extends RequestHandler {

    public OrcOfficer(RequestHandler next) {
        super(next);
    }

    public void handleRequest(Request request) {
        if(request.getRequestType().equals(RequestType.TORTURE_PRISONER)) {
            printHandling(request);
        } else {
            super.handleRequest(request);
        }
    }

    public String toString() {
        return "Orc officer";
    }
}

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : OrcSoldler.java
//  @ Date : 2016/8/29
//  @ Author : 
//
//




public class OrcSoldler extends RequestHandler {

    public OrcSoldler(RequestHandler next) {
        super(next);
    }

    public void handleRequest(Request request) {
        if(request.getRequestType().equals(RequestType.COLLECT_TAX)) {
            printHandling(request);
        } else {
            super.handleRequest(request);
        }
    }

    public String toString() {
        return "Orc soldier";
    }
}

//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : OrcKing.java
//  @ Date : 2016/8/29
//  @ Author : 
//
//




public class OrcKing {
    public RequestHandler chain;

    public OrcKing() {
        buildChain();
    }

    private void buildChain() {
        chain = new OrcCommander(new OrcOfficer(new OrcSoldler(null)));
    }

    public void makeRequest(Request request) {
        chain.handleRequest(request);
    }
}


//
//
//  Generated by StarUML(tm) Java Add-In
//
//  @ Project : Untitled
//  @ File Name : Request.java
//  @ Date : 2016/8/29
//  @ Author : 
//
//




public class Request {
    private String requestDescription;
    private RequestType requestType;

    public Request(RequestType requestType, String requestDescription) {
        this.setRequestType(requestType);
        this.setRequestDescription(requestDescription);
    }

    public String getRequestDescription() {
        return requestDescription;
    }

    public void setRequestDescription(String requestDescription) {
        this.requestDescription = requestDescription;
    }

    public RequestType getRequestType() {
        return requestType;
    }

    public void setRequestType(RequestType requestType) {
        this.requestType = requestType;
    }

    public String toString() {
        return getRequestDescription();
    }
}


public class App {

    public static void main(String[] args) {
        OrcKing king = new OrcKing();
        king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle"));
        king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner"));
        king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax"));
    }
}

/*Orc commander handling request "defend castle"
Orc officer handling request "torture prisoner"
Orc soldier handling request "collect tax"

*/




阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wwww_com/article/details/52355059
文章标签: java设计模式
个人分类: java设计模式进阶
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

java设计模式进阶_chain

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭