λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

객체 μ§€ν–₯ ν”„λ‘œκ·Έλž˜λ° (OOP)/Design Pattern

[Design Pattern] Observer Pattern

728x90

Observer Pattern

  • νŒ¨ν„΄μ˜ λΆ„λ₯˜ : ν–‰μœ„νŒ¨ν„΄
  • νŒ¨ν„΄μ˜ λͺ©μ  : This pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. – GoF –
  • νŒ¨ν„΄μ˜ 적용
    • RSS Feed: μ›Ή μ‚¬μ΄νŠΈλ“€μ€ μƒˆλ‘œμš΄ 정보가 λ°œμƒν•˜λ©΄ 관심을 κ°€μ§€κ³  μžˆλŠ” μΈ‘μ—κ²Œ μžλ™ ν†΅λ³΄ν•˜μ—¬ 쀌
    • Facebookκ³Ό 같은 λ§Žμ€ SNS의 경우 μ‚¬μš©μžκ°€ 글을 κ²Œμ‹œν•˜λ©΄ κ·Έ κ²Œμ‹œμžμ˜ λͺ¨λ“  followerμ—κ²Œ μžλ™ ν†΅λ³΄ν•˜μ—¬ 쀌
    • GUI ν”„λ‘œκ·Έλž˜λ°μ—μ„œ 사건 μ†ŒμŠ€μ— 사건 경청자λ₯Ό λ“±λ‘ν•˜μ—¬ 사건이 λ°œμƒν•˜μ˜€μ„ λ•Œ μ²˜λ¦¬ν•œλ‹€. 보톡 ν•˜λ‚˜μ˜ 사건 경청자만 λ“±λ‘ν•˜μ§€λ§Œ μ›ν•˜λ©΄ μ—¬λŸ¬ 개λ₯Ό 등둝할 μˆ˜λ„ μžˆλ‹€. 이 μΈ‘λ©΄μ—μ„œ 사건 μ†ŒμŠ€λŠ” κ΄€μ°° λŒ€μƒ, 사건 κ²½μ²­μžλŠ” κ΄€μ°°μžκ°€ 되며, 보톡 λ‚΄λΆ€μ μœΌλ‘œ κ΄€μ°°μž νŒ¨ν„΄μœΌλ‘œ κ΅¬ν˜„
  • νŒ¨ν„΄μ˜ μ°Έμ—¬μž
    • κ΄€μ°°μž interface: κ΄€μ°°λŒ€μƒμ΄ μƒνƒœ λ³€ν™”λ₯Ό μ•Œλ €μ£ΌκΈ° μœ„ν•΄ ν˜ΈμΆœν•˜λŠ” λ©”μ†Œλ“œκ°€ μ„ μ–Έλ˜μ–΄μ•Ό μžˆμ–΄μ•Ό 함. μ†ŒλΉ„μž(consumer) λ˜λŠ” κ΅¬λ…μž(publisher)라고도 함
    • κ΄€μ°°μžν΄λž˜μŠ€ : κ΄€μ°° λŒ€μƒμ— 관심을 κ°–λŠ” 객체이며, κ΄€μ°° λŒ€μƒμ€ μžμ‹ μ˜ μƒνƒœκ°€ λ³€ν–ˆμ„ λ•Œ 이 κ°μ²΄μ—κ²Œ μžλ™ 톡보함
    • κ΄€μ°°λŒ€μƒ interface: κ΄€μ°°λŒ€μƒμ΄ 되기 μœ„ν•΄ ν•„μš”ν•œ λ©”μ†Œλ“œλ“€μ΄ μ„ μ–Έλ˜μ–΄ 있음 μƒμ‚°μž(producer) λ˜λŠ” 좜판자(publisher)라고도 함.
    • κ΄€μ°°λŒ€μƒ 클래슀: μžμ‹ μ„ κ΄€μ°°ν•˜λŠ” κ΄€μ°°μžλ“€μ„ μœ μ§€ν•˜κ³  및 관리할 수 있고, μžμ‚°μ˜ μƒνƒœκ°€ λ³€ν™”ν•˜λ©΄ λ“±λ‘λœ κ΄€μ°°μžμ—κ²Œ μ•Œλ¦Ό
  • νŒ¨ν„΄μ˜ ꡬ쑰

    μŠ€ν¬λ¦°μƒ· 2021-07-25 μ˜€ν›„ 11 52 01
  • Weather Monitoring AppμŠ€ν¬λ¦°μƒ· 2021-08-01 μ˜€ν›„ 12 46 04
  • ν˜„μž¬ 날씨, 날씨톡계정보, 일기예보 μ„Έ μ’…λ₯˜μ˜ 좜λ ₯μž₯치λ₯Ό 지원해야 ν•˜λ©°, ν™•μž₯ κ°€λŠ₯ν•˜λ„λ‘ APIλ₯Ό μ œκ³΅ν•΄μ•Ό 함
  • κ΄€μ°°μž νŒ¨ν„΄ μ˜ˆμ‹œ
    μŠ€ν¬λ¦°μƒ· 2021-07-24 μ˜€ν›„ 7 12 28
class A implements EventHandler<ActionEvent>{
        @Override
        public void handle(ActionEvent event) {
            textData += "Apple\n";
            textOutArea.setText(textData);        
        }
    }
class B implements EventHandler<ActionEvent>{
        @Override
        public void handle(ActionEvent event) {
            textData += "Banana\n";
            textOutArea.setText(textData);            
        }
    }
class C implements EventHandler<ActionEvent>{
        @Override
        public void handle(ActionEvent event) {
            textData += "Cherry\n";
            textOutArea.setText(textData);            
        }
Button normalButton = new Button("단일 μ‚¬κ±΄μ²˜λ¦¬μž");
        Button multiActionButton = new Button("닀쀑 μ‚¬κ±΄μ²˜λ¦¬μž");
        Button clearButton = new Button("μ „μ²΄μ§€μš°κΈ°");
        buttonPane.getChildren().addAll(normalButton, multiActionButton, clearButton);

        normalButton.setOnAction(new A());

        multiActionButton.addEventHandler(ActionEvent.ACTION, new B());
        multiActionButton.addEventHandler(ActionEvent.ACTION, new C());
        multiActionButton.addEventHandler(ActionEvent.ACTION, e->{
            textData += "Kiwi\n";
            textOutArea.setText(textData);    
        });

        clearButton.setOnAction(e->{
            textData = "";
            textOutArea.setText("");
        });