๐ป Programming/GoF Design Pattern
[๋์์ธํจํด] ๊ด์ฐฐ์(์ต์ ๋ฒ) ํจํด ( Observer Pattern )
Creational Patterns ( ์์ฑ ํจํด )
These design patterns provides way to create objects while hiding the creation logic, rather than instantiating objects directly using new operator. This gives program more flexibility in deciding which objects need to be created for a given use case.
์์ฑํจํด์ ๊ฐ์ฒด์ ์์ฑ๋ก์ง์ ์จ๊ธฐ๊ณ new ๋ช ๋ น์ด๋ฅผ ํตํ์ง ์๊ณ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๋ฐฉ๋ฒ๋ค์ ์ ๊ณตํ๋ค. ์ด๋ ํน์ ์ํฉ์์ ์ด๋ค ๋ฐฉ๋ฒ์ผ๋ก ๊ฐ์ฒด๋ฅผ ์์ฑํด์ผํ ์ง๋ฅผ ๊ฒฐ์ ํ๋๋ฐ ์์ด์ ์ ์ฐ์ฑ์ ์ ๊ณตํ๋ค.
Structural Patterns ( ๊ตฌ์กฐ์ ํจํด )
These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.
๊ตฌ์กฐ์ ํจํด๋ค์ ํด๋์ค์ ๊ฐ์ฒด์ ๊ตฌ์ฑ์ ๊ด์ฌํ๋ค.
Behavioral Patterns ( ํ์์ ํจํด )
These design patterns are specifically concerned with communication between objects.
์ด ํจํด๋ค์ ๊ฐ์ฒด๋ค ์ฌ์ด์ ์ปค๋ฎค๋์ผ์ด์ ์ ๊ด์ฌ์ ๊ฐ์ง๋ค.
์ค๋ ์ดํด๋ณผ Observer ํจํด์ Behavioral ํจํด์ ์ํฉ๋๋ค.
์ต์ ๋ฒ๋ผ๊ณ ํ๋๊น 10๋ ์ ์ ์คํํฌ๋ํํธ์์ ๋์๋ ์ต์ ๋ฒ๊ฐ ์๊ฐ์ด ๋๋ค์. cloaking์ํ๋ก ์ฌ๊ธฐ์ ๊ธฐ ๋งต์ ๋ค์ง๊ณ ๋ค๋ ๋ ๋ ์์ด์ฃ . HP๋ ์ด์ฐ๋ ์ ์์ง ํ๋ฐฉ์ ๊ทธ๋ฅ ํ ํฐ์ ธ์ ์ฃฝ์ด๋ฒ๋ฆฌ๋ ๋ ์์ด์์ฃ ใ ใ .
์ด ์ต์ ๋ฒ ํจํด์ ํ ๊ฐ์ฒด์ ์ํ๋ฅผ ์ง์ผ๋ณด๊ณ ์๋ค๊ฐ ์ด ๊ฐ์ฒด์ ์ํ๊ฐ ๋ณ๊ฒฝ์ด ๋๋ฉด ์ด ๊ฐ์ฒด์ ์ํ์ ์์กด์ ์ธ ๋ค๋ฅธ ๊ฐ์ฒด๋ค์ ์ํ๋ฅผ ์๋์ผ๋ก ์ ๋ฐ์ดํธํ ์ ์๊ฒ๋ ํด์ฃผ๋ ํจํด์ด๋ผ๊ณ ํฉ๋๋ค.
์๋์์ ์ข ๋ ์์ธํ ์์๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
Observer Pattern Structure
ํจํด์ ๋ชฉ์ ( Intent ) :
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
์ต ์ ๋ฒ ํจํด์ ๋ชฉ์ ์ ํ๋์ ๊ฐ์ฒด์ ์ฌ๋ฌ๊ฐ์ ๊ฐ์ฒด๊ฐ ์์กด์ ์ธ ๊ฒฝ์ฐ์, ๊ทธ ํ ๊ฐ์ฒด์ ์ํ๊ฐ ๋ณ๊ฒฝ์ด ๋๋ฉด ์ด ๊ฐ์ฒด์ ์์กด์ ์ธ ๋ค๋ฅธ ์ฌ๋ฌ๊ฐ์ ๊ฐ์ฒด๋ค์ด notification์ ๋ฐ๊ณ ์๋์ผ๋ก ์์ ๋ค์ ์ํ๋ฅผ ์ ๋ฐ์ดํธ๋ฅผ ํ ์ ์๋๋ก ํ๊ธฐ ์ํจ์ ๋๋ค.
ํจํด์ด ๋์ค๊ฒ ๋ ๋๊ธฐ ( Motivation ) :
Both a spreadsheet object and bar chart object can depict information in the same application data object using different presentations. The spreadsheet and the bar chart don't know about each other, thereby letting you reuse only the one you need. But they behave as though they do. When the user changes the information in the spreadsheet, the bar chart reflects the changes immediately, and vice versa.
This behavior implies that the spreadsheet and bar chart are dependent on the data object and therefore should be notified of any change in its state. And there's no reason to limit the number of dependent objects to two; there may be any number of different user interfaces to the same data.
The Observer pattern describes how to establish these relationships. The key objects in this pattern are subject and observer. A subject may have any number of dependent observers. All observers are notified whenever the subject undergoes a change in state. In response, each observer will query the subject to synchronize its state with the subject's state.
This kind of interaction is also known as publish-subscribe. The subject is the publisher of notifications. It sends out these notifications without having to know who its observers are. Any number of observers can subscribe to receive notifications.
์ ์ฉ์ฑ ( Applicability ) :
Use the Observer pattern in any of the following situations:
- When an abstraction has two aspects, one dependent on the
other. Encapsulating these aspects in separate objects lets you vary
and reuse them independently.
- When a change to one object requires changing others, and you don't know how many objects need to be changed.
- When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you don't want these objects tightly coupled.
๋ฑ์ฅ ์ธ๋ฌผ ( Participants ) :
Subject
o knows its observers. Any number of Observer objects may observe a subject.
o provides an interface for attaching and detaching Observer objects.
Observer
o defines an updating interface for objects that should be notified of changes in a subject.
ConcreteSubject ( ์ดํ CS )
o stores state of interest to ConcreteObserver objects.
o sends a notification to its observers when its state changes.
ConcreteObserver ( ์ดํ CO )
o maintains a reference to a ConcreteSubject object.
o stores state that should stay consistent with the subject's.
o implements the Observer updating interface to keep its state consistent with the subject's.
์๋ฆฌ ( Collaborations ) :
- ConcreteSubject notifies its observers whenever a change occurs that could
make its observers' state inconsistent with its own.
ConcreteSubject( ์ดํ CS )๊ฐ์ฒด๊ฐ ์ด ๊ฐ์ฒด๋ฅผ ์ง์ผ๋ณด๋ ์ต์ ๋ฒ๋ค์๊ฒ ์์ ์ ์ํ๊ฐ ๋ณ๊ฒฝ๋์๋ค๋ ๊ฒ์ ์๋ ค์ค๋๋ค. ๊ทธ๋ฌ๋ฉด ์ต์ ๋ฒ๋ค์ ์ด ์ํ๋ฅผ ์ ์ฅํฉ๋๋ค. CS๊ฐ์ฒด๊ฐ ์์ ์ ์ํ๋ณํ๋ฅผ ์๋ ค์ค ๋๋ง๋ค ์ต์ ๋ฒ๋ค ๋ํ ์ํ์ ๋ณด๋ฅผ ์ ๋ฐ์ดํธ ํ๊ฒ ๋๋ ๊ฑฐ์ฃ . - After being informed of a change in the concrete subject, a ConcreteObserver
object may query the subject for information. ConcreteObserver uses this
information to reconcile its state with that of the subject.
CS ๊ฐ ๋ณด๋ธ ์ํ์ ๋ณด๊ฐ ๋ณ๊ฒฝ๋์๋ค๋ ์๋ฆผ ๋ฉ์์ง๋ฅผ ๋ฐ์ ConcreteObserver( ์ดํ CO )๋ CS์ ์ํ์ ๋ณด๋ฅผ ์ป๊ธฐ์ํด getState()๋ฅผ ํ ์ ์์ต๋๋ค. CO๋ ์ด๋ ๊ฒ ์ป์ ์ ๋ณด๋ฅผ ์ด์ฉํด์ CS ์ํ์ ์์ ์ด ๊ฐ๊ณ ์๋ ์ํ์ ๋ณด๋ฅผ ๋์ผํ๊ฒ ๋๊ธฐํ์ํต๋๋ค.
The following interaction diagram illustrates the collaborations between a subject and two observers:
Note how the Observer object that initiates the change request postpones its update until it gets a notification from the subject. Notify is not always called by the subject. It can be called by an observer or by another kind of object entirely. ์ต์ ๋ฒ๊ฐ ๊ด์ฐฐ์ค์ธ ๊ฐ์ฒด์ ์ํ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์์ ์ด ๊ฐ์ง๊ณ ์๋ ์ํ์ ๋ณด๋ฅผ ์ ๋ฐ์ดํธํ๋ ๊ฒ์ notify๊ฐ ์ด๋ฃจ์ด์ง ๋ค์ ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ด notify๋ CS, CO, ๋๋ ์ ํ ๋ค๋ฅธ ์ข ๋ฅ์ ๊ฐ์ฒด๊ฐ ํธ์ถ ํ ์๋ ์์ต๋๋ค.
ํจํด ์ฌ์ฉ๋ฒ
- Differentiate between the core (or independent) functionality and the optional (or dependent) functionality.
- Model the independent functionality with a "subject" abstraction.
- Model the dependent functionality with an "observer" hierarchy.
- The Subject is coupled only to the Observer base class.
- The client configures the number and type of Observers.
- Observers register themselves with the Subject.
- The Subject broadcasts events to all registered Observers.
- The Subject may "push" information at the Observers, or, the Observers may "pull" the information they need from the Subject.
ํจํด ์ฌ์ฉ์ ์ฅ๋จ์ ( Consequences ):
The Observer pattern lets you vary subjects and observers independently. You can reuse subjects without reusing their observers, and vice versa. It lets you add observers without modifying the subject or other observers.
Further benefits and liabilities of the Observer pattern include thefollowing:
1. Abstract coupling between Subject and Observer. All a subject knows is that it has a list of observers, each conforming to the simple interface of the abstract Observer class.The subject doesn't know the concrete class of any observer. Thus the coupling between subjects and observers is abstract and minimal.
Because Subject and Observer aren't tightly coupled, they can belong to different layers of abstraction in a system. A lower-level subject can communicate and inform a higher-level observer, thereby keeping the system's layering intact. If Subject and Observer are lumped together, then the resulting object must either span two layers (and violate the layering), or it must be forced to live in one layer or the other (which might compromise the layering abstraction).
2. Support for broadcast communication. Unlike an ordinary request, the notification that a subject sends needn't specify its receiver. The notification is broadcast automatically to all interested objects that subscribed to it. The subject doesn't care how many interested objects exist; its only responsibility is to notify its observers. This gives you the freedom to add and remove observers at any time. It's up to the observer to handle or ignore a notification.
3. Unexpected updates. Because observers have no knowledge of each other's presence, they can be blind to the ultimate cost of changing the subject. A seemingly innocuous operation on the subject may cause a cascade of updates to observers and their dependent objects. Moreover, dependency criteria that aren't well-defined or maintained usually lead to spurious updates, which can be hard to track down.
This problem is aggravated by the fact that the simple update protocol provides no details on what changed in the subject. Without additional protocol to help observers discover what changed, they maybe forced to work hard to deduce the changes.
๊ด๋ จ ํจํด๋ค :
- Mediator : Byencapsulating complex update semantics, the ChangeManager acts asmediator between subjects and observers.
- Singleton :The ChangeManager may use the Singleton pattern to make it uniqueand globally accessible.
์ถ๊ฐ ์ ๋ณด :
- Chain of Responsibility, Command, Mediator, and Observer, address how you can decouple senders and receivers, but with different trade-offs. Chain of Responsibility passes a sender request along a chain of potential receivers. Command normally specifies a sender-receiver connection with a subclass. Mediator has senders and receivers reference each other indirectly. Observer defines a very decoupled interface that allows for multiple receivers to be configured at run-time.
- Mediator and Observer are competing patterns. The difference between them is that Observer distributes communication by introducing "observer" and "subject" objects, whereas a Mediator object encapsulates the communication between other objects. We've found it easier to make reusable Observers and Subjects than to make reusable Mediators.
- On the other hand, Mediator can leverage Observer for dynamically registering colleagues and communicating with them.
์ถ์ฒ : http://sourcemaking.com/design_patterns/observer
Design Patterns : Element of Reusable Object Oriented Software ( by GoF, 1994 )
'๐ป Programming > GoF Design Pattern' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋์์ธํจํด] ๋ฉ๋ฉํ ํจํด ( Memento Pattern ) (0) | 2015.11.03 |
---|---|
[๋์์ธํจํด] ์ธํฐํ๋ฆฌํฐ ํจํด ( Interpreter Pattern ) (0) | 2015.11.03 |
[๋์์ธํจํด] ์ค์ฌ์ ํจํด ( Mediator Pattern ) (0) | 2015.11.03 |
[๋์์ธํจํด] ๋ฐ๋ณต์(์ดํฐ๋ ์ดํฐ) ํจํด ( Iterator Pattern ) (0) | 2015.11.03 |
[๋์์ธํจํด] ์ปค๋งจ๋ ํจํด ( Command Pattern ) (0) | 2015.11.03 |