[자바 디자인패턴 소스코드] Observer Pattern

옵저버 패턴

/**
 * [옵저버 패턴]은 통보 대상 객체의 관리를 Subject 클래스와 Observer 인터페이스로 일반화한다.
 * 그러면 데이터 변경을 통보하는 클래스(ConcreteSubject)는
 * 통보 대상 클래스나 객체(ConcreteObserver)에 대한 의존성을 없앨 수 있다.
 * 결과적으로 옵저버 패턴은 통보 대상 클래스나 대상 객체의 변경에도
 * ConcreteSubject 클래스를 수정 없이 그대로 사용할 수 있도록 한다.
 */
public class ObserverPattern {
    public static void main(String[] args) {
        Recruit recruit = new Recruit();

        JobKorea jobKorea3 = new JobKorea(recruit, "잡코리아_편집자");
        SaramIn saramIn = new SaramIn(recruit);

        recruit.attach(jobKorea3);
        recruit.attach(saramIn);
        recruit.addJob("Programer");
        recruit.addJob("Teacher");

        System.out.println();
        System.out.println("--------------------");
        System.out.println();

        recruit.detach(jobKorea3);

        Wanted wanted = new Wanted(recruit);
        recruit.attach(wanted);
        recruit.addJob("Banker");
    }
}

interface Observer {
    public abstract void update();
}

abstract class Subject {
    private List<Observer> observers = new ArrayList<Observer>();

    public void attach(Observer observer) {
        observers.add(observer);
    }

    public void detach(Observer observer) {
        observers.remove(observer);
    }

    public void notifyObservers() {
        for(Observer o : observers) {
            o.update();
        }
    }
}

class Recruit extends Subject {
    private List<String> title = new ArrayList<String>();

    public void addJob(String title) {
        this.title.add(title);
        notifyObservers(); // ★각 옵저버에게 통보
    }

    public List<String> getTitles() {
        return title;
    }
}

class JobKorea implements Observer {
    private Recruit recruit;
    private String onlyJobKorea;

    public JobKorea(Recruit recruit, String onlyJobKorea) {
        this.recruit = recruit;
        this.onlyJobKorea = onlyJobKorea;
    }

    public void update() {
        List<String> recruitTitles = recruit.getTitles();
        displayJobKorea(recruitTitles);
    }

    public void displayJobKorea(List<String> recruitTitles) {
        System.out.println("[Job Korea] ");
        System.out.print(" - ");
        for(String item : recruitTitles) {
            System.out.print(item + " ");
        }
        System.out.println(onlyJobKorea);
    }
}

class SaramIn implements Observer {
    private Recruit recruit;

    public SaramIn(Recruit recruit) {
        this.recruit = recruit;
    }

    public void update() {
        List<String> recruitTitles = recruit.getTitles();
        displaySaramIn(recruitTitles);
    }

    private void displaySaramIn(List<String> recruitTitles) {
        System.out.println("[Saram In]");
        System.out.print(" - ");
        for(String item : recruitTitles) {
            System.out.print(item + " ");
        }
        System.out.println();
    }

}

class Wanted implements Observer {
        private Recruit recruit;

    public Wanted(Recruit recruit) {
        this.recruit = recruit;
    }

    public void update() {
        List<String> recruitTitles = recruit.getTitles();
        displayWanted(recruitTitles);
    }

    private void displayWanted(List<String> recruitTitles) {
        System.out.println("[Wanted]");
        System.out.print(" - ");
        for(String item : recruitTitles) {
            System.out.print(item + " ");
        }
        System.out.println();
    }
}

이 글을 공유하기

댓글

Designed by JB FACTORY