Skip to content

Latest commit

ย 

History

History
153 lines (104 loc) ยท 4.48 KB

Observer pattern.md

File metadata and controls

153 lines (104 loc) ยท 4.48 KB

์˜ต์ €๋ฒ„ ํŒจํ„ด(Observer pattern)

์ƒํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ฃผ์ฒด ๊ฐ์ฒด & ์ƒํƒœ์˜ ๋ณ€๊ฒฝ์„ ์•Œ์•„์•ผ ํ•˜๋Š” ๊ด€์ฐฐ ๊ฐ์ฒด

(1 ๋Œ€ 1 or 1 ๋Œ€ N ๊ด€๊ณ„)

์„œ๋กœ์˜ ์ •๋ณด๋ฅผ ์ฃผ๊ณ ๋ฐ›๋Š” ๊ณผ์ •์—์„œ ์ •๋ณด์˜ ๋‹จ์œ„๊ฐ€ ํด์ˆ˜๋ก, ๊ฐ์ฒด๋“ค์˜ ๊ทœ๋ชจ๊ฐ€ ํด์ˆ˜๋ก ๋ณต์žก์„ฑ์ด ์ฆ๊ฐ€ํ•˜๊ฒŒ ๋œ๋‹ค. ์ด๋•Œ ๊ฐ€์ด๋“œ๋ผ์ธ์„ ์ œ์‹œํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด '์˜ต์ €๋ฒ„ ํŒจํ„ด'


์ฃผ์ฒด ๊ฐ์ฒด์™€ ๊ด€์ฐฐ ๊ฐ์ฒด์˜ ์˜ˆ๋Š”?
์žก์ง€์‚ฌ : ๊ตฌ๋…์ž
์šฐ์œ ๋ฐฐ๋‹ฌ์—…์ฒด : ๊ณ ๊ฐ

๊ตฌ๋…์ž, ๊ณ ๊ฐ๋“ค์€ ์ •๋ณด๋ฅผ ์–ป๊ฑฐ๋‚˜ ๋ฐ›์•„์•ผ ํ•˜๋Š” ์ฃผ์ฒด์™€ ๊ด€๊ณ„๋ฅผ ํ˜•์„ฑํ•˜๊ฒŒ ๋œ๋‹ค. ๊ด€๊ณ„๊ฐ€ ์ง€์†๋˜๋‹ค๊ฐ€ ์ •๋ณด๋ฅผ ์›ํ•˜์ง€ ์•Š์œผ๋ฉด ํ•ด์ œํ•  ์ˆ˜๋„ ์žˆ๋‹ค. (์žก์ง€ ๊ตฌ๋…์„ ์ทจ์†Œํ•˜๊ฑฐ๋‚˜ ์šฐ์œ  ๋ฐฐ๋‹ฌ์„ ์ค‘์ง€ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ)

์ด๋•Œ, ๊ฐ์ฒด์™€์˜ ๊ด€๊ณ„๋ฅผ ๋งบ๊ณ  ๋Š๋Š” ์ƒํƒœ ๋ณ€๊ฒฝ ์ •๋ณด๋ฅผ Observer์— ์•Œ๋ ค์ค˜์„œ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค.


  • Publisher ์ธํ„ฐํŽ˜์ด์Šค

    Observer๋“ค์„ ๊ด€๋ฆฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ

    ์˜ต์ €๋ฒ„ ๋“ฑ๋ก(add), ์ œ์™ธ(delete), ์˜ต์ €๋ฒ„๋“ค์—๊ฒŒ ์ •๋ณด๋ฅผ ์•Œ๋ ค์คŒ(notifyObserver)

    public interface Publisher { 
        public void add(Observer observer); 
        public void delete(Observer observer); 
        public void notifyObserver(); 
    }

  • Observer ์ธํ„ฐํŽ˜์ด์Šค

    ์ •๋ณด๋ฅผ ์—…๋ฐ์ดํŠธ(update)

    public interface Observer { 
        public void update(String title, String news); }

  • NewsMachine ํด๋ž˜์Šค

    Publisher๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋กœ, ์ •๋ณด๋ฅผ ์ œ๊ณตํ•ด์ฃผ๋Š” ํผ๋ธ”๋ฆฌ์…”๊ฐ€ ๋จ

    public class NewsMachine implements Publisher {
        private ArrayList<Observer> observers; 
        private String title; 
        private String news; 
    
        public NewsMachine() {
            observers = new ArrayList<>(); 
        }
        
        @Override public void add(Observer observer) {
            observers.add(observer);
        }
        
        @Override public void delete(Observer observer) 	{ 
            int index = observers.indexOf(observer);
            observers.remove(index); 
        }
        
        @Override public void notifyObserver() {
            for(Observer observer : observers) {
               observer.update(title, news); 
            }
        } 
        
        public void setNewsInfo(String title, String news) { 
            this.title = title; 
            this.news = news; 
            notifyObserver(); 
        } 
        
        public String getTitle() { return title; } 		public String getNews() { return news; }
    }

  • AnnualSubscriber, EventSubscriber ํด๋ž˜์Šค

    Observer๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋“ค๋กœ, notifyObserver()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ์•Œ๋ ค์ค„ ๋•Œ๋งˆ๋‹ค Update๊ฐ€ ํ˜ธ์ถœ๋จ

    public class EventSubscriber implements Observer {
        
        private String newsString;
        private Publisher publisher;
        
        public EventSubscriber(Publisher publisher) {
            this.publisher = publisher;
            publisher.add(this);
        }
        
        @Override
        public void update(String title, String news) {
            newsString = title + " " + news;
            display();
        }
        
        public void withdraw() {
            publisher.delete(this);
        }
        
        public void display() {
            System.out.println("์ด๋ฒคํŠธ ์œ ์ €");
            System.out.println(newsString);
        }
        
    }


Java์—๋Š” ์˜ต์ €๋ฒ„ ํŒจํ„ด์„ ์ ์šฉํ•œ ๊ฒƒ๋“ค์„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณตํ•ด์คŒ

Observer ์ธํ„ฐํŽ˜์ด์Šค, Observable ํด๋ž˜์Šค

ํ•˜์ง€๋งŒ Observable์€ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ƒ์†์„ ํ•ด์•ผ ํ•จ. ๋”ฐ๋ผ์„œ ๋‹ค๋ฅธ ์ƒ์†์„ ํ•จ๊ป˜ ์ด์šฉํ•  ์ˆ˜ ์—†๋Š” ๋‹จ์  ์กด์žฌ



์ •๋ฆฌ

์˜ต์ €๋ฒ„ ํŒจํ„ด์€, ํ•œ ๊ฐ์ฒด์˜ ์ƒํƒœ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๊ทธ ๊ฐ์ฒด์— ์˜์กดํ•˜๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด๋“ค์—๊ฒŒ ์—ฐ๋ฝ์ด ๊ฐ€๊ณ , ์ž๋™์œผ๋กœ ์ •๋ณด๊ฐ€ ๊ฐฑ์‹ ๋˜๋Š” 1:N ๊ด€๊ณ„(ํ˜น์€ 1๋Œ€1)๋ฅผ ์ •์˜ํ•œ๋‹ค.

์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐํ•˜์—ฌ ๋Š์Šจํ•œ ๊ฒฐํ•ฉ์„ฑ์„ ์œ ์ง€ํ•˜๋ฉฐ, Publisher์™€ Observer ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ ์šฉํ•œ๋‹ค.

์•ˆ๋“œ๋กœ์ด๋“œ ๊ฐœ๋ฐœ์‹œ, OnClickListener์™€ ๊ฐ™์€ ๊ฒƒ๋“ค์ด ์˜ต์ €๋ฒ„ ํŒจํ„ด์ด ์ ์šฉ๋œ ๊ฒƒ (๋ฒ„ํŠผ(Publisher)์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ์ƒํƒœ ๋ณ€ํ™”๋ฅผ ์˜ต์ €๋ฒ„์ธ OnClickListener๋กœ ์•Œ๋ ค์ฃผ๋กœ๋… ํ•จ)


[์ฐธ๊ณ ]

๋งํฌ