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, ๋˜๋Š” ์ „ํ˜€ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ๊ฐ์ฒด๊ฐ€ ํ˜ธ์ถœ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 


ํŒจํ„ด ์‚ฌ์šฉ๋ฒ•

    1. Differentiate between the core (or independent) functionality and the optional (or dependent) functionality.
    2. Model the independent functionality with a "subject" abstraction.
    3. Model the dependent functionality with an "observer" hierarchy.
    4. The Subject is coupled only to the Observer base class.
    5. The client configures the number and type of Observers.
    6. Observers register themselves with the Subject.
    7. The Subject broadcasts events to all registered Observers.
    8. 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 )