티스토리 뷰

Java

CoR design pattern another version

techbard 2016.07.15 23:59

CoR.zip



# Handler.java


public abstract class Handler {

    protected Handler nextHandler;


    public void setNextHandler(Handler handler) {

this.nextHandler = handler;

    }


    public abstract void processRequest(Request request);

}



# Request.java


public class Request {

    private int requestValue = 3;


    public int getRequest() {

        return requestValue;

    }

    

    public void setRequest(int value) {

this.requestValue = value;

    }

}



# ProcessZero.java


public class ProcessZero extends Handler {


    @Override

    public void processRequest(Request request) {

if (request.getRequest() == 0)

   System.out.println("request value is zero.");

else

   nextHandler.processRequest(request);

    }

}



# ProcessPlus.java


public class ProcessPlus extends Handler {


    @Override

    public void processRequest(Request request) {

if (request.getRequest() > 0)

   System.out.println("request value is plus.");

else

   nextHandler.processRequest(request);

    }

}



# ProcessMinus.java


public class ProcessMinus extends Handler {


    @Override

    public void processRequest(Request request) {

if (request.getRequest() < 0)

   System.out.println("request value is minus.");

else

   nextHandler.processRequest(request);

    }

}



# Main.java


public class Main {

    public static void main(String[] args) {

Request request = new Request();

ProcessZero processZero = new ProcessZero();

ProcessMinus processMinus = new ProcessMinus();

ProcessPlus processPlus = new ProcessPlus();

processZero.setNextHandler(processMinus);

processMinus.setNextHandler(processPlus);

request.setRequest(0);

processZero.processRequest(request);

request.setRequest(1);

processZero.processRequest(request);

request.setRequest(-1);

processZero.processRequest(request);

    }

}



# 결과


request value is zero.

request value is plus.

request value is minus.


댓글
댓글쓰기 폼
공지사항
Total
394,929
Today
0
Yesterday
28
«   2018/11   »
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30  
글 보관함