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.

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

 

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

 

ํ…œ ํ”Œ๋ฆฟํ•˜๋ฉด ๋ญ๊ฐ€ ๋– ์˜ค๋ฅด๋Š”๊ฐ€? ์‚ฌ์ „์  ์˜๋ฏธ๋Š” ํ˜•ํŒ, ๊ฒฌ๋ณธ์ด๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ผ์ƒ์ ์œผ๋กœ ์“ธ ๋•Œ ํ…œํ”Œ๋ฆฟ์ด๋ผ ํ•˜๋ฉด ๋ญ”๊ฐ€์˜ ๊ธฐ๋ณธ๊ตฌ์กฐ(?)๋ผ๊ณ  ์ƒ๊ฐ์„ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ด ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ๋Š” ๋ฐ”๋กœ ๊ทธ๋Ÿฐ ํ˜•ํŒ, ๊ฒฌ๋ณธ, ๊ธฐ๋ณธ๊ตฌ์กฐ์˜ ์—ญํ• ์„ ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฐ ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋Š” ํŒจํ„ด์„ ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์ด๋ผ๊ณ  ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค. ์ž ์•„๋ž˜ ๊ทธ๋ฆผ์„ ์‚ดํŽด๋ณด์ž.

 


 

 

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

 

Template Method Pattern Structure

 


 


ํŒจํ„ด์˜ ๋ชฉ์  ( Intent ) :
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.


์–ด๋–ค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋ผˆ๋Œ€๋ฅผ ์ •์˜ํ•˜๊ณ  ๊ณผ์ •์€ ํ•˜์œ„ํด๋ž˜์Šค๊ฐ€ ์ •์˜ํ•˜๊ฒŒ๋” ํ•œ๋‹ค. ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ๋Š” ํ•˜์œ„ํด๋ž˜์Šค๋“ค์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ตฌ์กฐ์— ๋ณ€ํ™”๋ฅผ ์ฃผ์ง€ ์•Š๋Š” ์„ ์—์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋‚ด์˜ ํŠน์ • ๊ณผ์ •๋งŒ ์žฌ์ •์˜ ํ•  ์ˆ˜ ์žˆ๊ฒŒ๋” ํ•ด์ค€๋‹ค.

 

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



 ์›Œ ๋“œ ํ”„๋กœ์„ธ์„œ๋ฅผ ์˜ˆ๋ฅผ๋“ค์–ด ์„ค๋ช…ํ•˜์ž๋ฉด Document(๋ฌธ์„œ)ํŒŒ์ผ๊ณผ Application(์›Œ๋“œํ”„๋กœ์„ธ์„œ)๊ฐ€ ์žˆ๋Š”๋ฐ ์ด ์›Œ๋“œํ”„๋กœ์„ธ์„œ๊ฐ€ ๋ฌธ์„œ๋ฅผ ์—ด์–ด๋ณธ๋‹ค๋Š” ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ์ƒ๊ฐํ•ด๋ณด์ž. OpenDocument()๋Š” ๋ฌธ์„œ๊ฐ€ ์—ด์ˆ˜ ์žˆ๋Š” ๋ฌธ์„œ์ธ์ง€(์ฝ์–ด๋“ค์ผ ์ˆ˜ ์žˆ๋Š”์ง€) ๊ฒ€์‚ฌ๋ฅผ ํ•  ๊ฒƒ์ด๊ณ , ์›Œ๋“œํ”„๋กœ์„ธ์„œ๋‚ด์—์„œ ์ •์˜ํ•œ Document๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ๋ฌธ์„œ๋ฅผ ์ฝ์–ด๋“ค์—ฌ ๊ฐ์ฒด์— ์ €์žฅํ•  ๊ฒƒ์ด๋‹ค.  

We call OpenDocument a template method. A template method defines an algorithm in terms of abstract operations that subclasses override to provide concrete behavior. Application subclasses define the steps of the algorithm that check if the document can be opened (CanOpenDocument) and that create the Document (DoCreateDocument). Document classes define the step that reads the document (DoRead). The template method also defines an operation that lets Application subclasses know when the document is about to be opened (AboutToOpenDocument), in case they care.  

By defining some of the steps of an algorithm using abstract operations, the template method fixes their ordering, but it lets Application and Document subclasses vary those steps to suit their needs.

 

์—ฌ๊ธฐ์„œ ์ด OpenDocument()๋Š” ํ•˜๋‚˜์˜ ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ์ด๋‹ค. ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ๋Š” ๋ง๊ทธ๋Œ€๋กœ ํŠน์ • ๋™์ž‘์„ ํ•˜๊ธฐ์œ„ํ•œ ๊ณจ๊ฒฉ๋งŒ ์ œ๊ณตํ•œ๋‹ค. ๊ณจ๊ฒฉ๋‚ด๋ถ€์˜ ๊ตฌ์„ฑ์€ ํ•˜์œ„ํด๋ž˜์Šค๋“ค์ด ํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

 

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

The Template Method pattern should be used

ยท to implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary.

ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์€ ๋ณ€ํ•˜์ง€ ์•Š์„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ตฌ์กฐ๋งŒ ์ •์˜ํ•˜๊ณ  ๊ตฌ์ฒด์ ์ธ ๊ตฌํ˜„์€ ํ•˜์œ„ํด๋ž˜์Šค๋“ค์—๊ฒŒ ์œ„์ž„ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. 

 

ยท when common behavior among subclasses should be factored and localized in a common class to avoid code duplication. This is a good example of"refactoring to generalize" as described by Opdyke and Johnson.

You first identify the differences in the existing code and then separate the differences into new operations. Finally, you replace the differing code with a template method that calls one of these new operations.

ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์€ ์—ฌ๋Ÿฌ ํ•˜์œ„ ํด๋ž˜์Šค๋“ค ์ค‘์—์„œ ์ค‘๋ณต๋œ ๋ถ€๋ถ„์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋„ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.


ยท to control subclasses extensions. You can define a template method that calls "hook" operations (see Consequences) at specific points, thereby permitting extensions only at those points.

ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์€ ํ•˜์œ„ ํด๋ž˜์Šค๋“ค์˜ ํ™•์žฅ์„ ์ปจํŠธ๋กคํ•˜๊ณ ์ž ํ•  ๋•Œ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ์— ํŠน์ •์‹œ์ ์— hook๋ฅผ ํ˜ธ์ถœํ•˜๋„๋ก ์ •์˜ํ•˜์—ฌ ๊ทธ ํŠน์ • ์‹œ์ ์—๋งŒ ํ™•์žฅ์„ ํ—ˆ์šฉํ•˜๋„๋ก ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ( ๋ฌด์Šจ ๋ง์ธ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค. ์•„๋ž˜์ชฝ์— Consequences์—์„œ hook๊ฐ€ ๋ญ”์ง€ ๋” ์•Œ์•„๋ณด์ž ) 

 

 

๋“ฑ์žฅ ์ธ๋ฌผ ( Participants ) :
ยท AbstractClass (Application)
o defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm.
o implements a template method defining the skeleton of an algorithm.The template method calls primitive operations as well as operationsdefined in AbstractClass or those of other objects.
ยท ConcreteClass (MyApplication)
o implements the primitive operations to carry out subclass-specific steps of the algorithm.

 

์›๋ฆฌ ( Collaborations ) :

ConcreteClass relies on AbstractClass to implement the invariant steps of the algorithm.


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

Template methods are a fundamental technique for code reuse. They are particularly important in class libraries, because they are the means for factoring out common behavior in library classes.
Template methods lead to an inverted control structure that's sometimes referred to as "the Hollywood principle," that is, "Don't call us, we'll call you" .
This refers to how a parent class calls the operations of a subclass and not the other way around.

Template methods call the following kinds of operations:
ยท concrete operations (either on the ConcreteClass or onclient classes);
ยท concrete AbstractClass operations (i.e., operations that are generally useful to subclasses);
ยท primitive operations (i.e., abstract operations);
ยท factory methods (see Factory Method); and
ยท hook operations, which provide default behavior that subclasses can extend if necessary. A hook operation often doesnothing by default. It's important for template methods to specify which operations arehooks (may be overridden) and which are abstract operations(must be  overridden). To reuse an abstract class effectively,subclass writers must understand which operations are designed foroverriding.
A subclass can extend a parent class operation's behavior byoverriding the operation and calling the parent operation explicitly:


void DerivedClass::Operation () {
// DerivedClass extended behavior
ParentClass::Operation();
}

 

Unfortunately, it's easy to forget to call the inherited operation.We can transform such an operation into a template method to give the parent control over how subclasses extend it. The idea is to call a hook operation from a template method in the parent class.Then subclasses can then override this hook operation:


void ParentClass::Operation () {
// ParentClass behavior
HookOperation();
}


HookOperation does nothing in ParentClass:


void ParentClass::HookOperation () { }


Subclasses override HookOperation to extend its behavior:


void DerivedClass::HookOperation () {
// derived class extension
}


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

Factory Methods are often called by template methods. In the Motivation example, the factory method DoCreateDocument is called by the template method OpenDocument.
Strategy : Template methods use inheritance to vary part of an algorithm. Strategies use delegation to vary the entire algorithm.

 

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

  • Strategy is like Template Method except in its granularity.
  • Template Method uses inheritance to vary part of an algorithm. Strategy uses delegation to vary the entire algorithm.
  • Strategy modifies the logic of individual objects. Template Method modifies the logic of an entire class.
  • Factory Method is a specialization of Template Method.

 

 

 

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

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