욱'S 노트

Chain of Responsibility - 처리자 연결하기 본문

Methdology/Design Pattern

Chain of Responsibility - 처리자 연결하기

devsun 2012. 4. 25. 17:46
When Using It

다수의 처리자들을 연결하고 싶을때

Class Diagram


Sample Code



public abstract class Handler {

private Handler nextHandler;

public abstract boolean handleRequest();

public final void setNext(Handler handler) {

this.nextHandler = handler;

}

public final void process() {

if (handleRequest() == false) {

if ( nextHandler != null) {

nextHandler.handleRequest();

}

}

}

}


public class ConcreteHandler extends Handler {

@Override

public boolean handleRequest() {

return false;

}

}


public class ConcreteHandler2 extends Handler {

@Override

public boolean handleRequest() {

return true;

}

}



public class Client {

public void process() {

Handler handler1 = new ConcreteHandler();

Handler handler2 = new ConcreteHandler2();

handler1.setNext(handler2);

handler1.process();

}

}


Caution


특별한 주의사항은 없다.
Comments