Skip to content

Latest commit

ย 

History

History
174 lines (117 loc) ยท 5.92 KB

observer.md

File metadata and controls

174 lines (117 loc) ยท 5.92 KB

Observer(์˜ต์ €๋ฒ„) ๋””์ž์ธ ํŒจํ„ด

์ž‘์„ฑ์ž : ์žฅ์ฃผ์„ญ

Observer ํŒจํ„ด ๊ฐœ์š”

์˜ต์ €๋ฒ„ ํŒจํ„ด(observer pattern)์€ ๊ฐ์ฒด์˜ ์ƒํƒœ ๋ณ€ํ™”๋ฅผ ๊ด€์ฐฐํ•˜๋Š” ๊ด€์ฐฐ์ž๋“ค, ์ฆ‰ ์˜ต์ €๋ฒ„๋“ค์˜ ๋ชฉ๋ก์„ ๊ฐ์ฒด์— ๋“ฑ๋กํ•˜์—ฌ ์ƒํƒœ ๋ณ€ํ™”๊ฐ€ ์žˆ์„ ๋•Œ๋งˆ๋‹ค ๋ฉ”์„œ๋“œ ๋“ฑ์„ ํ†ตํ•ด ๊ฐ์ฒด๊ฐ€ ์ง์ ‘ ๋ชฉ๋ก์˜ ๊ฐ ์˜ต์ €๋ฒ„์—๊ฒŒ ํ†ต์ง€ํ•˜๋„๋ก ํ•˜๋Š” ๋””์ž์ธ ํŒจํ„ด์ด๋‹ค. ์ฃผ๋กœ ๋ถ„์‚ฐ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋ง ์‹œ์Šคํ…œ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. ๋ฐœํ–‰/๊ตฌ๋… ๋ชจ๋ธ๋กœ ์•Œ๋ ค์ ธ ์žˆ๊ธฐ๋„ ํ•˜๋‹ค.

์ถœ์ฒ˜: wikipedia: ์˜ต์ €๋ฒ„ ํŒจํ„ด

์ด ๋ง์„ ์ข€ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ํ’€์–ด์„œ ์–˜๊ธฐํ•˜์ž๋ฉด ObserverํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด ๋ฐ ์ธํ„ฐํŽ˜์ด์Šค๋กœ Observable(์ฃผ์ฒด)๊ณผ Observer ๋‘ ๊ฐ€์ง€๊ฐ€ ์กด์žฌํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ Observable๊ฐ์ฒด๋Š” ์ƒํƒœ๊ฐ€ ๋ณ€ํ™” ๋˜๋Š” ์ฃผ์ฒด์ด๊ณ  ์ด๋Ÿฌํ•œ ์ฃผ์ฒด๋ฅผ ๊ด€์ฐฐํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์ด Observer์ด๋‹ค. Observable์€ ํ•˜๋‚˜ ๋˜๋Š” ์—ฌ๋Ÿฌ Observer๋ฅผ ๋“ฑ๋กํ•˜๊ฑฐ๋‚˜ ํ•ด์ œํ•  ์ˆ˜ ์žˆ๊ณ  ์–ด๋– ํ•œ ์ƒํƒœ ๋ณ€ํ™”์‹œ Observer๋“ค์— ์•Œ๋ฆด ์ˆ˜ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ Observer๋“ค์€ ์ด๋Ÿฌํ•œ ์ƒํƒœ๋ณ€ํ™”์— ๋Œ€ํ•ด์„œ ์ „๋‹ฌ ๋ฐ›์œผ๋ฉด ๊ฐ๊ฐ์˜ ์•Œ๋งž๋Š” ํ–‰๋™์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

Observer ํŒจํ„ด ์žฅ์ 

  1. ํ•œ ๊ฐ์ฒด์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด์—์„œ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ์ „ํŒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  2. ๊ฐ์ฒด ๊ฐ„์˜ ์˜์กด์„ฑ์„ ์ œ๊ฑฐํ•จ์œผ๋กœ์จ ๋Š์Šจํ•œ ๊ฒฐํ•ฉ์œผ๋กœ ์‹œ์Šคํ…œ์ด ์œ ์—ฐํ•ด์ง„๋‹ค.
    • ์˜ต์ €๋ฒ„๋ฅผ ์–ธ์ œ๋“  ์ƒˆ๋กœ ์ถ”๊ฐ€ ๋ฐ ์ œ๊ฑฐ ๊ฐ€๋Šฅํ•˜๋‹ค.
    • ์ƒˆ๋กœ์šด ํ˜•์‹์˜ ์˜ต์ €๋ฒ„๋ผ๋„ Observable(์ฃผ์ฒด)๋ฅผ ๋ณ€๊ฒฝํ•  ํ•„์š” ์—†๋‹ค.
    • Observable(์ฃผ์ฒด)์™€ Observer๋Š” ๋…๋ฆฝ์ ์œผ๋กœ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

Observer ํŒจํ„ด ๋‹จ์ 

  1. observer๋ฅผ ํ•„์š”ํ•œ ์‹œ์  ์™ธ์— ์ œ๊ฑฐํ•˜์ง€ ์•Š์œผ๋ฉด ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ์ผ์–ด๋‚  ์ˆ˜ ์žˆ๋‹ค.
  2. ๋„ˆ๋ฌด ๋นˆ๋ฒˆํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์ƒํƒœ ๊ด€๋ฆฌ๊ฐ€ ํž˜๋“ค ์ˆ˜ ์žˆ๋‹ค.

Netflix ๊ตฌ๋… ์˜ˆ์ œ (Java)

Observer ํŒจํ„ด์„ ์ด์šฉํ•œ Netflix๊ตฌ๋… ์˜ˆ์ œ์ด๋‹ค.

NetflixUser(Observer)๋Š” Netflix(Observable)์— ์˜ํ™” ๋˜๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜์ด ์—…๋ฐ์ดํŠธ ๋˜๋Š” ๊ฒƒ์„ ๊ด€์ฐฐ ํ•˜๊ณ  ์žˆ๋‹ค. ๋งŒ์•ฝ ์˜ํ™” ๋˜๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜์ด ์—…๋ฐ์ดํŠธ ๋˜๋ฉด Netflix(Observable)์€ ์ž์‹ ์„ ๊ตฌ๋…ํ•˜๊ณ  ์žˆ๋Š” NetflixUser(Observer)์—๊ฒŒ ์•Œ๋ ค์ฃผ๊ณ  NetflixUser(Observer)๋Š” ์˜ํ™”์ด๋ƒ ์• ๋‹ˆ๋ฉ”์ด์…˜์ด๋ƒ์— ๋”ฐ๋ผ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.

Netflix Class (Observable)

public class Netflix implements Observable {

	// ๊ตฌ๋…ํ•˜๊ณ  ์žˆ๋Š” user ๋ฆฌ์ŠคํŠธ
    ArrayList<NetflixUser> userList;

    public Netflix() {
        this.userList = new ArrayList<NetflixUser>();
    }

  	// movie๋ฅผ updateํ•ด์ฃผ๊ณ  ๊ตฌ๋… ์ค‘์ธ ๊ตฌ๋…์ž์—๊ฒŒ ์•Œ๋ฆผ
    public void updateMovie(String movie){ 
      	// movie update code
        notifyMovie(movie);
    }

	  // animation์„ updateํ•ด์ฃผ๊ณ  ๊ตฌ๋… ์ค‘์ธ ๊ตฌ๋…์ž์—๊ฒŒ ์•Œ๋ฆผ
    public void updateAnimation(String animation){
      	// animation update code
        notifyAnimation(animation);
    }

  	// ๊ตฌ๋…์ž ์ถ”๊ฐ€
    @Override
    public synchronized void addObserver(NetflixUser user) {
        System.out.println(user.name + "๊ฐ€ Netflix๋ฅผ ๊ตฌ๋…ํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค.");
        userList.add(user);
    }

    // ๊ตฌ๋…์ž ์‚ญ์ œ
    @Override
    public synchronized void deleteObserver(NetflixUser user) {
        System.out.println(user.name + "๊ฐ€ Netflix๋ฅผ ๊ตฌ๋… ์ทจ์†Œํ–ˆ์Šต๋‹ˆ๋‹ค.");
        userList.remove(user);
    }
  
		// Movie๊ฐ€ update๋˜์—ˆ๋‹ค๊ณ  ๊ตฌ๋…์ž๋“ค์—๊ฒŒ ์•Œ๋ฆผ
    @Override
    public void notifyMovie(String content) {
        for (NetflixUser user : userList) {
            user.updateMovie(content);
        }
    }

  	// Animation์ด update๋˜์—ˆ๋‹ค๊ณ  ๊ตฌ๋…์ž๋“ค์—๊ฒŒ ์•Œ๋ฆผ
    @Override
    public void notifyAnimation(String content) {
        for (NetflixUser user : userList) {
            user.updateAnimation(content);
        }
    }
}

NetflixUser Class (Observer)

public class NetflixUser implements Observer {
		// ๊ตฌ๋…์ž ์ด๋ฆ„
    public String name;

    public NetflixUser(String name){
        this.name = name;
    }
		// movie update์‹œ ์•Œ๋ฆผ์„ ๋ฐ›์Œ
    @Override
    public void updateMovie(String content) {
        System.out.println(name+ " | ์˜ํ™” : " + content +"๊ฐ€ ์—…๋กœ๋“œ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
    }

  	// animation update์‹œ ์•Œ๋ฆผ์„ ๋ฐ›์Œ
    @Override
    public void updateAnimation(String content) {
        System.out.println(name+ " | ์• ๋‹ˆ๋ฉ”์ด์…˜ : " + content +"๊ฐ€ ์—…๋กœ๋“œ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
    }
}

Main Class

public class Main {

    public static void main(String[] args) {
        Netflix netflix = new Netflix();
        NetflixUser user1 = new NetflixUser("user1");
        NetflixUser user2 = new NetflixUser("user2");
        NetflixUser user3 = new NetflixUser("user3");

				// user1 ์ด ๊ตฌ๋…์„ ์‹œ์ž‘ ํ•จ
      	netflix.addObserver(user1);

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

				// netflix์— Dark Knight๊ฐ€ ์—…๋ฐ์ดํŠธ ๋จ
        netflix.updateMovie("Dark Knight");

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

      	// user2, user3 ๊ฐ€ ๊ตฌ๋…์„ ์‹œ์ž‘ํ•จ
        netflix.addObserver(user2);
        netflix.addObserver(user3);

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

      	// netflix์— Shutter Island๊ฐ€ ์—…๋ฐ์ดํŠธ ๋จ
        netflix.updateMovie("Shutter Island");

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

      	// user3๊ฐ€ ๊ตฌ๋…์„ ์ทจ์†Œํ•จ
        netflix.deleteObserver(user3);

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

      	// netflix์— ์งฑ๊ตฌ๋Š” ๋ชป๋ง๋ ค14๊ฐ€ ์—…๋ฐ์ดํŠธ ๋จ
        netflix.updateAnimation("์งฑ๊ตฌ๋Š” ๋ชป๋ง๋ ค14");
    }
}

Result