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.
์ด ํจํด๋ค์ ๊ฐ์ฒด๋ค ์ฌ์ด์ ์ปค๋ฎค๋์ผ์ด์ ์ ๊ด์ฌ์ ๊ฐ์ง๋ค.
์ค๋ ์ดํด๋ณผ Memento ํจํด์ Behavioral ํจํด์ ์ํฉ๋๋ค.
Memento Pattern Structure
ํจํด์ ๋ชฉ์ ( Intent ) :
Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.
ํจํด์ด ๋์ค๊ฒ ๋ ๋๊ธฐ ( Motivation ) :
๋ค์ด์ด๊ทธ๋จ์ ๊ทธ๋ฆฌ๋ ํด์ ์๋ก ๋ค์ด๋ณด๊ฒ ์ต๋๋ค. ์ฌ๊ฐํ ๋๊ฐ๋ฅผ ๊ทธ๋ฆฌ๊ณ ๋ ์ฌ๊ฐํ์ ์ ์ผ๋ก ์ฐ๊ฒฐํฉ๋๋ค. ์ ์ด์ ์๋์ชฝ ์ฌ๊ฐํ์ ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋์ํต๋๋ค. ๊ทธ๋ฌ๋ฉด ์๋ ๊ทธ๋ฆผ์ฒ๋ผ ๋ ์ฌ๊ฐํ์ ์ด์ ์ ๋ ๋ฐ๋ผ์ ์ด๋ํ๋ฉด์ ๋ ์ฌ๊ฐํ์ ๊ณ์ ์ด์ด์ค๋๋ค.
์ด๋ ๊ฒ ๋ ๊ฐ์ฒด ์ฌ์ด์ ์ฐ๊ฒฐ ๊ด๊ณ๋ฅผ ์ ์ง์ํค๋ ์ ์๋ ค์ง ๋ฐฉ๋ฒ์ค ํ๋๋ ์ ์ฝ-ํด๊ฒฐ ์์คํ (constraint-solving system)์ ๋๋ค. ์ด ์์คํ ์ ConstraintSolver ์ ์บก์ํ๋์ด ์์ต๋๋ค. ConstraintSolver๋ ๊ฐ์ฒด๋ค ์ฌ์ด์ ์ฐ๊ฒฐ์ ๊ธฐ๋กํ๊ณ ์ด ์ฐ๊ฒฐ์ ์ค๋ช ํ๋ ์ํ์์ ์์ฑํฉ๋๋ค. ๊ทธ๋์ ๊ฐ์ฒด์ ์์น๊ฐ ๋ณ๊ฒฝ์ด ๋๋ฉด ์ด ์ํ์์ ๋ค์ ๊ณ์ฐํ์ฌ ์ ์ ์ด๋ป๊ฒ ๊ทธ๋ ค์ผ ํ๋์ง๋ฅผ ์ ์ ์๋ ๊ฒ์ ๋๋ค.
ํ ์ง๋ง ๋๋๋ฆฌ๊ธฐ ๊ธฐ๋ฅ์ ์๊ฐ์ฒ๋ผ ์ฝ์ง๊ฐ ์์ต๋๋ค. ๋ง์ฝ ์ฌ๊ฐํ์ด ์์ง์ธ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐํ์ฌ ๊ทธ๋งํผ ๋ค์ ๋๋๋ฆฐ๋ค๊ณ ํ์ ๋ ๊ณผ์ฐ ๋ ์ฌ๊ฐํ์ ์ฐ๊ฒฐํ๋ ์ ์ ์ด๋ป๊ฒ ์์ง์ฌ์ผ ํ๋๊ฐ๋ฅผ ์๊ฐํด์ผ ํฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ์๋์ ๊ฐ์ด ๋ถ์์ ํ ๋๋๋ฆฌ๊ธฐ๊ฐ ๋์ด๋ฒ๋ฆด ํ ๋๊น์.
์ผ ๋ฐ์ ์ผ๋ก ConstraintSolver์ public interface ๋ undo๊ธฐ๋ฅ์ ์์ ํ ๊ตฌํํ๊ธฐ์ ๋ถ์ถฉ๋ถํฉ๋๋ค. undo ๋ฉ์นด๋์ฆ์ ์ด์ ์ ์ํ๋ฅผ ๋ค์ ๋ถ๋ฌ์ค๊ธฐ ์ํด์ ConstraintSolver์ ์ข ๋ ๋ฐ์ ํ ๊ด๊ณ๋ฅผ ๋งบ์ด์ผ ํ์ง๋ง ํํธ์ผ๋ก๋ ConstraintSolver์ ๋ด๋ถ๋ฅผ ๋ ธ์ถ์ํค๋ฉด ์๋ฉ๋๋ค. ์ด ๋ฌธ์ ๋ฅผ ํ๊ธฐ์ํด์ ๋์จ ํจํด์ด ๋ฐ๋ก ๋ฉ๋ฉํ ํจํด์ ๋๋ค. ๋ฉ๋ฉํ ๋ ๋ค๋ฅธ ๊ฐ์ฒด(๋ฉ๋ฉํ ์ originator)์ ๋ด๋ถ ์ํ๋ฅผ ์ ์ฅํ๋ ๊ฐ์ฒด์ ๋๋ค. undo ๋ฉํ๋์ฆ์ originator์ ์ด์ ์ํ๊ฐ ํ์ํ ๋ ๋ฉ๋ฉํ ๋ฅผ ์์ฒญํ๊ฒ ๋ฉ๋๋ค. originator๋ ์์ ์ ํ์ฌ ์ํ๋ฅผ ๋ฉ๋ฉํ ์ ์ ์ฅํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ค๋ก์ง originator๋ง์ด ๋ฉ๋ฉํ ์ ์ ์ฅํ ์์ ์ ์ํ๋ฅผ ์ ์ฅ ๋๋ ์ฝ์ด์ฌ ์๊ฐ ์์ต๋๋ค โ ๋ฉ๋ฉํ ๋ ๋ค๋ฅธ ๊ฐ์ฒด๋ค์๊ฒ "์คํํฌ(์นจํฌํ ์ ์๋ ๊ฒ)"๋ผ๊ณ ํ ์ ์์ฃ . ์์์ ์๋ก ๋ ํด์์ ConstraintSolver๋ originator์ ์ญํ ์ ํ๋๊ฑฐ์ฃ .
The following sequence of events characterizes theundo process:
The editor requests a memento from the ConstraintSolver as aside-effect of the move operation.
The ConstraintSolver creates and returns a memento, an instance of a class SolverState in this case. A SolverState memento contains data structures that describe the current state of the ConstraintSolver's internal equations and variables.
Later when the user undoes the move operation, the editor gives the SolverState back to the ConstraintSolver.
Based on the information in the SolverState, the ConstraintSolver changes its internal structures to return its equations and variables to their exact previous state.
This arrangement lets the ConstraintSolver entrust other objects with the information it needs to revert to a previous state without exposing its internal structure and representations.
์ ์ฉ์ฑ ( Applicability ) :
Use the Memento pattern when
a snapshot of (some portion of) an object's state must be saved so that it can be restored to that state later, and
a direct interface to obtaining the state would expose implementation details and break the object's encapsulation.
๋ฑ์ฅ ์ธ๋ฌผ ( Participants ) :
โข Memento (SolverState)
o stores
internal state of the Originator object. The memento may store as much
or as little of the originator's internal state as necessary at its
originator's discretion.
o protects against access by objects other than the originator. Mementos have effectively two interfaces. Caretaker sees a narrow
interface to the Mementoโit can only pass the memento to other objects.
Originator, in contrast, sees a wide interface, one that lets it
access all the data necessary to restore itself to its previous state.
Ideally, only the originator that produced the memento would be
permitted to access the memento's internal state.
โข Originator (ConstraintSolver)
o creates a memento containing a snapshot of its current internal state.
o uses the memento to restore its internal state.
โข Caretaker (undo mechanism)
o is responsible for the memento's safekeeping.
o never operates on or examines the contents of a memento.
์๋ฆฌ ( Collaborations ) :
โข A caretaker requests a memento from an originator, holds it for a time, and passes it back to the originator, as the following interaction diagram illustrates:
Sometimes the caretaker won't pass the memento back to the originator, because the originator might never need to revert to an earlier state.
โข Mementos are passive. Only the originator that created a memento will assign or retrieve its state.
ํจํด ์ฌ์ฉ๋ฒ
- Identify the roles of โcaretakerโ and โoriginatorโ.
- Create a Memento class and declare the originator a friend.
- Caretaker knows when to "check point" the originator.
- Originator creates a Memento and copies its state to that Memento.
- Caretaker holds on to (but cannot peek into) the Memento.
- Caretaker knows when to "roll back" the originator.
- Originator reinstates itself using the saved state in the Memento.
ํจํด ์ฌ์ฉ์ ์ฅ๋จ์ ( Consequences ):
The Memento pattern has several consequences:
- Preserving encapsulation boundaries. Memento avoids exposing information
that only an originator should manage but that must be stored nevertheless
outside the originator. The pattern shields other objects from potentially
complex Originator internals, thereby preserving encapsulation boundaries.
- It simplifies Originator. In other encapsulation-preserving designs,
Originator keeps the versions of internal state that clients have requested.
That puts all the storage management burden on Originator. Having
clients manage the state they ask for simplifies Originator and keeps clients
from having to notify originators when they're done.
- Using mementos might be expensive. Mementos might incur considerable
overhead if Originator must copy large amounts of information to store in
the memento or if clients create and return mementos to the originator often enough. Unless encapsulating and restoring Originator state is cheap, the
pattern might not be appropriate.
- Defining narrow and wide interfaces. It may be difficult in some languages
to ensure that only the originator can access the memento's state.
- Hidden costs incaring for mementos. A caretaker is responsible for deleting the mementos it cares for. However, the caretaker has no idea how much state is in the memento. Hence an otherwise lightweight caretaker might incur large storagecosts when it stores mementos.
๊ด๋ จ ํจํด๋ค :
Command : Commands can use mementos to maintain state for undoable operations.
Iterator : Mementos can be used for iteration as described earlier.
์ถ๊ฐ ์ ๋ณด :
- Command and Memento act as magic tokens to be passed around and invoked at a later time. In Command, the token represents a request; in Memento, it represents the internal state of an object at a particular time. Polymorphism is important to Command, but not to Memento because its interface is so narrow that a memento can only be passed as a value.
- Command can use Memento to maintain the state required for an undo operation.
- Memento is often used in conjunction with Iterator. An Iterator can use a Memento to capture the state of an iteration. The Iterator stores the Memento internally.
์ถ์ฒ : http://sourcemaking.com/design_patterns/memento
Design Patterns : Element of Reusable Object Oriented Software ( by GoF, 1994 )
'๐ป Programming > GoF Design Pattern' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋์์ธํจํด] ์ธํฐํ๋ฆฌํฐ ํจํด ( Interpreter Pattern ) (0) | 2015.11.03 |
---|---|
[๋์์ธํจํด] ๊ด์ฐฐ์(์ต์ ๋ฒ) ํจํด ( Observer Pattern ) (0) | 2015.11.03 |
[๋์์ธํจํด] ์ค์ฌ์ ํจํด ( Mediator Pattern ) (0) | 2015.11.03 |
[๋์์ธํจํด] ๋ฐ๋ณต์(์ดํฐ๋ ์ดํฐ) ํจํด ( Iterator Pattern ) (0) | 2015.11.03 |
[๋์์ธํจํด] ์ปค๋งจ๋ ํจํด ( Command Pattern ) (0) | 2015.11.03 |