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 ) :
ํจํด ์ฌ์ฉ์ ์ฅ๋จ์ ( 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.
์ถ๊ฐ ์ ๋ณด :
์ถ์ฒ : http://sourcemaking.com/design_patterns/template_method
Design Patterns : Element of Reusable Object Oriented Software ( by GoF, 1994 )
[๋์์ธํจํด] ํ๋กํ ํ์ ํจํด ( Prototype Pattern ) (0) | 2015.11.03 |
---|---|
[๋์์ธํจํด] ํฉํ ๋ฆฌ ๋ฉ์๋ ํจํด ( Factory Method Pattern ) (2) | 2015.11.03 |
[๋์์ธํจํด] ์ถ์ ํฉํ ๋ฆฌ ํจํด ( Abstract Factory Pattern ) (0) | 2015.11.03 |
[๋์์ธํจํด] ์ํ ํจํด ( State Pattern ) (0) | 2015.11.03 |
[๋์์ธ ํจํด] ์ ๋ต ํจํด ( Strategy Pattern ) (0) | 2015.11.03 |
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.
์ํ ๊ฐ์ฒด๋ค์ ๋๋ถ๋ถ ์ฑ๊ธํด์ด๋ค.
์ถ๊ฐ ์ ๋ณด :
์ถ์ฒ : 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 ํจํด์ ์ํ๋ค.
์ฐ์ ์ฌ์ด ์ดํด๋ฅผ ์ํด์ ์๋ ์์ ๋ถํฐ ๋ณด์.
๊ณตํญ์ผ๋ก ๊ฐ๋ ๊ตํต์๋จ์ด ์ธ๊ฐ์ง๊ฐ ์๋ค. ๋ฒ์ค, ์๋์ฐจ, ๊ทธ๋ฆฌ๊ณ ํ์. ์ฐ๋ฆฌ๊ฐ ์ด๋ค ๊ฒ์ ํ๊ณ ๊ฐ ์ง๋ ๊ฐ๊ฒฉ, ์ด๋ํ๋๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ, ๋ฐฐ์ฐจ์๊ฐ, ๊ธฐ๋ฆ๊ฐ ๋ฑ๋ฑ์ด ๋ ๊ฒ์ด๋ค. ์ด๋ฐ๊ฒ์ ๋ก์ง์ผ๋ก ๊ตฌํํ๋ค๊ณ ์๊ฐํด๋ณด์. ์ ๋ตํจํด์ ๊ณต๋ถํ๋ ์๊ฐ์ด๋ ์ ๋ต์ด๋ ๋ง์ ์จ์ ๊ฐ๊ฐ์ ๊ตํต์๋จ์ ํ๋์ ์ ๋ต์ด๋ผ๊ณ ํ ์ ์์ ๊ฒ์ด๋ค. ๊ทธ๋ฆฌ๊ณ ๊ฐ ์ ๋ต์ ๊ณตํต๋ถ๋ชจ๋ฅผ ๋ชจ์์ 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 ) :
ํจํด ์ฌ์ฉ์ ์ฅ๋จ์ ( 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
์ถ๊ฐ ์ ๋ณด :
์ถ์ฒ : http://sourcemaking.com/design_patterns/strategy
Design Patterns : Element of Reusable Object Oriented Software ( by GoF, 1994 )