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.

์ด ํŒจํ„ด๋“ค์€ ๊ฐ์ฒด๋“ค ์‚ฌ์ด์˜ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์— ๊ด€์‹ฌ์„ ๊ฐ€์ง„๋‹ค.

 

์˜ค๋Š˜ ์‚ดํŽด๋ณผ  Mediator ํŒจํ„ด์€ Behavioral ํŒจํ„ด์— ์†ํ•ฉ๋‹ˆ๋‹ค. 

 

Mediator Pattern Structure

 



์ „ํ˜•์ ์ธ ๊ฐ์ฒด ๊ตฌ์กฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.




  

ํŒจํ„ด์˜ ๋ชฉ์  ( Intent ) :  

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.


ํŒจํ„ด์ด ๋‚˜์˜ค๊ฒŒ ๋œ ๋™๊ธฐ ( Motivation ) :

    We want to design reusable components, but dependencies between the potentially reusable pieces demonstrates the "spaghetti code" phenomenon. ๊ฐœ๋ฐœ์ž๋“ค์€ ๋ณดํ†ต ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ๋งŒ๋“ค๊ณ  ์‹ถ์–ดํ•˜์ง€๋งŒ ์ž ์žฌ์ ์œผ๋กœ ์žฌ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ๋“ค์˜ ์˜์กด์„ฑ์ด ์ผ๋ช… "์ŠคํŒŒ๊ฒŒํ‹ฐ ์ฝ”๋“œ"๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ŠคํŒŒ๊ฒŒํ‹ฐ ์ฝ”๋“œ๋ผ๋Š” ๊ฒƒ์€ ๊ฑฐ๋ฏธ์ค„์ฒ˜๋Ÿผ ๋’ค์ฃฝ๋ฐ•์ฃฝ ์˜์กด์„ฑ์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

 

์œ ๋‹‰์Šค๋‚˜ ๋ฆฌ๋ˆ…์Šค ์‹œ์Šคํ…œ์— ์žˆ๋Š” ์‚ฌ์šฉ์ž ํผ๋ฏธ์…˜ ์‹œ์Šคํ…œ์„ ์˜ˆ๋กœ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•œ ์‚ฌ์šฉ์ž๋Š” ์—ฌ๋Ÿฌ ๊ทธ๋ฃน์— ์†ํ•ด์žˆ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ํ•œ ๊ทธ๋ฃน์€ ์—ฌ๋Ÿฌ ์‚ฌ์šฉ์ž๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ ์ฒ˜๋Ÿผ ๋ง์ด์ฃ . 


์œ„ ์‹œ์Šคํ…œ์„ ๊ตฌํ˜„ํ•œ๋‹ค๊ณ  ํ•ด๋ณด์ž๊ตฌ์š”. ๊ทธ๋Ÿผ User ๊ฐ์ฒด๋“ค๊ณผ Group๊ฐ์ฒด๋“ค ์‚ฌ์ด์— ์—ฐ๊ฒฐ๊ณ ๋ฆฌ๊ฐ€ ์žˆ๊ฒ ์ฃ ? ๊ทธ ์—ฐ๊ฒฐ๊ณ ๋ฆฌ ๋•Œ๋ฌธ์— User๊ฐ์ฒด๋‚˜ Group๊ฐ์ฒด๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ ์„œ๋กœ ์˜ํ–ฅ์„ ๋ฏธ์น˜๊ฒŒ ๋ ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋‘˜ ์‚ฌ์ด์˜ ๊ด€๊ณ„๋งŒ์„ ์ •์˜ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค๋ฉด ์–ด๋–จ ๊นŒ์š”? User๊ฐ์ฒด๋‚˜ Group๊ฐ์ฒด๊ฐ€ ๋ณ€๊ฒฝ๋˜์–ด๋„ ์„œ๋กœ์—๊ฒŒ ์˜ํ–ฅ์„ ๋ฏธ์น˜๊ฒŒ ๋  ์ผ์ด ์‚ฌ๋ผ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ˆ˜๋งŽ์€ ๊ด€๊ณ„๋“ค์„ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋  ๋ฟ์•„๋‹ˆ๋ผ ์ˆ˜์ •ํ•˜๋Š” ์ผ๋„ ์‰ฌ์›Œ์งˆ ๊ฒ๋‹ˆ๋‹ค. 

 

์—ฌ๊ธฐ์„œ ๊ด€๊ณ„๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ค‘์žฌ์ž ๊ฐ์ฒด๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ค‘์žฌ์ž ๊ฐ์ฒด๋Š” ๋‹ค์Œ ์—ญํ• ๋“ค์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 

  1. encapsulates all interconnections
  2. acts as the hub of communication
  3. is responsible for controlling and coordinating the interactions of its clients
  4. promotes loose coupling by keeping objects from referring to each other explicitly


์œ ์šฉ์„ฑ ( Applicability ) :

 

Use the Mediator pattern when

  • a set of objects communicate in well-defined but complex ways. The resulting inter dependencies are unstructured and difficult to understand.

  • reusing an object is difficult because it refers to and communicates with many other objects.

  • a behavior that's distributed between several classes should be customizable without a lot of subclassing.


๋“ฑ์žฅ ์ธ๋ฌผ ( Participants ) :

  • Mediator
    o
    defines an interface for communicating with Colleague objects.
  • ConcreteMediator
    o
    implements cooperative behavior by coordinating Colleague objects.
    o
    knows and maintains its colleagues.
  • Colleague classes
    o
    each Colleague class knows its Mediator object.
    o
    each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague.


์›๋ฆฌ ( Collaborations ) :  

  • Colleagues send and receive requests from a Mediator object. The mediator implements the cooperative behavior by routing requests between the appropriate colleague(s).


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

  1. Identify a collection of interacting objects that would benefit from mutual decoupling.
  2. Encapsulate those interactions in the abstraction of a new class.
  3. Create an instance of that new class and rework all "peer" objects to interact with the Mediator only.
  4. Balance the principle of decoupling with the principle of distributing responsibility evenly.
  5. Be careful not to create a "controller" or "god" object.


ํŒจํ„ด ์‚ฌ์šฉ์˜ ์žฅ๋‹จ์  ( Consequences ): 

The Mediator pattern has the following benefits and drawbacks:

  1. It limits subclassing. A mediator localizes behavior that otherwise would be distributed amongseveral objects. Changing this behavior requires subclassing Mediatoronly; Colleague classes can be reused as is.

  2. It decouples colleagues. A mediator promotes loose coupling between colleagues. You can vary and reuse Colleague and Mediator classes independently.

  3. It simplifies object protocols. A mediator replaces many-to-many interactions with one-to-many interactions between the mediator and its colleagues. One-to-many relationships are easier to understand, maintain, and extend.

  4. It abstracts how objects cooperate. Making mediation an independent concept and encapsulating it in an object lets you focus on how objects interact apart from their individual behavior. That can help clarify how objects interact in a system.

  5. It centralizes control. The Mediator pattern trades complexity of interaction for complexity in the mediator. Because a mediator encapsulates protocols, it can become more complex than any individual colleague. This can make the mediator itself a monolith that's hard to maintain.

๊ด€๋ จ ํŒจํ„ด๋“ค :  

 

Facade differsfrom Mediator in that it abstracts a subsystem of objects to providea more convenient interface. Its protocol is uni-directional; thatis, Facade objects make requests of the subsystem classes but notvice versa. In contrast, Mediator enables cooperative behavior that colleague objects don't or can't provide, and the protocol is multi-directional.

Colleagues can communicate with the mediator using the Observer pattern.


์ถ”๊ฐ€ ์ •๋ณด :      

  • 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.
  • Mediator is similar to Facade in that it abstracts functionality of existing classes. Mediator abstracts/centralizes arbitrary communication between colleague objects, it routinely "adds value", and it is known/referenced by the colleague objects (i.e. it defines a multidirectional protocol). In contrast, Facade defines a simpler interface to a subsystem, it doesn't add new functionality, and it is not known by the subsystem classes (i.e. it defines a unidirectional protocol where it makes requests of the subsystem classes but not vice versa).

 

์ถœ์ฒ˜ :  http://sourcemaking.com/design_patterns/mediator

Design Patterns : Element of Reusable Object Oriented Software ( by GoF, 1994 )