์ต์ ๋ฒ ํจํด์ ์ฃผ์ฒด๊ฐ ์ด๋ค ๊ฐ์ฒด์ ์ํ ๋ณํ๋ฅผ ๊ด์ฐฐํ๋ค๊ฐ ์ํ ๋ณํ๊ฐ ์์ ๋๋ง๋ค ๋ฉ์๋ ๋ฑ์ ํตํด์ ์ต์ ๋ฒ ๋ชฉ๋ก์ ์๋ ์ต์ ๋ฒ๋ค์๊ฒ ๋ณํ๋ฅผ ์๋ ค์ฃผ๋ ๋์์ธ ํจํด์ด๋ค.
์ฌ๊ธฐ์ '์ฃผ์ฒด'๋ ๊ฐ์ฒด์ ์ํ ๋ณํ๋ฅผ ์ง์ผ๋ณด๊ณ ์๋ ๊ด์ฐฐ์์ด๋ฉฐ, '์ต์ ๋ฒ'๋ค์ด๋ ์ด ๊ฐ์ฒด์ ์ํ ๋ณํ์ ๋ฐ๋ผ ์ ๋ฌ๋๋ ๋ฉ์๋ ๋ฑ์ ๊ธฐ๋ฐ์ผ๋ก '์ถ๊ฐ ๋ณํ ์ฌํญ'์ด ์๊ธฐ๋ ๊ฐ์ฒด๋ค์ ์๋ฏธํ๋ค.
์ต์ ๋ฒ ํจํด์ ์ฃผ์ฒด์ ๊ฐ์ฒด๋ฅผ ๋ฐ๋ก ๋์ง ์๊ณ ์ํ๊ฐ ๋ณ๊ฒฝ๋๋ ๊ฐ์ฒด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ตฌ์ถํ๊ธฐ๋ ํ๋ค.
์ด๋ฌํ ์ต์ ๋ฒ ํจํด์ ํ์ฉํ ๋ํ์ ์ธ ์๋น์ค๋ก๋ 'ํธ์ํฐ'๊ฐ ์๋ค.
ํธ์ํฐ๋ ์ฃผ์ฒด(์ด ๊ฒฝ์ฐ์ ์ํ๊ฐ ๋ณ๊ฒฝ๋๋ ๊ฐ์ฒด)๊ฐ ์๋ก์ด ํธ์์ ์ฌ๋ฆฌ๋ฉด ์ฃผ์ฒด์ ํ๋ก์๋ค์๊ฒ ์๋ก์ด ํธ์์ด ์ฌ๋ผ์๋ค๋ ์๋์ ์ ์กํ๋ค.
์ต์ ๋ฒ ํจํด์ ์ฃผ๋ก ์ด๋ฒคํธ ๊ธฐ๋ฐ ์์คํ ์ ์ฌ์ฉํ๊ณ , MVC(Model-View-Controller) ํจํด์๋ ์ฌ์ฉ๋๋ค.
์๋ฅผ ๋ค์ด, ์ฃผ์ฒด๋ผ๊ณ ๋ณผ ์ ์๋ ๋ชจ๋ธ์์ ๋ณ๊ฒฝ ์ฌํญ์ด ์๊ฒจ์ update() ๋ฉ์๋๋ก ์ต์ ๋ฒ์ธ ๋ทฐ์๊ฒ ์๋ ค์ฃผ๊ณ ์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ปจํธ๋กค๋ฌ๊ฐ ์๋ํ๋ ๊ฒ์ด๋ค.
์๋ฐ์ ์ต์ ๋ฒ ํจํด
import java.util.ArrayList;
import java.util.List;
interface Subject {
public void register(Observer obj);
public void unregister(Observer obj);
public void notifyObservers();
public Object getUpdate(Observer obj);
}
interface Observer {
public void update();
}
class Topic implements Subject {
private List<Observer> observers;
private String message;
public Topic() {
this.observers = new ArrayList<>();
this.message = "";
}
@Override
public void register(Observer obj) {
if (!observers.contains(obj)) observers.add(obj);
}
@Override
public void unregister(Observer obj) {
observers.remove(obj);
}
@Override
public void notifyObservers() {
this.observers.forEach(Observer::update);
}
@Override
public Object getUpdate(Observer obj) {
return this.message;
}
public void postMessage(String msg) {
System.out.println("Message sended to Topic: " + msg);
this.message = msg;
notifyObservers();
}
}
class TopicSubscriber implements Observer {
private String name;
private Subject topic;
public TopicSubscriber(String name, Subject topic) {
this.name = name;
this.topic = topic;
}
@Override
public void update() {
String msg = (String) topic.getUpdate(this);
System.out.println(name + ":: got message >> " + msg);
}
}
public class HelloWorld {
public static void main(String[] args) {
Topic topic = new Topic();
Observer a = new TopicSubscriber("a", topic);
Observer b = new TopicSubscriber("b", topic);
Observer c = new TopicSubscriber("c", topic);
topic.register(a);
topic.register(b);
topic.register(c);
topic.postMessage("amumu is op champion!!");
}
}
/*
Message sended to Topic: amumu is op champion!!
a:: got message >> amumu is op champion!!
b:: got message >> amumu is op champion!!
c:: got message >> amumu is op champion!!
*/
์ ์ฝ๋์์ topic์ ๊ธฐ๋ฐ์ผ๋ก ์ต์ ๋ฒ ํจํด์ ๊ตฌํํ๋ค. ์ฆ, topic์ ์ฃผ์ฒด์ด์ ๊ฐ์ฒด๊ฐ ๋๋ค. Subject ์ธํฐํ์ด์ค๋ฅผ
class topic implements Subject์์ ๊ตฌํํ๊ณ ,
Observer a = new TopicSubscriber("a", topic);
์ต์ ๋ฒ๋ฅผ ์ ์ธํ ๋ ์ต์ ๋ฒ์ ์ด๋ฆ๊ณผ ์ด๋ค ํ ํฝ์ ์ต์ ๋ฒ๊ฐ ๋ ๊ฒ์ธ์ง ์ค์ ํ๋ค.
์ด ์ฝ๋์ main ํจ์์ ํ๋ฆ์ ๋ฐ๋ผ๊ฐ๋ณด๋ฉด
- topic์ด๋ผ๋ ์ฃผ์ฒด์ด์ ๊ฐ์ฒด๊ฐ ๋๋ Topic ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
- ์์ ์์ฑํ topic์ ์ต์ ๋ฒ๊ฐ ๋๋ a, b, c ์ต์ ๋ฒ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
- topic.register() ํจ์๋ฅผ ํตํด์ topic๊ฐ์ฒด์ ํด๋น ์ต์ ๋ฒ๊ฐ ํฌํจ๋์ด ์์ง ์๋ค๋ฉด, topic ๊ฐ์ฒด์ observer ArrayList์ ์ถ๊ฐํด์ ํด๋น topic ๊ฐ์ฒด์ ์ต์ ๋ฒ๋ก ๋ฑ๋กํ๋ค.
- ์ต์ ๋ฒ a,b,c์ ์ฃผ์ฒด์ด์ ๊ฐ์ฒด์ธ topic์ด postMessage('')๋ฅผ ํตํด์ ์ด๋ค ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๋ ๋ณ๊ฒฝ ์ฌํญ์ด ์๊ธฐ๋ฉด, postMessage ํจ์์์ ํด๋น ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๊ณ topic์ message ์์ฑ์ ์ ์ฅํ ๋ค์, notifyObserver() ๋ฉ์๋๋ฅผ ํตํด์ ๋ฑ๋ก๋ ์ต์ ๋ฒ๋ค์ update() ๋ฉ์๋๋ฅผ ์คํํ๊ฒ ํ๋ค.
์ถ๊ฐ์ ์ผ๋ก, ์๋ฐ์คํฌ๋ฆฝํธ์์ ํ๋ก์ ๊ฐ์ฒด๋ฅผ ์ด์ฉํด์ ์ต์ ๋ฒ ํจํด์ ๊ตฌํํ ์๋ ์๋๋ฐ ํ๋ก์ ๊ฐ์ฒด์ ๋ํด์ ๋ค๋ฅธ ํฌ์คํธ๋ก ์์ธํ๊ฒ ๋ค๋ฃฐ ์์ ์ด๋ค!