๐ก About Observer Pattern
์ต์ ๋ฒ ํจํด์ ๊ฐ๋จํ ์๊ธฐํ์๋ฉด ์ด๋ค ๊ฐ์ฒด์ ์ํ๊ฐ ๋ณํ ๋ ๊ทธ์ ์ฐ๊ด๋ ๊ฐ์ฒด๋ค์๊ฒ ์๋ฆผ์ ๋ณด๋ด๋ ๋์์ธ ํจํด์ด๋ค.
- pull → ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์จ๋ค
- push → ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅํ๋ค
๊ธฐ๋ณธ์ ์ผ๋ก ๋ฐ์ดํฐ์ ๋ณ๊ฒฝ๊ฐ์ ๋ฐ์ํ๊ฑฐ๋ ์ถ๊ฐํ ๋, pull๋ณด๋ค push๋ฅผ ํตํด ์ฒ๋ฆฌํด์ฃผ๋๊ฒ ๋ ๋น ๋ฅด๋ค๋ ์์ด๋์ด์์ ์์๋ ํจํด !
- One-To-Many Relationship
→ ํ๋์ ์ ๋ฌธ์ฌ์์ ๊ตฌ๋ ์๋ฅผ ๊ณ์ ์์ ํ๊ณ , ์ถ๊ฐํด์ค๋ค๊ณ ์๊ฐํ๋ฉด ์ฝ๋ค.
๐ก Observer Pattern Structure
Subject
→ ์ ๋ฌธ์ฌ ์ญํ , ConcreteSubject์ ์์ ํด๋์ค
- subject ๊ฐ์ฒด์์ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๋ค.
- ๋ฐ์ดํฐ๊ฐ ๋ฌ๋ผ์ง๋ฉด observer์๊ฒ notify๋ฅผ ๋ณด๋ธ๋ค.
Observer
→ ๊ตฌ๋
์ ์ญํ , ConcreteObserver์ ์์ ํด๋์ค
- update() ๋ฉ์๋๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ pushํ๋ค.
ConcreteSubject
→ Subject์ ํ์ ํด๋์ค
ConcreteObserver
→ Observer์ ์์ ํด๋์ค
- ์์ ๋๋ฅผ ๋์ด๊ธฐ ์ํ์ฌ ์์/ํ์๊ตฌ์กฐ๋ก ํด๋์ค๋ฅผ ๋
๋ฆฝ์ํจ๋ค
↔ Dependency Inversion Principle - ConcreteObserver ๊ฐ์ฒด๋ ์ฌ๋ฌ๊ฐ๊ฐ ๋ง๋ค์ด์ง ์ ์์ง๋ง, Observer๋ interface์ด๋ฏ๋ก ์ฌ๋ฌ๊ฐ์ ๊ฐ์ฒด๊ฐ ๋ง๋ค์ด์ง ์ ์๋ ๊ตฌ์กฐ์ด๋ค.
- ์ผ๋ฐ์ ์ผ๋ก ์ต์ ๋ฒ ํจํด์ push๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ๊ตฌ์กฐ์ด์ง๋ง, ์์ ๋๋ฅผ ๋์ด๊ธฐ ์ํด ํน์ ์ํฉ์์๋ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋๋ก pullํ ์๋ ์๋ค.
๐ก Java's Observer
์๋ฐ์์๋ ์์ฒด์ ์ผ๋ก ์ต์ ๋ฒ ํจํด์ ์ง์ํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์๋ค. Observable ์ธํฐํ์ด์ค์ Observableํด๋์ค๋ฅผ ์ ๊ณตํ๋ฏ๋ก ์ฐ๋ฆฌ๋ ์ ๊ทธ๋ฆผ์ Concrete๋ถ๋ถ๋ง ๊ตฌํํ๋ฉด ์ต์ ๋ฒ ํจํด์ ์ฝ๊ฒ ๊ตฌํํ ์ ์๋ค!
- Observer ์ธํฐํ์ด์ค → ์ ๊ทธ๋ฆผ์ ConcreteSubject์ ๊ฐ์ ์ญํ
Observser ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ์ฌ ์ ๋ฌธ์ฌ ํด๋์ค๋ฅผ ๋ง๋ค ์ ์๋ค. - Observable ํด๋์ค → ์ ๊ทธ๋ฆผ์ Observerํด๋์ค์ ๊ฐ์ ์ญํ
Observable ๊ฐ์ฒด์ addObserver() ๋ฉ์๋ ํธ์ถ์ ํตํด ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ , One-To-Many Relationship์ ๊ตฌํํ ์ ์๋ค.
๐ก Practice Observer Pattern
๋ค์ ์ฝ๋์ Observer Pattern์ ์ ์ฉํด๋ณด์. (์ผ๋ถ ๋ฉ์๋ ๊ตฌํ์ ์๋ตํ ์ฝ๋)
import java.util.ArrayList;
public class WeatherData {
private float temperature;
private float humidity;
private float pressure;
public WeatherData() { }
public void setMeasurements(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
}
public float getTemperature() {
return temperature;
}
public float getHumidity() {
return humidity;
}
public float getPressure() {
return pressure;
}
}
class WeatherStation {
public static void main(String[] args) {
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentDisplay =
new CurrentConditionsDisplay();
StatisticsDisplay statisticsDisplay = new StatisticsDisplay();
ForecastDisplay forecastDisplay = new ForecastDisplay();
weatherData.setMeasurements(80, 65, 30.4f);
currentDisplay.update(weatherData.getTemperature(), weatherData.getHumidity(), weatherData.getPressure());
statisticsDisplay.update(weatherData.getTemperature(), weatherData.getHumidity(), weatherData.getPressure());
forecastDisplay.update(weatherData.getTemperature(), weatherData.getHumidity(), weatherData.getPressure());
weatherData.setMeasurements(82, 70, 29.2f);
currentDisplay.update(weatherData.getTemperature(), weatherData.getHumidity(), weatherData.getPressure());
statisticsDisplay.update(weatherData.getTemperature(), weatherData.getHumidity(), weatherData.getPressure());
forecastDisplay.update(weatherData.getTemperature(), weatherData.getHumidity(), weatherData.getPressure());
weatherData.setMeasurements(78, 90, 29.2f);
currentDisplay.update(weatherData.getTemperature(), weatherData.getHumidity(), weatherData.getPressure());
statisticsDisplay.update(weatherData.getTemperature(), weatherData.getHumidity(), weatherData.getPressure());
forecastDisplay.update(weatherData.getTemperature(), weatherData.getHumidity(), weatherData.getPressure());
}
}
class StatisticsDisplay {
private float maxTemp = 0.0f;
private float minTemp = 200;
private float tempSum= 0.0f;
private int numReadings;
public StatisticsDisplay() {
}
public void update(float temp, float humidity, float pressure) {
display();
}
public void display() {
}
}
class CurrentConditionsDisplay {
private float temperature;
private float humidity;
public CurrentConditionsDisplay() {
}
public void update(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
display();
}
public void display() {
}
}
class ForecastDisplay {
private float currentPressure = 29.92f;
private float lastPressure;
public ForecastDisplay() {
}
public void update(float temp, float humidity, float pressure) {
lastPressure = currentPressure;
currentPressure = pressure;
display();
}
public void display() {
}
}
- DisplayElements, Observer ์ธํฐํ์ด์ค๋ฅผ ์ถ๊ฐํ์ฌ 3๊ฐ์ Display ํด๋์ค(StatisticsDisplay, CurrentConditionDisplay, ForecastDisplay)์์ ์์๋ฐ์ ๊ตฌํํ๋๋ก ํ๋ค.
interface Observer {
public void update(float temp, float humidity, float pressure);
}
interface DisplayElements{
public void display();
}
class StatisticsDisplay implements Observer, DisplayElements{
private float maxTemp = 0.0f;
private float minTemp = 200;
private float tempSum= 0.0f;
private int numReadings;
public StatisticsDisplay() {
}
public void update(float temp, float humidity, float pressure) {
}
public void display() {
}
}
class CurrentConditionsDisplay implements Observer, DisplayElements{}
class ForecastDisplay implements Observer, DisplayElements{}
- Observer Pattern Structure ๊ทธ๋ฆผ์ Subject ์ธํฐํ์ด์ค๋ฅผ ์์ฑํด์ฃผ๊ณ , WeatherData ํด๋์ค๊ฐ ์ด๋ฅผ ์์๋ฐ์ ๋ฑ๋ก, ์ญ์ , ์
๋ฐ์ดํธ ๋ฉ์๋๋ฅผ ๊ตฌํํ๋ค.
Subject๋ ์ธํฐํ์ด์ค๋ก ๊ตฌํํ๋ ๋ฐฉ๋ฒ ์ธ์๋ ์ถ์ํด๋์ค๋ก ์์ฑํ๋ ๋ฐฉ๋ฒ ๊ทธ๋ฆฌ๊ณ ์๋ฐ์์ ์ ๊ณตํ๋ Observable ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด ์๋ค.
interface Subject {
public void registerObserver(Observer o); // ๊ตฌ๋
public void removeObserver(Observer o); // ๊ตฌ๋
ํด์ง
public void notifyObservers();
}
public class WeatherData implements Subject{
private float temperature;
private float humidity;
private float pressure;
// Observer ๊ฐ์ฒด๋ค์ ๊ด๋ฆฌํด์ค ๋ฆฌ์คํธ ์์ฑ
private ArrayList<Observer> observers = new ArrayList<Observer>();
// ์์ฑ์
public WeatherData(){
observers = new ArrayList<Observer>();
}
public void setMeasurements(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
// ๋ณํ๊ฐ ์๊ธฐ๋ฉด notifyObservers() ํธ์ถ!
notifyObservers();
}
// ์๋ต
// ๋ฆฌ์คํธ๋ฅผ ๊ด๋ฆฌํด์ผํจ -> ๊ตฌ๋
์ ๋ฑ๋ก, ์ญ์ , ์
๋ฐ์ดํธ ๋ฉ์๋ ๊ตฌํ
@Override
public void registerObserver(Observer o) {
// Observer๊ฐ์ฒด ArrayList์ ๋ฑ๋ก
}
@Override
public void removeObserver(Observer o) {
// ๋ฆฌ์คํธ์์ ์ ๊ฑฐํ๊ธฐ -> ์ธ๋ฑ์ค ์ฐพ์์ ์ง์ฐ๊ธฐ
}
@Override
public void notifyObservers() {
// loop๋ฅผ ๋๋ฉด์ ๋ณ๊ฒฝ๋ ๊ฐ์ฒด๋ง๋ค ํ๋ฒ์ฉ update ๋ฉ์๋๋ฅผ ํธ์ถํด์ค
}
}
๊ฒฐ๊ณผ์ ์ผ๋ก, ๊ฐ์ฒด๊ฐ ๋ฑ๋ก๋๊ฑฐ๋ ์ญ์ ๋ ๋, ๋ณ๊ฒฝ๋ ๋ ๊ฐ๊ฐ registerObserver, removeObserver, notifyObserver ๋ฉ์๋๋ง ํธ์ถํ๋ฉด ๋๋ฏ๋ก WeatherStation ํด๋์ค์ ์ฝ๋๊ฐ ๋ค์๊ณผ ๊ฐ์ด ๊ฐ๋จํด์ง๋ค.
class WeatherStation {
public static void main(String[] args) {
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentDisplay =
new CurrentConditionsDisplay(weatherData);
StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);
weatherData.setMeasurements(80, 65, 30.4f);
weatherData.setMeasurements(82, 70, 29.2f);
weatherData.setMeasurements(78, 90, 29.2f);
}
}
push๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅ, ์์ ํ๊ธฐ ๊ฐํธํ๋ฉด์๋ ๊ธฐ์กด์ get ๋ฉ์๋๋ค๋ ์ ์งํ์ฌ ํ์์ ์ํด pull์ ํตํด ๋ฐ์ดํฐ๋ค์ ๋ถ๋ฌ์ฌ ์๋ ์๋ ๊ตฌ์กฐ๊ฐ ๋์๋ค๐ค๐ค
'Software > Design Pattern' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Design Pattern] Command Pattern (4) | 2021.11.28 |
---|---|
[Design Pattern] Adapter Pattern (0) | 2021.11.23 |