ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 행위패턴 : Chain of Responsibility 패턴
    디자인패턴 2013. 2. 7. 09:30
    반응형

    분류 : 행위패턴(Behavioral Patterns)


    정의 :

    명령(command) 객체들과 프로세싱(processing) 객체들의 집합으로 구성된 디자인 패턴. 각각의 프로세싱객체들은 명령객체가 처리할수 있는 타입을 정의하는 로직을 포함.


    용도 :

    좀 더 느슨한 결합을 가능하게 만듬.




    소스

    
    public abstract class Logger {
    	public static int ERR = 3;
        public static int NOTICE = 5;
        public static int DEBUG = 7;
        protected int mask;
     
        //chain of responsibility의 다음 요소
        protected Logger next;
     
        public void setNext(Logger log) {
            next = log;
        }
     
        public void message(String msg, int priority) {
            if (priority <= mask) {
                writeMessage(msg);
            }
            if (next != null) {
                next.message(msg, priority);
            }
        }
     
        abstract protected void writeMessage(String msg);
    }
    
    
    public class StdoutLogger extends Logger {
    	public StdoutLogger(int mask) { 
            this.mask = mask;
        }
     
        protected void writeMessage(String msg) {
            System.out.println("Writing to stdout: " + msg);
        }
    }
    
    
    public class EmailLogger extends Logger {
    	public EmailLogger(int mask) {
            this.mask = mask;
        }
     
        protected void writeMessage(String msg) {
            System.out.println("Sending via email: " + msg);
        }
    }
    
    public class StderrLogger extends Logger {
    	public StderrLogger(int mask) {
            this.mask = mask;
        }
     
        protected void writeMessage(String msg) {
            System.err.println("Sending to stderr: " + msg);
        }
    }
    
    public class ChainOfResponsibilityExample {
    	private static Logger createChain() {
            // chain of responsibility 구성
     
            Logger logger = new StdoutLogger(Logger.DEBUG);
     
            Logger logger1 = new EmailLogger(Logger.NOTICE);
            logger.setNext(logger1);
     
            Logger logger2 = new StderrLogger(Logger.ERR);        
            logger1.setNext(logger2);
     
            return logger;
        }
     
        public static void main(String[] args) {
     
            Logger chain = createChain();
     
            // StdoutLogger (level = 7) 에서 처리됨
            chain.message("Entering function y.", Logger.DEBUG);
     
            // StdoutLogger 와 EmailLogger (level = 5)  에서 처리됨
            chain.message("Step1 completed.", Logger.NOTICE);
     
            // 3개의 logger 모두에서 처리됨. (level = 3)
            chain.message("An error has occurred.", Logger.ERR);
        }
    }
    
    


    참고자료

    http://www.oodesign.com/chain-of-responsibility-pattern.html

    http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern

    http://ko.wikipedia.org/wiki/Chain_of_responsibility_%ED%8C%A8%ED%84%B4

    반응형

    '디자인패턴' 카테고리의 다른 글

    행위패턴 : Interpreter 패턴  (0) 2013.02.27
    행위패턴 : Command 패턴  (0) 2013.02.26
    구조패턴 : Proxy 패턴  (0) 2013.02.06
    구조패턴 : Flyweight 패턴  (0) 2013.02.05
    구조패턴 : Facade 패턴  (0) 2013.01.29

    댓글

Designed by Tistory.