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.

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

 

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


๋นŒ๋” ํŒจํ„ด์€ ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ๋˜๋Š” ์ถ”์ƒ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์˜ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋‚˜์˜จ ํŒจํ„ด์ด๋‹ค. ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ๋˜๋Š” ์ถ”์ƒ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์—๋Š” ๊ฐ์ฒด๊ฐ€ ๋งŽ์€ ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๊ฒฝ์šฐ ๋‘๊ฐ€์ง€ ์ค‘์š”ํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. 

 

์ฒซ ์งธ๋กœ, ํด๋ผ์ด์–ธํŠธ๊ฐ€ ํŒฉํ† ๋ฆฌ ํด๋ž˜์Šค๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ์ธ์ž๊ฐ€ ๋งŽ์•„์ง€๊ฒŒ๋˜๋ฉด( ํŒฉํ† ๋ฆฌ๊ฐ€ ๋งŽ์€ ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉด ์ด์— ๋Œ€ํ•ด ์„ค์ •์„ ํ•˜๊ธฐ์œ„ํ•ด ๋งŽ์€ ์ธ์ž๋ฅผ ๋ฐ›์•„์•ผ ํ•œ๋‹ค ) ์ž˜๋ชป๋œ ์ธ์ž๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š” ์ผ๋„ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ผ์–ด๋‚  ๋ฟ ์•„๋‹ˆ๋ผ ๊ด€๋ฆฌํ•˜๊ธฐ๋„ ํž˜๋“ค์–ด์ง„๋‹ค.

๋‘˜์งธ๋กœ, ๋ช‡๋ช‡ ์ธ์ž๋“ค์€ optional์ผ ์ˆ˜ ์žˆ์ง€๋งŒ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์—์„œ๋Š” ๋ชจ๋“  ์ธ์ž๋ฅผ ๋„˜๊ฒจ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ( optional ์ธ์ž๋Š” null๋กœ ๋„˜๊ธด๋‹ค. ์ด๊ฑด ๊ท€์ฐฎ์€ ์ผ์ด๋‹ค. )

 

๋นŒ๋” ํŒจํ„ด์€ ๋‹จ๊ณ„๋ณ„๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋งˆ์ง€๋ง‰ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•จ์œผ๋กœ์จ ์œ„์™€ ๊ฐ™์€ ๋ฌธ์ œ๋“ค์„ ํ•ด๊ฒฐํ•ด์ค€๋‹ค. 


 

Builder Pattern Structure



 


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

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

 ๋ณตํ•ฉ ๊ฐ์ฒด์˜ ์ƒ์„ฑ ๊ณผ์ •๊ณผ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๋™์ผํ•œ ์ƒ์„ฑ ์ ˆ์ฐจ์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ํ‘œํ˜„ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ํŒจํ„ด


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


 

์˜ˆ ๋ฅผ๋“ค์–ด RTFํฌ๋งท ํ˜•ํƒœ์˜ ํŒŒ์ผ์„ ASCIIText๋‚˜ TeXText ๋˜๋Š” ๋‹ค๋ฅธ TextWidget์˜ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ ๋‹ค๊ณ  ํ•˜์ž. ์ด๋•Œ ๋ณ€ํ™˜๋˜์–ด ๋‚˜์˜ค๋Š” ํฌ๋งท์„ ์‰ฝ๊ฒŒ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ ๋‚˜์˜จ ํŒจํ„ด์ด ๋ฐ”๋กœ ์ด ๋นŒ๋” ํŒจํ„ด์ด๋‹ค. ์œ„ ๋‹ค์ด์–ด๊ทธ๋žจ์—์„œ ๊ฐ๊ฐ์˜ ์ปจ๋ฒ„ํ„ฐ ํด๋ž˜์Šค๋“ค์€ ๋ชจ๋‘ builder์ด๋ฉฐ reader๋Š” director๋ผ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ณ€ํ˜•๋˜์–ด ๋‚˜์˜ค๋Š” Text๊ฐ์ฒด๋“ค์€ product๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค. 

 


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

Use the Builder pattern when
ยท the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled.
ยท the construction process must allow different representations for the object that's constructed.

 

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

ยท Builder
o specifies an abstract interface for creating parts of a Product object.
ยท ConcreteBuilder
o constructs and assembles parts of the product by implementing the Builder interface.
o defines and keeps track of the representation it creates.
o provides an interface for retrieving the product.
ยท Director
o constructs an object using the Builder interface.
ยท Product
o represents the complex object under construction. ConcreteBuilder builds the product's internal representation and defines the process by which it's assembled.
o includes classes that define the constituent parts, including interfaces for assembling the parts into the final result.


์›๋ฆฌ ( Collaborations ) :

ยท The client creates the Director object and configures it with the desired Builder object.
ยท Director notifies the builder whenever a part of the product should be built.
ยท Builder handles requests from the director and adds parts to the product.
ยท The client retrieves the product from the builder.

 

The following interaction diagram illustrates how Builder and Director cooperate with a client.

 

 


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

Here are key consequences of the Builder pattern:


1. It lets you vary a product's internal representation. The Builder object provides the director with an abstract interface for constructing the product. The interface lets the builder hide the representation and internal structure of the product. It also hides how the product gets assembled. Because the product is constructed through an abstract interface, all you have to do to change the product's internal representation is define a new kind of builder.


2. It isolates code for construction and representation. The Builder pattern improves modularity by encapsulating the way a complex object is constructed and represented. Clients needn't know anything about the classes that define the product's internal structure; such classes don't appear in Builder's interface. Each ConcreteBuilder contains all the code to create and assemble a particular kind of product. The code is written once; then different Directors can reuse it to build Product variants from the same set of parts.
In the earlier RTF example, we could define a reader for a format other than RTF, say, an SGMLReader, and use the same TextConverters to generate ASCIIText, TeXText, and TextWidget renditions of SGML documents.


3. It gives you finer control over the construction process. Unlike creational patterns that construct products in one shot, the Builder pattern constructs the product step by step under the director's control. Only when the product is finished does the director retrieve it from the builder. Hence the Builder interface reflects the process of constructing the product more than other creational patterns. This gives you finer control over the construction process and consequently the internal structure of the resulting product. 

 

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

 Abstract Factory is similar to Builder in that it โ€‹constructs too may complex objects. The primary difference is that the Builder pattern focuses on constructing a complex object step by step. Abstract Factory's emphasis is on families of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory pattern is concerned, the product gets returned immediately.
A Composite is what the builder often builds.

 

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

  • Sometimes creational patterns are complementory: Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementations.
  • 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.
  • Builder often builds a Composite.
  • 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/builder

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

http://www.journaldev.com/1425/builder-design-pattern-in-java