Javascript

๊ฐ์‹œ์ž(Observer) ํŒจํ„ด

frontChoi 2022. 1. 23. 19:38
๋ฐ˜์‘ํ˜•

๐Ÿค  ๊ฐ์‹œ์ž(Observer) ํŒจํ„ด์„ ๊ฐ‘์ž๊ธฐ ์ ๋Š”์ด์œ ?

Angular๋ฅผ ํ•˜๋‹ค๋ณด๋ฉด Rxjs๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์•„์ฃผ ๋งŽ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ Rxjs๋ฅผ ์•Œ์•„๋ณด๋‹ค๋ณด๋ฉด ObserverํŒจํ„ด์„ ์ด์šฉํ–ˆ๋‹ค๊ณ  ํ•˜์—ฌ ์ด๋ฒˆ์— ์ƒˆ๋กญ๊ฒŒ ๊ณต๋ถ€ํ•ด ๋ณด์•˜๋‹ค.

 

๐Ÿ’ˆ๊ฐ์‹œ์ž(Observer) ํŒจํ„ด์ด๋ž€?

"A" ๋ผ๋Š” ๊ฐ์ฒด๊ฐ€ "B"๋ผ๋Š” ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, "B"๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ์‹œํ•˜๋Š” ํŒจํ„ด์ด๋‹ค.

 

๐Ÿค ํ•„์š”ํ•œ ๊ฐ์ฒด๋“ค

  • publish ๊ฐ์ฒด : ๋ฐœํ–‰์ž์˜ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด
  • paper ๊ฐ์ฒด : ๋ฐœํ–‰์ž์˜ ๊ธฐ๋Šฅ์„ ์ƒ์†(?)๋ฐ›๋Š” ๊ฐ์ฒด
  • maxGun๊ฐ์ฒด : paper๊ฐ์ฒด์˜ ๊ตฌ๋…์ž

 

๐Ÿคฉ publish ๊ฐ์ฒด

var publisher = {
    //๊ตฌ๋…์ž๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฐ์—ด
    subscribers: {
        any: []// '์ด๋ฒคํŠธ ํƒ€์ž… : ๊ตฌ๋…์ž์˜ ๋ฐฐ์—ด'
    },
    //๊ตฌ๋…์ถ”๊ฐ€
    subscribe(fn, type) {
        type = type || 'any';

        if (typeof this.subscribers[type] === 'undefined') {
            this.subscribers[type] = [];
        }

        this.subscribers[type].push(fn);
    },
    //๊ตฌ๋… ํ•ด์ œ
    undescribe(fn, type) {
        this.visitSubscribers('undescribe', fn, type);
    },
    //๊ตฌ๋…์ž๋“ค์ด ๋“ฑ๋กํ•œ ๋ฉ”์†Œ๋“œ๋“ค์„ ํ˜ธ์ถœ
    publish(publication, type) {
        this.visitSubscribers('publish', publication, type);
    },
    visitSubscribers(action, arg, type) {
        var pubType = type || 'any',
            subscribers = this.subscribers[pubType],
            i,
            max = subscribers.length;
        
        for (i = 0; i < max; i += 1) {
            if (action === 'publish') {
                subscribers[i](arg);
            } else {
                if (subscribers[i] === arg) {
                    subscribers.splice(i, 1);
                }
            }
        }
    }
};

 

๐Ÿš“ publish ํ”„๋กœํผํ‹ฐ ๋ฐ ํ•จ์ˆ˜ ์„ค๋ช…

  • subscribers : ๊ตฌ๋…์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๋Š” publish์˜ ํ”„๋กœํผํ‹ฐ
  • undescribe : ๊ตฌ๋…์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ, ๊ตฌ๋…ํ•ด์ œ
  • publish : ๊ตฌ๋…์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ, ์ด๋ฒคํŠธ ํƒ€์ž… ๊ฐ™์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ
  • visitSubscribers : ๊ตฌ๋…์ž ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜๋Š” ํ•จ์ˆ˜

 

๐Ÿค”๋ฐœํ–‰์ž ์ƒ์„ฑ

//๋ฐœํ–‰์ž ์ƒ์„ฑ
function makePublisher(o) {
    var i;
    for (i in publisher) {
        if (publisher.hasOwnProperty(i) && typeof publisher[i] === "function") {
            o[i] = publisher[i];
        }
    }

    o.subscribers = { any: [] };
}

๋ฐœํ–‰์ž ์ƒ์„ฑ์„ ํ†ตํ•ด ๋ฐœํ–‰์ž์˜ ๋ฉ”์†Œ๋“œ๋“ค๋ฅผ ์ƒ์† ๋ฐ›๋„๋ก ํ•˜๋Š”๊ฒƒ์ด๋‹ค.

 

๐Ÿš“ Paper ๊ฐ์ฒด ์ƒ์„ฑ

//paper ๊ฐ์ฒด ์ƒ์„ฑ
var paper = {
    daily(){
        this.publish(`big news today`);
    },
    monthly(){
        this.publish(`interesting analysic`,`monthly`);
    }
}

๋ฐœํ–‰์ž ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์—ˆ์œผ๋ฉฐ, daliy๋ฉ”์†Œ๋“œ์™€ , monthly๋ฉ”์†Œ๋“œ๋ฅผ ์ƒ์„ฑํ•˜์˜€๋‹ค. ์—ฌ๊ธฐ์„œ this.pulish๋Š” "publish" ๊ฐ์ฒด์— ์กด์žฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. ์ฆ‰ ๊ตฌ๋…์ž๋“ค์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ ํ•œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

๐ŸคฉPaper ๋ฅผ ๋ฐœํ–‰์ž๋กœ ๋งŒ๋“ค๊ธฐ

//paper๋ฅผ ๋ฐœํ–‰์ž๋กœ ๋งŒ๋“ ๋‹ค.
makePublisher(paper);

 

๐Ÿšถ‍โ™‚๏ธ๊ตฌ๋…์ž ์ƒ์„ฑ ๋ฐ ๋ฐœํ–‰์ž์˜ ๊ตฌ๋…ํ•  ํ•จ์ˆ˜ ์ถ”๊ฐ€

//Maxgun ๊ตฌ๋…์ž ์ƒ์„ฑ
var maxGun = {
    drinkCoffee(paper){
        console.log(`${paper} ๋ฅผ ์ฝ์—ˆ์Šต๋‹ˆ๋‹ค.`);
    },
    sundayPreNap(monthly){
        console.log(`์ž ๋“ค๊ธฐ ์ „์— ${monthly}๋ฅผ ์ฝ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.`);
    }
}

//๋ฐœํ–‰์ž(paper)๋Š” maxGun์„ ๊ตฌ๋…ํ•œ๋‹ค
paper.subscribe(maxGun.drinkCoffee);
paper.subscribe(maxGun.sundayPreNap, 'monthly');

 

์œ„์™€ ๊ฐ™์ด ๋งŒ๋“ค ๊ฒฝ์šฐ ๊ฐ์ฒด๋“ค๊ฐ„์˜ ๊ด€๊ณ„๊ฐ€ ๋А์Šจํ•ด์กŒ๋‹ค.  ์‹ค๋ฌด์—์„œ๋Š” ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค์–ด์•ผํ• ์ง€ ๊ณ ๋ฏผ์„ ํ•ด๋ด์•ผ ๊ฒ ๋‹ค.

๋ฐ˜์‘ํ˜•