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 )