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.

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

 

์˜ค๋Š˜ ์‚ดํŽด๋ณผ  Abstract Factory ํŒจํ„ด์€ Creational ํŒจํ„ด์— ์†ํ•œ๋‹ค.  

 

Abstract Factory Pattern Structure


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

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

์ถ”์ƒ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์€ ๊ตฌ์ƒํด๋ž˜์Šค๋ฅผ ๋ช…์‹œํ•˜์ง€ ์•Š์•„๋„ ์—ฐ๊ด€์žˆ๊ฑฐ๋‚˜ ์˜์กด์ ์ธ ๊ฐ์ฒด์˜ ๊ทธ๋ฃน์„ ์ƒ์„ฑํ•˜๊ธฐ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ํŒจํ„ด์ด๋‹ค.

์ฐธ๊ณ ๋กœ Factory๋Š” ์‹ฑ๊ธ€ํ„ด ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค. ( ๋™์ผํ•œ ์ œํ’ˆ์„ ์ƒ์‚ฐํ•˜๋Š” ๋‘๊ฐœ์˜ ๋‹ค๋ฅธ ๊ณต์žฅ์„ ํ•˜๋‚˜์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๊ฐ€์ง€๊ณ  ์žˆ์„ ํ•„์š”๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ) 

 

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



 GOF์˜ ๋””์ž์ธ ํŒจํ„ด์—์„œ ์˜ˆ๋กœ ๋“  ๋‚ด์šฉ์„ ์ข€ ๊พธ๋ฉฐ์„œ ์–˜๊ธฐํ•ด๋ณด๊ฒ ๋‹ค.  

์ž, ์šฐ๋ฆฌ ํšŒ์‚ฌ๋Š” ์—ฌ๋Ÿฌ๊ฐœ์˜ ์œ„์ ฏ์„ ์ œ๊ณตํ•˜๋Š”๋ฐ ์œ„์ ฏ๋งˆ๋‹ค Look and Feel ( ์ดํ•˜ LNF )์ด ๋‹ค๋ฅด๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ Window์™€ ScrollBar๋ฅผ ๋‚ด๊ฐ€ ๋งŒ๋“ค์–ด ๋†จ๋Š”๋ฐ ์‹ ์ž…๊ฐœ๋ฐœ์ž๊ฐ€ ์ด๋ฅผ ์ด์šฉํ•ด์„œ ์œ„์ ฏ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๊ณ  ์‹ถ๋‹ค. ๊ทธ๋ž˜์„œ ๋‚˜๋Š” WidgetFactory๋ฅผ ๋งŒ๋“ค์–ด ์‹ ์ž…๊ฐœ๋ฐœ์ž๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•  ๊ฒƒ์ด๋‹ค. ๋‚ด๊ฐ€ ๋งŒ๋“  Window์™€ ScrollBar์˜ ๊ตฌ์ƒ ํด๋ž˜์Šค๋ฅผ ๋ชฐ๋ผ๋„ WidgetFactory์—์„œ ์•Œ์•„์„œ ์ƒ์„ฑํ•ด ์ค„ ํ…Œ๋‹ˆ ์‹ ์ž…๊ฐœ๋ฐœ์ž๋Š” WidgetFactory ์‚ฌ์šฉ๋ฒ•๋งŒ ์•Œ๋ฉด ๋˜๊ฒ ์ง€. ์‹ ์ž…๊ฐœ๋ฐœ์ž๊ฐ€ PMWidget์„ ๋งŒ๋“ค๊ณ  ์‹ถ์œผ๋ฉด PMWidgetFactory๋ฅผ ์ด์šฉํ•˜๋ฉด๋˜๊ณ  MotifWidget์„ ๋งŒ๋“ค๊ณ  ์‹ถ์œผ๋ฉด MotifWidgetFactory๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋Š”๊ฑฐ๋‹ค. 


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

Use the Abstract Factory pattern when


ยท a system should be independent of how its products are created, composed, and represented.
ยท a system should be configured with one of multiple families of products.  

ยท a family of related product objects is designed to be used together, and you need to enforce this constraint.
ยท you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

 

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

ยท AbstractFactory (WidgetFactory)
o declares an interface for operations that create abstract product objects.
ยท ConcreteFactory (MotifWidgetFactory, PMWidgetFactory)
o implements the operations to create concrete product objects.
ยท AbstractProduct (Window, ScrollBar)
o declares an interface for a type of product object.
ยท ConcreteProduct (MotifWindow, MotifScrollBar)
o defines a product object to be created by the corresponding concrete factory.
o implements the AbstractProduct interface.
ยท Client
o uses only interfaces declared by AbstractFactory and AbstractProduct classes.


์›๋ฆฌ ( Collaborations ) :

ยท Normally a single instance of a ConcreteFactory class is created at run-time.
This concrete factory creates product objects having a particular implementation. To create different product objects, clients should use a different concrete factory.
ยท AbstractFactory defers creation of product objects to its ConcreteFactory subclass.

 



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

The Abstract Factory pattern has the following benefits and liabilities:


1. It isolates concrete classes. The Abstract Factory pattern helps you control the classes of objects that an application creates. Because a factory encapsulates the responsibility and the process of creating product objects, it isolates clients from implementation classes. Clients manipulate instances through their abstract interfaces. Product class names are isolated in the implementation of the concrete factory; they do not appear in client code.  

 

2. It makes exchanging product families easy. The class of a concrete factory appears only once in an applicationโ€”that is, where it's instantiated. This makes it easy to change the concrete factory an application uses. It can use different product configurations simply by changing the concrete factory. Because an abstract factory creates a complete family of products, the whole product family changes at once. In our user interface example, we can switch from Motif widgets to Presentation Manager widgets simply by switching the corresponding factory objects and recreating the interface.


3. It promotes consistency among products. When product objects in a family are designed to work together, it's important that an application use objects from only one family at a time. AbstractFactory makes this easy to enforce.


4. Supporting new kinds of products is difficult. Extending abstract factories to produce new kinds of Products isn't easy. That's because the AbstractFactory interface fixes the set of products that can be created. Supporting new kinds of products requires extending the factory interface, which involves changing the AbstractFactory class and all of its subclasses

์ƒˆ ๋กœ์šด ์ข…๋ฅ˜์˜ ์ œํ’ˆ์„ ์ƒ์‚ฐํ•˜๋Š” ๊ฒƒ์ด ํž˜๋“ค๋‹ค. ์ƒˆ๋กœ์šด ์ œํ’ˆ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด AbstractFactory๊ฐ€ ๋ณ€๊ฒฝ์ด ๋˜๋ฉด ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ตฌ์ƒํด๋ž˜์Šค(concrete classes)๋“ค์ด ๋ชจ๋‘ ๋ณ€๊ฒฝ๋˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด์„œ Prototype-based Factory ( ํ”„๋กœํ† ํƒ€์ž… ํŒจํ„ด์„ ์ด์šฉํ•œ Factory )๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.  

 

 

 

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

 AbstractFactory classes are often implemented with factory methods (Factory Method), but they can also be implemented using Prototype.
A concrete factory is often a singleton.

 

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

  • Sometimes creational patterns are competitors: there are cases when either Prototype or Abstract Factory could be used profitably. At other times they are complementary: Abstract Factory might store a set of Prototypes from which to clone and return product objects, Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementation.
  • Abstract Factory, Builder, and Prototype define a factory object that's responsible for knowing and creating the class of product objects, and make it a parameter of the system. Abstract Factory has the factory object producing objects of several classes. Builder has the factory object building a complex product incrementally using a correspondingly complex protocol. Prototype has the factory object (aka prototype) building a product by copying a prototype object.
  • Abstract Factory classes are often implemented with Factory Methods, but they can also be implemented using Prototype.
  • Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.
  • Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.
  • Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.

 

 

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

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