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.

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

 

์˜ค๋Š˜ ์‚ดํŽด๋ณผ  Facade ํŒจํ„ด์€ Structural ํŒจํ„ด์— ์†ํ•œ๋‹ค.  ์ด ํŒจํ„ด์€ ์ปดํฌ์ง€ํŠธ ํŒจํ„ด๊ณผ ๋น„์Šทํ•œ ๋‹ค์ด์–ด๊ทธ๋žจ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. 

Facade๋Š” ํผ์‚ฌ๋“œ๋ผ๊ณ  ์ฝ๋Š”๋‹ค. ํผ์ผ€์ด๋“œ, ํŒจ์ผ€์ด๋“œ ์ด๋”ด์‹์œผ๋กœ ์ฝ๋Š”๊ฒŒ ์•„๋‹ˆ๋‹ค.

 

Facade Pattern Structure


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

 Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.  

ํผ์‚ฌ๋“œ ํŒจํ„ด์€ ์„œ๋ธŒ์‹œ์Šคํ…œ ๋‚ด๋ถ€์— ์žˆ๋Š” ํด๋ž˜์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ํ•˜๋‚˜์˜ ํ†ตํ•ฉ๋œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ํŒจํ„ด์ด๋‹ค. 


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

 

ํฐ ์‹œ์Šคํ…œ์„ ์ž‘์€ ์‹œ์Šคํ…œ์œผ๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์€ ์‹œ์Šคํ…œ์˜ ๋ณต์žก๋„๋ฅผ ๋‚ฎ์ถฐ์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋””์ž์ธ์˜ ๊ณตํ†ต ๋ชฉํ‘œ๋Š” ์„œ๋ธŒ์‹œ์Šคํ…œ๋“ค ์‚ฌ์ด์˜ ์˜์กด์„ฑ์„ ์ค„์ด๊ณ  ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜ ํšŸ์ˆ˜๋ฅผ ์ค„์ด๋Š” ๊ฒƒ์ธ๋ฐ ํผ์‚ฌ๋“œ ํŒจํ„ด์ด ์ด๋ฅผ ์œ„ํ•œ ๋ฐฉ๋ฒ•์ค‘ ํ•˜๋‚˜์ด๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ ์ปค๋‹ค๋ž€ ์‚ฌ๊ฐํ˜•์ด ์„œ๋ธŒ์‹œ์Šคํ…œ์ด๊ณ  ๊ทธ ์•ˆ์— ์ž‘์€ ์‚ฌ๊ฐํ˜•๋“ค์€ ์„œ๋ธŒ์‹œ์Šคํ…œ์˜ ํด๋ž˜์Šค๋“ค์„ ์˜๋ฏธํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์„œ๋ธŒ์‹œ์Šคํ…œ ๋ฐ–์—๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ธŒ์‹œ์Šคํ…œ์— ์ ‘๊ทผํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ํด๋ž˜์Šค๋“ค์ด ์žˆ๋‹ค. 


๊ทธ๋Ÿฐ๋ฐ ์™ผ์ชฝ ๊ทธ๋ฆผ์„ ๋ณด๋ฉด ์„œ๋ธŒ์‹œ์Šคํ…œ์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ ํด๋ผ์ด์–ธํŠธ ํด๋ž˜์Šค๊ฐ€ ์ง์ ‘์ ์œผ๋กœ ์„œ๋ธŒ์‹œ์Šคํ…œ ๋‚ด๋ถ€์˜ ํด๋ž˜์Šค๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ๊ทธ๋ฆผ์œผ๋กœ๋ด๋„ ๋ญ”๊ฐ€ ๋ณต์žกํ•ด ๋ณด์ธ๋‹ค. ์„œ๋ธŒ์‹œ์Šคํ…œ์ด ๋ณ€๊ฒฝ๋˜๋ฉด?? ํด๋ผ์ด์–ธํŠธ ์ชฝ์—์„œ๋„ ์ˆ˜์ •ํ•ด์•ผํ•˜๋Š” ๋ถ€๋ถ„์ด ์ƒ๊ธธ ๊ฒƒ์ด๊ณ  ํฐ ํ”„๋กœ์ ํŠธ์˜€๋‹ค๋ฉด ์–ด๋””๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ• ์ง€ ์ฐพ์•„๋‚ด๊ธฐ๋„ ํž˜๋“ค๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ ์ด๋ฅผ ํผ์‚ฌ๋“œ ํŒจํ„ด์„ ์ ์šฉํ•ด์„œ ๋ฐ”๊พธ๋ฉด ์˜ค๋ฅธ์ชฝ๊ณผ ๊ฐ™์€ ๊ทธ๋ฆผ์ด ๋‚˜์˜จ๋‹ค. ํด๋ผ์ด์–ธํŠธ ํด๋ž˜์Šค๋“ค์ด ํผ์‚ฌ๋“œ๋ฅผ ํ†ตํ•ด์„œ ์„œ๋ธŒ์‹œ์Šคํ…œ์— ์ ‘๊ทผํ•˜๋„๋ก ํ•œ ๊ฒƒ์ด๋‹ค. ์ด๋•Œ ๊ผญ ํผ์‚ฌ๋“œ๋ฅผ ํ†ตํ•ด์„œ๋งŒ ์„œ๋ธŒ์‹œ์Šคํ…œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๋‹จ์ง€ ํผ์‚ฌ๋“œ๋ฅผ ํ†ตํ•ด์„œ ์„œ๋ธŒ์‹œ์Šคํ…œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ธธ์„ ์—ด๊ณ  ๊ทธ๋ ‡๊ฒŒ ์œ ๋„๋งŒ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ฐ•์ œ์„ฑ์ด ์—†๋‹ค๋Š” ๋œป์ด๋‹ค.  

 

์ด ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ์˜ˆ๊ฐ€ ์ปดํŒŒ์ผ๋Ÿฌ์ด๋‹ค. ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ˜„๋œ๋‹ค.



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

Use the Facade pattern when


ยท you want to provide a simple interface to a complex subsystem. Subsystems often get more complex as they evolve. Most patterns, when applied, result in more and smaller classes. This makes the subsystem more reusable and easier to customize, but it also becomes harder to use for clients that don't need to customize it. A facade can provide a simple default view of the subsystem that is good enough for most clients. Only clients needing more customizability will need to look beyond the facade.

๋ณต์žกํ•œ ์„œ๋ธŒ์‹œ์Šคํ…œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ„๋‹จํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ


ยท there are many dependencies between clients and the implementation classes of an abstraction. Introduce a facade to decouple the subsystem from clients and other subsystems, thereby promoting subsystem independence and portability.

์„œ๋ธŒ์‹œ์Šคํ…œ์„ ํด๋ผ์ด์–ธํŠธ ๋˜๋Š” ๋‹ค๋ฅธ ์„œ๋ธŒ์‹œ์Šคํ…œ์œผ๋กœ๋ถ€ํ„ฐ ๋””์ปคํ”Œ์‹œํ‚ด์œผ๋กœ์จ ๋…๋ฆฝ์„ฑ๊ณผ ํœด๋Œ€์„ฑ(์ด์‹์„ฑ)์„ ๋†’์ด๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ 


ยท you want to layer your subsystems. Use a facade to define an entry point to each subsystem level. If subsystems are dependent, then you can simplify the dependencies between them by making them communicate with each other solely through their facades.


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

ยท Facade (Compiler)
o knows which subsystem classes are responsible for a request.
o delegates client requests to appropriate subsystem objects.
ยท subsystem classes (Scanner, Parser, ProgramNode, etc.)
o implement subsystem functionality.
o handle work assigned by the Facade object.
o have no knowledge of the facade; that is, they keep no references to it.


์›๋ฆฌ ( Collaborations ) :

ยท Clients communicate with the subsystem by sending requests to Facade, which forwards them to the appropriate subsystem object(s). Although the subsystem objects perform the actual work, the facade may have to do work of its own to translate its interface to subsystem interfaces.

ํด๋ผ์ด์–ธํŠธ๋Š” ํผ์‚ฌ๋“œ์— ์š”์ฒญ์„ ํ•จ์œผ๋กœ์จ ๋ณต์žกํ•œ ์„œ๋ธŒ์‹œ์Šคํ…œ์— ์ ‘๊ทผํ•œ๋‹ค.
ยท Clients that use the facade don't have to access its subsystem objects directly.

ํด๋ผ์ด์–ธํŠธ๋Š” ๋ณต์žกํ•œ ์„œ๋ธŒ์‹œ์Šคํ…œ์˜ ๊ฐ์ฒด์— ์ง์ ‘์ ์œผ๋กœ ์ ‘๊ทผํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. 


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

 The Facade pattern offers the following benefits:


1. It shields clients from subsystem components, thereby reducing the number of objects that clients deal with and making the subsystem easier to use. ์‚ฌ์šฉ์ž๊ฐ€ ์•Œ์•„์•ผ ํ•˜๋Š” ์„œ๋ธŒ์‹œ์Šคํ…œ์˜ ๋ฒ”์œ„๋ฅผ ์ค„์—ฌ์ค€๋‹ค. 


2. It promotes weak coupling between the subsystem and its clients. 1๋ฒˆ์˜ ์ด์œ ๋กœ ์‚ฌ์šฉ์ž์™€ ์„œ๋ธŒ์‹œ์Šคํ…œ ์‚ฌ์ด์˜ ์ปคํ”Œ๋ง ๊ด€๊ณ„๋ฅผ ์•ฝํ™”์‹œ์ผœ์ค€๋‹ค. Often the components in a subsystem are strongly coupled. Weak coupling lets you vary the components of the subsystem without affecting its clients. Facades help layer a system and the dependencies between objects. They can eliminate complex or circular dependencies. This can be an important consequence when the client and the subsystem are implemented independently. Reducing compilation dependencies is vital in large software systems. You want to save time by minimizing recompilation when subsystem classes change. Reducing compilation dependencies with facades can limit the recompilation needed for a small change in an important subsystem. A facade can also simplify porting systems to other platforms, because it's less likely that building one subsystem requires building all others.


3. It doesn't prevent applications from using subsystem classes if they need to. ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•  ๋•Œ์—๋Š” ์„œ๋ธŒ์‹œ์Šคํ…œ์— ์ง์ ‘ ์ ‘๊ทผํ•˜์—ฌ ๋‚ด๋ถ€ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.Thus you can choose between ease of use and generality.

 

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

Abstract Factory can be used with Facade to provide an interface for creating subsystem objects in a subsystem-independent way. Abstract Factory can also be used as an alternative to Facade to hide platform-specific classes.
Mediator is similar to Facade in that it abstracts functionality of existing classes. However, Mediator's purpose is to abstract arbitrary communication between colleague objects, often centralizing functionality that doesn't belong in any one of them. A mediator's colleagues are aware of and communicate with the mediator instead of communicating with each other directly. In contrast, a facade merely abstracts the interface to subsystem objects to make them easier to use; it doesn't define new functionality, and subsystem classes don't know about it.
Usually only one Facade object is required. Thus Facade objects are often Singletons.


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

  • Facade defines a new interface, whereas Adapter uses an old interface. Remember that Adapter makes two existing interfaces work together as opposed to defining an entirely new one.
  • Whereas Flyweight shows how to make lots of little objects, Facade shows how to make a single object represent an entire subsystem.
  • Mediator is similar to Facade in that it abstracts functionality of existing classes. Mediator abstracts/centralizes arbitrary communications between colleague objects. It routinely "adds value", and it is known/referenced by the colleague objects. 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.
  • Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.
  • Facade objects are often Singletons because only one Facade object is required.
  • Adapter and Facade are both wrappers; but they are different kinds of wrappers. The intent of Facade is to produce a simpler interface, and the intent of Adapter is to design to an existing interface. While Facade routinely wraps multiple objects and Adapter wraps a single object; Facade could front-end a single complex object and Adapter could wrap several legacy objects.
  • Facade pattern is more like a helper for client applications, it doesnโ€™t hide subsystem interfaces from the client. Whether to use Facade or not is completely dependent on client code.
  • Facade pattern can be applied at any point of development, usually when the number of interfaces grow and system gets complex.
  • Subsystem interfaces are not aware of Facade and they shouldnโ€™t have any reference of the Facade interface.
  • Facade pattern should be applied for similar kind of interfaces, its purpose is to provide a single interface rather than multiple interfaces that does the similar kind of jobs.
  • We can use Factory Pattern with Facade to provide better interface to client systems.

์–ด๋Œ‘ํ„ฐ ํŒจํ„ด๊ณผ ํผ์‚ฌ๋“œ ํŒจํ„ด์˜ ์ฐจ์ด์ ์€ ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ํด๋ž˜์Šค๋ฅผ wrapํ•˜๋Š๋ƒ๊ฐ€ ์•„๋‹ˆ๋‹ค. Adapter ํŒจํ„ด์€ ํ•˜๋‚˜ ์ด์ƒ์˜ ํด๋ž˜์Šค๋ฅผ ๋ฌถ์–ด์„œ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์›ํ•˜๋Š” ์ถœ๋ ฅ์ด ๋‚˜์˜ค๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๊ณ  Facade ํŒจํ„ด์€ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์‚ฌ์šฉํ•˜๊ธฐ์—๋Š” ๋ณต์žกํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ•˜๋‚˜๋กœ ๋ฌถ๊ณ  ๊ฐ„๋‹จํ•œ ์ ‘๊ทผ๊ฒฝ๋กœ๋ฅผ ์ œ๊ณตํ•จ์œผ๋กœ์จ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์†์‰ฝ๊ฒŒ ๋ณต์žกํ•œ ์ธํ„ฐํŽ˜์ด์Šค ๋‚ด๋ถ€์˜ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

 

 

 

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

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

http://www.journaldev.com/1557/facade-pattern-in-java-example-tutorial