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.

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

 

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

 

Composite Pattern Structure

 

A typical Composite object structure might look like this:



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

 

Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

์ปด ํฌ์ง€ํŠธ ํŒจํ„ด์€ ๊ฐ์ฒด๋“ค์˜ ๊ด€๊ณ„๋ฅผ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋กœ ๊ตฌ์„ฑํ•˜์—ฌ ๋ถ€๋ถ„-์ „์ฒด ๊ณ„์ธต์„ ํ‘œํ˜„ํ•˜๋Š” ํŒจํ„ด์œผ๋กœ, ์‚ฌ์šฉ์ž๊ฐ€ ๋‹จ์ผ ๊ฐ์ฒด์™€ ๋ณตํ•ฉ ๊ฐ์ฒด ๋ชจ๋‘ ๋™์ผํ•˜๊ฒŒ ๋‹ค๋ฃจ๋„๋ก ํ•˜๋Š” ํŒจํ„ด์ด๋‹ค. ๋”ฐ๋ผ์„œ, {๊ฐ์ฒด์˜ ๊ทธ๋ฃน}๊ณผ {ํ•˜๋‚˜์˜ ๊ฐ์ฒด}๊ฐ€ ๋™์ผํ•œ ํ–‰์œ„๋ฅผ ํ•  ๋•Œ ์ ์šฉ๋˜์•ผ ํ•˜๋ฉฐ, ๋˜ํ•œ ํŠธ๋ฆฌ๊ตฌ์กฐ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค. 

 

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


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


 

์œ„ ๋‹ค์ด์–ด๊ทธ๋žจ์€ ์ง์„ , ์‚ฌ๊ฐํ˜•, ๋ฌธ์ž, ๊ทธ๋ฆผ์„ ๋ชจ๋‘ ๊ทธ๋ž˜ํ”ฝ ๊ฐ์ฒด๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์–ด ๋ฒ„๋ฆฌ๊ณ  ๊ฐ ๊ทธ๋ž˜ํ”ฝ ํด๋ž˜์Šค๋Š” ์ž์‹์œผ๋กœ ๊ทธ๋ž˜ํ”ฝ ํด๋ž˜์Šค๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ๋‹ค. ๊ทธ๋ž˜ํ”ฝ ํด๋ž˜์Šค๊ฐ€ ๊ทธ๋ž˜ํ”ฝ ํด๋ž˜์Šค๋ฅผ ์ž์‹์œผ๋กœ ๊ฐ–๋Š”๋‹ค? ์ดํ•ด๊ฐ€ ๋˜๋Š”๊ฐ€? ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด์ž.

๋‚ด ๊ฐ€ ์–ด์ œ ํ•˜๋‚˜์˜ ๊ทธ๋ฆผ์„ ๊ทธ๋ ธ๋‹ค. ์ด ๊ทธ๋ฆผ์€ ์‚ฌ๊ฐํ˜•๊ณผ ์ง์„  ๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ž๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์˜ค๋Š˜ ๋‚˜๋Š” ๋‘๋ฒˆ์งธ ๊ทธ๋ฆผ์„ ๊ทธ๋ฆฌ๋ฉด์„œ ์–ด์ œ ๊ทธ๋ฆฐ ๊ทธ๋ฆผ๊ณผ ๋˜ ๋‹ค๋ฅธ ์„ , ์‚ฌ๊ฐํ˜•์„ ๊ทธ๋ ค๋„ฃ์—ˆ๋‹ค. ์ด๊ฒƒ์„ ๋‹ค์ด์–ด๊ทธ๋žจ์œผ๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์ด ์•„๋ž˜ ๊ทธ๋ฆผ์ด๋‹ค.

aPicture, aTest, aLine, aRectangle์€ ๋ชจ๋‘ ๊ทธ๋ž˜ํ”ฝ ๊ฐ์ฒด์ด๋‹ค. ์ด ๊ทธ๋ž˜ํ”ฝ ๊ฐ์ฒด๋“ค์€ ๋˜ ๋‹ค๋ฅธ ๊ทธ๋ž˜ํ”ฝ ๊ฐ์ฒด๋“ค์„ ์ž์‹์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ด์ œ ์ดํ•ด๊ฐ€ ๋˜๋Š”๊ฐ€?


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

 Use the Composite pattern when
ยท you want to represent part-whole hierarchies of objects.
ยท you want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly.



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

ยท Component (Graphic)
o declares the interface for objects in the composition.
o implements default behavior for the interface common to all classes, as appropriate.
o declares an interface for accessing and managing its child components.
o (optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate.
ยท Leaf (Rectangle, Line, Text, etc.)
o represents leaf objects in the composition. A leaf has no children.
o defines behavior for primitive objects in the composition.
ยท Composite (Picture) 

o defines behavior for components having children.
o stores child components.
o implements child-related operations in the Component interface.
ยท Client
o manipulates objects in the composition through the Component interface.

 


์›๋ฆฌ ( Collaborations ) :

ยท Clients use the Component class interface to interact with objects in the composite structure. If the recipient is a Leaf, then the request is handled directly. If the recipient is a Composite, then it usually forwards requests to its child components, possibly performing additional operations before and/or after forwarding.


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

The Composite pattern


ยท defines class hierarchies consisting of primitive objects and composite objects. Primitive objects can be composed into more complex objects, which in turn can be composed, and so on recursively. Wherever client code expects a primitive object, it can also take a composite object.  

ยท makes the client simple. Clients can treat composite structures and individual objects uniformly. Clients normally don't know (and shouldn't care) whether they're dealing with a leaf or a composite component. This simplifies client code, because it avoids having to write tag-and-case-statement-style functions over the classes that define the composition.
ยท makes it easier to add new kinds of components. Newly defined Composite or Leaf subclasses work automatically with existing structures and client code. Clients don't have to be changed for new Component classes.
ยท can make your design overly general. The disadvantage of making it easy to add new components is that it makes it harder to restrict the components of a composite. Sometimes you want a composite to have only certain components. With Composite, you can't rely on the type system to enforce those constraints for you. You'll have to use run-time checks instead.

 

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

 Often the component-parent link is used for a Chain of Responsibility.
Decorator is often used with Composite. When decorators and composites are used together, they will usually have a common parent class. So decorators will have to support the Component interface with operations like Add, Remove, and GetChild.
Flyweight lets you share components, but they can no longer refer to their parents.
Iterator can be used to traverse composites.
Visitor localizes operations and behavior that would otherwise be distributed across Composite and Leaf classes.

 

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

  • Composite and Decorator have similar structure diagrams, reflecting the fact that both rely on recursive composition to organize an open-ended number of objects.
  • Composite can be traversed with Iterator. Visitor can apply an operation over a Composite. Composite could use Chain of Responsibility to let components access global properties through their parent. It could also use Decorator to override these properties on parts of the composition. It could use Observer to tie one object structure to another and State to let a component change its behavior as its state changes.
  • Composite can let you compose a Mediator out of smaller pieces through recursive composition.
  • Decorator is designed to let you add responsibilities to objects without subclassing. Composite's focus is not on embellishment but on representation. These intents are distinct but complementary. Consequently, Composite and Decorator are often used in concert.
  • Flyweight is often combined with Composite to implement shared leaf nodes.

 

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

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

http://www.journaldev.com/1535/composite-design-pattern-in-java-example-tutorial