๐Ÿ’ป Programming/GoF Design Pattern (23)

 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 )


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.

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

 

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

 

State Pattern Structure

 

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

Allow an object to alter its behavior when its internal state changes.The object will appear to change its class.

State ํŒจํ„ด์˜ ๋ชฉ์ ์€ ๋‚ด๋ถ€ ์ƒํƒœ์˜ ๋ณ€ํ™”์— ๋”ฐ๋ผ ๊ฐ์ฒด์˜ ํ–‰์œ„๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.


 ์œ„ ๋‹ค์ด์–ด๊ทธ๋žจ์„ ์˜ˆ์ œ๋กœ ๋“ค๋ฉด TCPConnection์€ TCPState๊ฐ์ฒด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด TCPState๊ฐ์ฒด๋Š” ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค๋กœ ํฌ๊ฒŒ ์„ธ๊ฐ€์ง€ ์ƒํƒœ๋กœ ๋‚˜๋‰˜๋Š”๋ฐ ( Established, Listen, Closed ) ์ด ์ƒํƒœ์˜ ๋ณ€ํ™”์— ๋”ฐ๋ผ TCPConnection์—์„œ ํ˜ธ์ถœํ•˜๋Š” open()๋ฉ”์†Œ๋“œ๊ฐ€ TCPEstablished๊ฐ์ฒด์˜ open()์ด ๋  ์ˆ˜๋„ ์žˆ๊ณ  TCPListen๋‚˜  TCPClosed ๊ฐ์ฒด์˜ open()์ด ๋  ์ˆ˜๋„ ์žˆ๋‹ค.  

 

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

ยท An object's behavior depends on its state, and it must change its behavior at run-time depending on that state.

๊ฐ์ฒด์˜ ํ–‰์œ„๊ฐ€ ์ƒํƒœ์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ๋ณ€ํ•ด์•ผ ํ•  ๋•Œ
ยท Operations have large, multipart conditional statements that depend on the object's state. This state is usually represented by one or more enumerated constants. Often, several operations will contain this same conditional structure. The State pattern puts each branch of the conditional in a
separate class. This lets you treat the object's state as an object in its own right that can vary independently from other objects.

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

 

๋“ฑ์žฅ ์ธ๋ฌผ :
ยท Context (TCPConnection)
o defines the interface of interest to clients.
o maintains an instance of a Concrete State subclass that defines the current state.

ยท State (TCPState)
o defines an interface for encapsulating the behavior associated with a particular state of the Context. 

 

ยท ConcreteState subclasses (TCPEstablished, TCPListen, TCPClosed)
o each subclass implements a behavior associated with a state of the Context.

 

์›๋ฆฌ ( Collaborations ) :

ยท Context delegates state-specific requests to the current ConcreteState object.

Context๋Š” ์ƒํƒœ์— ๋”ฐ๋ฅธ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ์„ ConcreteState๊ฐ์ฒด์— ์œ„์ž„ํ•œ๋‹ค.
ยท A context may pass itself as an argument to the State object handling the request. This lets the State object access the context if necessary.

์ƒํƒœ๊ฐ์ฒด๊ฐ€ Context๋ฅผ ํ•„์š”๋กœํ•œ๋‹ค๋ฉด Context๊ฐ์ฒด๋Š” ์ž์‹ ์„ argument๋กœ ๋„˜๊ฒจ์ฃผ์–ด ์ƒํƒœ๊ฐ์ฒด์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.
ยท Context is the primary interface for clients. Clients can configure a context with State objects. Once a context is configured, its clients don't have to deal with the State objects directly.

ํด๋ผ์ด์–ธํŠธ๋Š” ์ƒํƒœ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด์„œ context๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ํ•œ๋ฒˆ ์„ค์ •ํ•˜๋ฉด ๊ทธ ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ์ƒํƒœ๊ฐ์ฒด์— ์ง์ ‘์ ์œผ๋กœ ์ ‘๊ทผํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
ยท Either Context or the ConcreteState subclasses can decide which state succeeds another and under what circumstances.

Context ๋˜๋Š” ConcreteState์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋“ค์€ ์ƒํƒœ์˜ ์ˆœ์„œ๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.


ํŒจํ„ด ์‚ฌ์šฉ์˜ ์žฅ๋‹จ์  ( Consequences ):
1. It localizes state-specific behavior and partitions behavior for different states.The State pattern puts all behavior associated with a particular state into one object. Because all state-specific code lives in a State subclass, new states and transitions can be added easily by defining new subclasses.
An alternative is to use data values to define internal states and have Context operations check the data explicitly. But then we'd have look-alike conditional or case statements scattered throughout Context's implementation. Adding a new state could require changing several operations, which complicates maintenance.

The State pattern avoids this problem but might introduce another, because the pattern distributes behavior for different states across several State subclasses. This increases the number of classes and is less compact than a single class. But such distribution is actually good if there are many states, which would otherwise necessitate large conditional statements.
Like long procedures, large conditional statements are undesirable.They're monolithic and tend to make the code less explicit, which in turn makes them difficult to modify and extend. The State pattern offers a better way to structure state-specific code. The logic that determines the state transitions doesn't reside in monolithic if or switch statements but instead is partitioned between the State  subclasses. Encapsulating each state transition and action in a class elevates the idea of an execution state to full object status. That imposes structure on the code and makes its intent clearer. 

 

2. It makes state transitions explicit.When an object defines its current state solely in terms of internal data values, its state transitions have no explicit representation;they only show up as assignments to some variables. Introducing separate objects for different states makes the transitions more explicit. Also, State objects can protect the Context from inconsistent internal states, because state transitions are atomic from the Context's perspectiveโ€”they happen by rebinding one variable (the Context's State object variable), not several. 

 

3. State objects can be shared.If State objects have no instance variablesโ€”that is, the state they represent is encoded entirely in their typeโ€”then contexts can sharea State object. When states are shared in this way, they are essentially flyweights (see Flyweight) with nointrinsic state, only behavior.


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

The Flyweight pattern explains when and how State objects can be shared.

Flyweight ํŒจํ„ด์€ ์–ธ์ œ ์–ด๋–ป๊ฒŒ ์ƒํƒœ ๊ฐ์ฒด๋“ค์ด ๊ณต์œ ๋˜๋Š”์ง€ ์„ค๋ช…ํ•ด์ค€๋‹ค.

State objects are often Singletons.

์ƒํƒœ ๊ฐ์ฒด๋“ค์€ ๋Œ€๋ถ€๋ถ„ ์‹ฑ๊ธ€ํ„ด์ด๋‹ค.



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

  • State objects are often Singletons.
  • Flyweight explains when and how State objects can be shared.
  • Interpreter can use State to define parsing contexts.
  • Strategy has 2 different implementations, the first is similar to State. The difference is in binding times (Strategy is a bind-once pattern, whereas State is more dynamic).
  • The structure of State and Bridge are identical (except that Bridge admits hierarchies of envelope classes, whereas State allows only one). The two patterns use the same structure to solve different problems: State allows an object's behavior to change along with its state, while Bridge's intent is to decouple an abstraction from its implementation so that the two can vary independently.
  • The implementation of the State pattern builds on the Strategy pattern. The difference between State and Strategy is in the intent. With Strategy, the choice of algorithm is fairly stable. With State, a change in the state of the "context" object causes it to select from its "palette" of Strategy objects.

 

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

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


 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.

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

 

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

 

์šฐ์„  ์‰ฌ์šด ์ดํ•ด๋ฅผ ์œ„ํ•ด์„œ ์•„๋ž˜ ์˜ˆ์ œ๋ถ€ํ„ฐ ๋ณด์ž.

Example

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

 

 

Strategy Pattern Structure

 

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

Define a family of algorithms, encapsulate each one, and make them interchangeable.
Strategy lets the algorithm vary independently from clients that use it.


์ „๋žต ํŒจํ„ด์€ ๊ต์ฒด๊ฐ€ ๊ฐ€๋Šฅํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ทธ๋ฃน์„ ์ •์˜ํ•˜๊ณ  ๊ฐ๊ฐ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์บก์Šํ™”ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ „๋žต์€ ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋…๋ฆฝ์ ์ด๊ณ  ๋‹ค์–‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค.



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

 

Suppose a Composition class is responsible for maintaining and updating the linebreaks of text displayed in a text viewer. Linebreaking strategies aren't implemented by the class Composition. Instead, they are implemented separately by subclasses of the abstract Compositor class. Compositor subclasses implement different strategies:

ยท SimpleCompositor implements a simple strategy that determines linebreaks one at a time.
ยทโ€‹ TeXCompositor implements the TeX algorithm for finding linebreaks. This strategy tries to optimize linebreaks globally, that is, one paragraph at a time.
ยท ArrayCompositor implements a strategy that selects breaks so that each row has a fixed number of items. It's useful for breaking a collection of icons into rows, for example. 

 

A Composition maintains a reference to a Compositor object. Whenever a Composition reformats its text, it forwards this responsibility to its Compositor object. The client of Composition specifies which Compositor should be used by installing the Compositor it desires into the Composition.

 

 

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

ยท many related classes differ only in their behavior. Strategies provide a way to configure a class with one of many behaviors.

์—ฐ๊ด€๋œ ๋งŽ์€ ํด๋ž˜์Šค๋“ค์ด ํ–‰ํ•˜๋Š” ํ–‰์œ„์— ์žˆ์–ด์„œ๋งŒ ๋‹ค๋ฅผ ๋•Œ


ยท you need different variants of an algorithm. For example, you might define algorithms  reflecting different space/time trade-offs.Strategies can be used when these variants are  implemented as a class hierarchy of algorithms.

ํด๋ž˜์Šค๊ณ„์ธต์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋‹ค์–‘ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ํ•„์š”ํ•  ๋•Œ ์ „๋žตํŒจํ„ด์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


ยท an algorithm uses data that clients shouldn't know about. Use the Strategy pattern to avoid exposing complex, algorithm-specific data structures.

๋ณต์žกํ•˜๊ณ  ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ํŠนํ™”๋œ ๋ฐ์ดํƒ€๊ตฌ์กฐ๋ฅผ ๋…ธ์ถœ์‹œํ‚ค๊ณ  ์‹ถ์ง€ ์•Š์œผ๋ฉด ์ „๋žตํŒจํ„ด์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


ยท a class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditionals, move related conditional branches into their own Strategy class.

ํ•œ ํด๋ž˜์Šค๊ฐ€ ์—ฌ๋Ÿฌ ํ–‰์œ„๋ฅผ ์ •์˜ํ•˜๊ณ  ์ˆ˜๋งŽ์€ ์กฐ๊ฑด์ ˆ์— ์˜ํ•ด ํŠน์ • ํ–‰์œ„๋ฅผ ํ•ด์•ผํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค. ์ด๋Ÿด ๋•Œ๋Š” ๋งŽ์€ ์กฐ๊ฑด์ ˆ๋Œ€์‹ ์— ์—ฐ๊ด€์žˆ๋Š” ์กฐ๊ฑด์ ˆ๋“ค์„ ์ „๋žต ํด๋ž˜์Šค๋กœ ๋ฌถ์–ด๋ฒ„๋ ค๋ผ. 

 

 

๋“ฑ์žฅ ์ธ๋ฌผ ( Participants ) :
ยท Strategy (Compositor)
o declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.

์ง€์›๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ณตํ†ต ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•œ๋‹ค. Context๊ฐ€ ConcreteStrategy์— ์˜ํ•ด ์ •์˜๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•œ๋‹ค. 

 

ยท ConcreteStrategy (SimpleCompositor, TeXCompositor,ArrayCompositor)  

o implements the algorithm using the Strategy interface.

Strategy ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•ด์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•œ๋‹ค.


ยท Context (Composition)
o is configured with a ConcreteStrategy object.

o maintains a reference to a Strategy object.  

Strategy๊ฐ์ฒด๋กœ์˜ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์œ ์ง€ํ•œ๋‹ค.
o may define an interface that lets Strategy access its data.  

Strategy๊ฐ€ Context์˜ ๋ฐ์ดํƒ€๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

์›๋ฆฌ ( Collaborations ) :

ยท Strategy and Context interact to implement the chosen algorithm. A context may pass all data required by the algorithm to the strategy when the algorithm is called. Alternatively, the context can pass itself as an argument to Strategy operations. That lets the strategy call back on the context as required.
ยท A context forwards requests from its clients to its strategy. Clients usually create and pass a ConcreteStrategy object to the context; thereafter, clients interact with the context exclusively. There is often a family of ConcreteStrategy classes for a client to choose from.


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

1. Families of related algorithms. Hierarchies of Strategy classes define a family of algorithms  or behaviors for contexts to reuse. Inheritance can help factor out common functionality of the algorithms.

์ „๋žตํŒจํ„ด์€ ์—ฐ๊ด€๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ทธ๋ฃน์„ ์ƒ์†์„ ํ†ตํ•ด ํด๋ž˜์Šค๊ณ„์ธต์œผ๋กœ ๋งŒ๋“ค์–ด ์ด๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.  

 

2. An alternative to subclassing. Inheritance offers another way to support a variety of algorithms or behaviors. You can subclass a Context class directly to give it different behaviors. But this hard-wires the behavior into Context. It mixes the algorithm implementation with Context's, making Context harder to understand, maintain, and extend. And you can't vary the algorithm dynamically. You wind up with many related classes whose only difference is the algorithm or behavior they employ. Encapsulating the algorithm in separate Strategy classes lets you vary the algorithm independently of its context, making it easier to switch, understand, and extend.


3. Strategies eliminate conditional statements. The Strategy pattern offers an alternative to conditional statements for selecting desired behavior. When different behaviors are lumped into one class, it's hard to avoid using conditional statements to select the right behavior. Encapsulating the behavior in separate Strategy classes eliminates these conditional statements.
Code containing many conditional statements often indicatesthe need to apply the Strategy pattern.


4. A choice of implementations. Strategies can provide different implementations of the  same behavior. The client can choose among strategies with different time and space trade-offs.


5. Clients must be aware of different Strategies.The pattern has a potential drawback in that a client must understand how Strategies differ before it can select the appropriate one. Clients might be exposed to implementation issues. Therefore you should use the Strategy pattern only when the variation in behavior is relevant to clients.


6. Communication overhead between Strategy and Context. The Strategy interface is shared by all ConcreteStrategy classes whether the algorithms they implement are trivial or complex. Hence it's likely that some ConcreteStrategies won't use all the information passed to them through this interface; simple ConcreteStrategies may use none of it! That means there will be times when the context creates and initializes parameters that never get used. If this is an issue, then you'll need tighter coupling between Strategy and Context.


7. Increased number of objects. Strategies increase the number of objects in an application. Sometimes you can reduce this overhead by implementing strategies as stateless objects that contexts can share. Any residual state is maintained by the context, which passes it in each request to the Strategy object. Shared strategies should not maintain state across invocations. The Flyweight pattern describes this approach in more detail.


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

Flyweight

 

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

  • Strategy is like Template Method except in its granularity.
    Strategy ํŒจํ„ด์€ Template Method ํŒจํ„ด๊ณผ ๋น„์Šทํ•˜๋‹ค. ( granularity๋ฅผ ์ œ์™ธํ•˜๋ฉด )
    ( granularity๋ฅผ ํ…Œํฌ๋†€๋กœ์ง€์ปฌํ•˜๊ฒŒ ๋ญ๋ผ๊ณ  ํ•ด์„ํ•ด์•ผํ•  ์ง€ ๋ชจ๋ฅด๊ฒ ๋„ค์š”.)

  • State is like Strategy except in its intent.
    State ํŒจํ„ด์€ Strategy ํŒจํ„ด๊ณผ ๋น„์Šทํ•˜๋‹ค. ( intent๋ฅผ ์ œ์™ธํ•˜๋ฉด )
  • Strategy lets you change the guts of an object. Decorator lets you change the skin.
    Strategy ํŒจํ„ด์„ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด๋‚ด๋ถ€๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, Decorator ํŒจํ„ด์€ ๊ฐ์ฒด๊ป๋ฐ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.
  • State, Strategy, Bridge (and to some degree Adapter) have similar solution structures. They all share elements of the 'handle/body' idiom. They differ in intent - that is, they solve different problems.
    State, Strategy, Bridge ํŒจํ„ด๋“ค์€ ๋ชจ๋‘ ๋น„์Šทํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ์ง€๋งŒ, ํŒจํ„ด์˜ ๋ชฉ์ (intent)๊ฐ€ ๋‹ค๋ฅด๋‹ค. ์ฆ‰, ์„œ๋กœ ๋‹ค๋ฅธ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‚˜์˜จ ํŒจํ„ด๋“ค์ด๋ผ๋Š” ์–˜๊ธฐ๋‹ค.
  • Strategy has 2 different implementations, the first is similar to State. The difference is in binding times (Strategy is a bind-once pattern, whereas State is more dynamic).
    Strategy ํŒจํ„ด์ด ์ •์ ์œผ๋กœ ํ•œ๋ฒˆ ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๋ฐ˜๋ฉด State ํŒจํ„ด์€ ๋™์ ์œผ๋กœ ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.
  • Strategy objects often make good Flyweights.
    Strategy ๊ฐ์ฒด๋“ค์€ ๋ณดํ†ต ์ข‹์€ Filyweights๋ฅผ ๋งŒ๋“ ๋‹ค. ( Flyweight ํŒจํ„ด์„ ๊ณต๋ถ€ํ•˜๋ฉด ๋ฌด์Šจ ๋ง์ธ์ง€ ์ดํ•ด๊ฐ€ ๋  ๋“ฏ ์‹ถ๋„ค์š” )

 

 

 

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

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