최근 포토로그


2008/09/16 23:30

[DesignPattern - Chain of Responsibility] sample1 study. note

Design Pattern - Chain of Responsibility

1. 클래스다이어그램
2. Source
  • Trouble.java
    package com.dazzilove.chainofresponsibility.sample1;

    public class Trouble {
    private int number;
    public Trouble(int number) {
    this.number = number;
    }
    public int getNumber() {
    return number;
    }
    public String toString() {
    return "[Trouble" + number + "]";
    }
    }
  • Support.java
    package com.dazzilove.chainofresponsibility.sample1;

    public abstract class Support {
    private String name;
    private Support next;
    public Support(String name) {
    this.name = name;
    }
    public Support setNext(Support next) {
    this.next = next;
    return next;
    }
    public final void support(Trouble trouble) {
    if(resolve(trouble)) {
    done(trouble);
    } else if(next != null) {
    next.support(trouble);
    } else {
    fail(trouble);
    }
    }
    public String toString() {
    return "[" + name + "]";
    }
    protected abstract boolean resolve(Trouble trouble);
    protected void done(Trouble trouble) {
    System.out.println(trouble + " is resolved by " + this + ".");
    }
    protected void fail(Trouble trouble) {
    System.out.println(trouble + " cannot be resolved.");
    }
    }
  • NoSupport.java
    package com.dazzilove.chainofresponsibility.sample1;

    public class NoSupport extends Support {
    public NoSupport(String name) {
    super(name);
    }
    @Override
    protected boolean resolve(Trouble trouble) {
    return false;
    }
    }
  • LimitSupport.java
    package com.dazzilove.chainofresponsibility.sample1;

    public class LimitSupport extends Support {
    private int limit;
    public LimitSupport(String name, int limit) {
    super(name);
    this.limit = limit;
    }
    @Override
    protected boolean resolve(Trouble trouble) {
    if(trouble.getNumber() < limit) {
    return true;
    } else {
    return false;
    }
    }
    }
  • OddSupport.java
    package com.dazzilove.chainofresponsibility.sample1;

    public class OddSupport extends Support {
    public OddSupport(String name) {
    super(name);
    }
    @Override
    protected boolean resolve(Trouble trouble) {
    if(trouble.getNumber() % 2 == 1) {
    return true;
    } else {
    return false;
    }
    }
    }
  • SpecialSupport.java
    package com.dazzilove.chainofresponsibility.sample1;

    public class SpecialSupport extends Support {
    private int number;
    public SpecialSupport(String name, int number) {
    super(name);
    this.number = number;
    }
    @Override
    protected boolean resolve(Trouble trouble) {
    if(trouble.getNumber() == number) {
    return true;
    } else {
    return false;
    }
    }
    }
  • Main.java
    package com.dazzilove.chainofresponsibility.sample1;

    public class Main {
    public static void main(String[] args) {
    Support alice = new NoSupport("Alice");
    Support bob = new LimitSupport("Bob", 100);
    Support charlie = new SpecialSupport("Charlie", 429);
    Support diana = new LimitSupport("Diana", 200);
    Support elmo = new OddSupport("Elmo");
    Support fred = new LimitSupport("Fred", 300);
    //연쇄의 형성
    alice.setNext(bob).setNext(charlie).setNext(diana).setNext(elmo).setNext(fred);
    //다양한 트러블 발생
    for(int i=0; i<500; i += 33) {
    alice.support(new Trouble(i));
    }
    }
    }
3. 결과출력

트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://dazzilove.egloos.com/tb/835718 [도움말]

핑백

  • dazzilove : 디자인 패턴 공부 2008-09-16 23:30:37 #

    ... sample1 : "Java 언어로 배우는 디자인 패턴 입문" Chapter13. Visitor- 구조 안을 돌아다니면서 일을 한다.Chain of Responsibility sample1 : "Java 언어로 배우는 디자인 패턴 입문" Chapter14. Chain of Responsibility- 책임 떠넘기기 ... more

덧글

덧글 입력 영역



미투데이

문화꽃 키우기