public class OddSupport extends Support{ public OddSupport(String name){ super(name); } protected boolean resolve(Trouble trouble){ if(trouble.getNumber() % 2 == 1){ return true; }else{ return false; } } }
SpecialSupport
public class SpecialSupport extends Support{ private int number; public SpecialSupport(String name, int number){ super(name); this.number = number; } protected boolean resolve(Trouble trouble){ if(trouble.getNumber() == number) return true; return false; } }
main.java
next 메소드를 통해 Support를 반환해주는데 줄줄이 등록하는 구조입니다.
public class Main { public static void main(String[] args){ Support limitSupport = new LimitSupport("limit",200); Support notSupport = new NotSupport("not"); Support oddSupport = new OddSupport("odd"); Support specialSupport = new SpecialSupport("special", 429); limitSupport.setNext(notSupport).setNext(oddSupport).setNext(specialSupport); for(int i = 0; i < 500; i++){ limitSupport.support(new Trouble(i)); } } }