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)λΌκ³ λ ν¨.
- κ΄μ°°λμ ν΄λμ€: μμ μ κ΄μ°°νλ κ΄μ°°μλ€μ μ μ§νκ³ λ° κ΄λ¦¬ν μ μκ³ , μμ°μ μνκ° λ³ννλ©΄ λ±λ‘λ κ΄μ°°μμκ² μλ¦Ό
- ν¨ν΄μ ꡬ쑰
- Weather Monitoring App
- νμ¬ λ μ¨, λ μ¨ν΅κ³μ 보, μΌκΈ°μ보 μΈ μ’ λ₯μ μΆλ ₯μ₯μΉλ₯Ό μ§μν΄μΌ νλ©°, νμ₯ κ°λ₯νλλ‘ APIλ₯Ό μ 곡ν΄μΌ ν¨
- κ΄μ°°μ ν¨ν΄ μμ
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("");
});
'κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ° (OOP) > Design Pattern' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Design Pattern] Command Pattern (0) | 2021.09.24 |
---|---|
[Design Pattern] Abstract Factory Pattern (0) | 2021.09.04 |
[Design Pattern] Factory Method Pattern (0) | 2021.09.04 |
[Design Pattern] Strategy Pattern (0) | 2021.09.04 |