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 )