π [λμμΈν¨ν΄] π» Observer Pattern (μ΅μ λ² ν¨ν΄/κ΄μ°°μ ν¨ν΄)
π» Observer Pattern (μ΅μ λ² ν¨ν΄/κ΄μ°°μ ν¨ν΄)
μλ
νμΈμ. π
μ€λμ Observer Pattern (κ΄μ°°μ ν¨ν΄ ) μ λν΄
μμλ³΄λ €κ³ ν©λλ€.
What is Observer Pattern? (μ΅μ λ² ν¨ν΄μ΄λ?)
-
Observer Pattern μ΄λ
μ΅μλ² ν¨ν΄(observer pattern)μ κ°μ²΄μ μν λ³νλ₯Ό κ΄μ°°νλ κ΄μ°°μλ€, μ¦ μ΅μ λ²λ€μ λͺ©λ‘μ κ°μ²΄μ λ±λ‘νμ¬ μν λ³νκ° μμ λλ§λ€ λ©μλ λ±μ ν΅ν΄ κ°μ²΄κ° μ§μ λͺ©λ‘μ κ° μ΅μ λ²μκ² ν΅μ§νλλ‘ νλ λμμΈ ν¨ν΄μ΄λ€. μ£Όλ‘ λΆμ° μ΄λ²€νΈ νΈλ€λ§ μμ€ν μ ꡬννλ λ° μ¬μ©λλ€. λ°ν/ꡬλ λͺ¨λΈλ‘ μλ €μ Έ μκΈ°λ νλ€. μΆμ²(μν€λ°±κ³Ό) https://ko.wikipedia.org/wiki/%EC%98%B5%EC%84%9C%EB%B2%84_%ED%8C%A8%ED%84%B4
Observer Patternμ νμ©
μ΅μ λ² ν¨ν΄μ μ μμ²λΌ λ³΄ν΅ λ°ν/ꡬλ λͺ¨λΈλ‘ μλ €μ Έμμ΅λλ€. λν κ΄μ°°μν¨ν΄μ΄λΌκ³ λ ν©λλ€. νλμ Publisher(λ°νμ)μκ² μ¬λ¬ Subscriber (ꡬλ μ)κ° μμ μ μκΈ° λλ¬Έμ μΌλμΌμ΄λ μΌλλ€ κ΄κ³λ₯Ό μ΄λ£°μ μμ΅λλ€. κ·Έλ¦¬κ³ Publisher(λ°νμ)μ μνκ° μ λ°μ΄νΈλλ€λ©΄ μ΄ Publisher(λ°νμ)λ₯Ό ꡬλ (κ΄μ°°)νκ³ μλ μ΅μ λ²λ€μκ² μ λ°μ΄νΈκ° λμμμ μλ €μ€λλ€.
μ΅μ λ² ν¨ν΄μ νΈμ λ°©μκ³Ό ν λ°©μμ΄ μμ΅λλ€. λν Publisherμ μ 보λ μνκ° λ³κ²½λ λλ§λ€ Observerμκ² μ νν΄μ£Όλ κ²μ΄ Pushλ°©μμ΄κ³ Observerμμ μ λ³΄κ° νμν λλ§λ€ Publisherμκ² κ°μ Έμ€λ λ°©μμ Pullλ°©μμ΄λΌκ³ ν©λλ€.
μ΄ μ΅μ λ² ν¨ν΄μ λνμ μΈ μ¬μ©μ²λ μλ°μ Swingμ΄λ μλλ‘μ΄λμ Viewλ Button λ±μ μμ ―μμ κ°μ’ μ΄λ²€νΈλ₯Ό λ°μ λ μ°μ λλ€.
Observer Pattern μ μ© μμ
μ΅μ λ² ν¨ν΄μ μ’ λ μ΄ν΄νκΈ° μ½κ² νκΈ°μν΄ λ΄μ€ ꡬλ μ΄λ μ νλΈ κ΅¬λ λ±μ μμλ‘ λ€μμμ΅λλ€. μ νλΈ κ΅¬λ μ μμλ‘ λ³΄μ¬λλ¦¬κ² μ΅λλ€.
λ³΄ν΅ νν λ§μ΄λ€ νλ κ²μ²λΌ μ΄λ€ μ νλΈ μ±λμ ꡬλ νκ³ μλ¦Όμ μ€μ ν΄λλ©΄ ꡬλ ν μ±λ(Publihser)μ μμμ΄ μλ‘ μ¬λΌμ¬ λ (Publisher μ 보 μ λ°μ΄νΈ) ꡬλ ν μ¬λλ€μκ²(Subscriber / Observer) μλ €μ€λλ€. (push λ°©μ)
λν ꡬλ ν΄μ λ₯Ό ν΅ν΄ ꡬλ μ κ·Έλ§λ μλ μμ΅λλ€. (unsubscribe)
Publisher μΈν°νμ΄μ€
package observer;
public interface Publisher {
public void add(Observer observer);
public void delete(Observer observer);
public void notifyObserver();
}
Observer μΈν°νμ΄μ€
package observer;
public interface Observer {
public void update(String title, String content);
}
YoutubeChannel ν΄λμ€ (Publisher)
package observer;
import java.util.ArrayList;
import java.util.List;
public class YoutubeChannel implements Publisher{
private List<Observer> observerList;
private String title;
private String content;
public YoutubeChannel() {
this.observerList = new ArrayList<>();
}
@Override
public void add(Observer observer) {
observerList.add(observer);
}
@Override
public void delete(Observer observer) {
int targetIndex = observerList.indexOf(observer);
observerList.remove(targetIndex);
}
@Override
public void notifyObserver() {
for(Observer observer : observerList) {
observer.update(title, content);
}
}
public List<Observer> getObserverList() {
return observerList;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public void setNewsInfo(String title, String content) {
this.title = title;
this.content = content;
notifyObserver();
}
}
AnnualSubscriber ν΄λμ€ (Subscriber / Observer)
package observer;
public class AnnualSubscriber implements Observer {
private String newsString;
private Publisher publisher;
public AnnualSubscriber(Publisher publisher) {
this.publisher = publisher;
publisher.add(this);
}
@Override
public void update(String title, String content) {
this.newsString = "title : " + title + "\n" + "content : " + content + "\n========";
display();
}
public void unsubscribe() {
this.publisher.delete(this);
}
private void display() {
System.out.println("AnnualSubscriber \n");
System.out.println("Today News\n" + newsString);
}
}
EventSubscriber ν΄λμ€ (Subscriber / Observer)
package observer;
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 content) {
this.newsString = "title : " + title + "\n" + "content : " + content + "\n========";
display();
}
public void unsubscribe() {
this.publisher.delete(this);
}
private void display() {
System.out.println("EventSubscriber \n");
System.out.println("Today News\n" + newsString);
}
}
Main문
package observer;
public class Main {
public static void main(String[] args) {
YoutubeChannel youtubeChannel = new YoutubeChannel();
AnnualSubscriber annualSubscriber = new AnnualSubscriber(youtubeChannel);
EventSubscriber eventSubscriber = new EventSubscriber(youtubeChannel);
youtubeChannel.setNewsInfo("첫 μμμ
λλ€.", "μλ
νμΈμ!");
youtubeChannel.setNewsInfo("λλ²μ§Έ μμμ
λλ€.", "λ λ΄μ£Όμ
μ κ°μ¬ν©λλ€!");
eventSubscriber.unsubscribe();
System.out.println("eventSubscriber νν΄ ν\n\n");
youtubeChannel.setNewsInfo("ꡬλ
μκ° 100λ§λͺ
μ΄ λμμ΅λλ€", "κ°μ¬ν©λλ€!");
}
}
μ€νκ²°κ³Ό
λ
μ€λμ μ΅μ λ²(κ΄μ°°μ) ν¨ν΄ (observer pattern)μ λν΄ μμ보μμ΅λλ€.
κ°μ¬ν©λλ€. π
Reference
https://ko.wikipedia.org/wiki/%EC%98%B5%EC%84%9C%EB%B2%84_%ED%8C%A8%ED%84%B4
https://flowarc.tistory.com/entry/%EB%94%94%EC%9E%90%EC%9D%B8-%ED%8C%A8%ED%84%B4-%EC%98%B5%EC%A0%80%EB%B2%84-%ED%8C%A8%ED%84%B4Observer-Pattern