A A
[Design Pattern] Memento Pattern - ๋ฉ”๋ฉ˜ํ†  ํŒจํ„ด

Memento Pattern

Memento ํŒจํ„ด์€ ๊ฐ์ฒด์˜ ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ณต์›ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๋””์ž์ธ ํŒจํ„ด์ž…๋‹ˆ๋‹ค.
  • ์ด ํŒจํ„ด์€ ๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ์ƒํƒœ๋ฅผ ์บก์Аํ™”ํ•˜์—ฌ ์™ธ๋ถ€์— ๋…ธ์ถœ์‹œํ‚ค์ง€ ์•Š๊ณ ๋„ ๊ฐ์ฒด์˜ ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ณต์›ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.
์ผ๋ฐ˜์ ์œผ๋กœ ์–ด๋–ค Object์˜ ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์œ ์šฉํ•œ ํŒจํ„ด์ž…๋‹ˆ๋‹ค. → ๊ฒŒ์ž„์—์„œ Saveํ•˜๋Š” ๊ฒƒ์„ ๋– ์˜ฌ๋ฆฌ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • ๊ฐ€๋ น ํ…์ŠคํŠธ ์—๋””ํ„ฐ ์‚ฌ์šฉํ•  ๋•Œ undo์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ์‚ญ์ œ ํ•˜๊ธฐ ์ „ ์ƒํƒœ๋กœ ํ…์ŠคํŠธ ๋ณต์›์ด ๊ฐ€๋Šฅํ•˜๊ณ , ๊ทธ ๊ธฐ๋Šฅ์„ ๋ช‡ ๋‹จ๊ณ„๋กœ ์‹คํ–‰ํ•ด์„œ ํŽธ์ง‘์ž‘์—…์„ ์ ์  ๊ณผ๊ฑฐ๋กœ ๋Œ๋ฆฌ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•จ
  • ๊ฐ์ฒด์ง€ํ–ฅํ”„๋กœ๊ทธ๋žจ์—์„œ undo๊ธฐ๋Šฅ์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ •๋ณด๋ฅผ ์ €์žฅํ•ด ๋‘˜ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ๋‹จ, ์ €์žฅ๋งŒ ํ•ด์„œ๋Š” ์•ˆ๋˜๊ณ , ์ €์žฅํ•œ ์ •๋ณด๋กœ๋ถ€ํ„ฐ ์ธ์Šคํ„ด์Šค๋ฅผ ์›๋Œ€๋ž˜๋กœ ๋˜๋Œ๋ฆด ์ˆ˜ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • Mememto Pattern์„ ์‚ฌ์šฉํ•˜๋ฉด ์–ด๋–ค ์‹œ์ ์˜ ์ธ์Šคํ„ด์Šค์˜ ์ƒํƒœ๋ฅผ ํ™•์‹คํ•˜๊ฒŒ ๊ธฐ๋กํ•ด์„œ ์ €์žฅํ•ด ๋‘๋ฉด ๋‚˜์ค‘์— ์ธ์Šคํ„ด์Šค๋ฅผ ๊ทธ ์‹œ์ ์˜ ์ƒํƒœ ๋˜๋Œ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • Originator: ์ž‘์„ฑ์ž ์—ญํ• , ์ž์‹ ์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ Memento๋ฅผ ๋งŒ๋“ค์–ด ์ €์žฅํ•˜๊ณ  ๋ถˆ๋Ÿฌ์˜ค๊ธฐ๋„ ํ•จ
  • Memento: Originator ์—ญํ• ์˜ ๋‚ด๋ถ€ ์ •๋ณด๋ฅผ ์ •๋ฆฌ ๋ณด๊ด€ํ•ฉ๋‹ˆ๋‹ค.
  • Caretaker: ๋ฏธ๋ž˜์˜ ํ•„์š”์— ๋Œ€๋น„ํ•˜์—ฌ Memento ์—ญํ• ์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

Object

  • Object๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” State๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. (Memento์— ๋“ค์–ด๊ฐ€์•ผ ํ•  ๋‚ด์šฉ)
  • createMemento(): State์— ๋Œ€ํ•œ memento๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • restore(): Memento Object๋กœ๋ถ€ํ„ฐ State๋ฅผ ๋ณต์›ํ•ด ์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

Memento

  • Object์˜ stae๋ฅผ ๊ทธ๋Œ€๋กœ ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— Object์™€ ๋™์ผํ•œ state๋ฅผ property๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  • Memento๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Memento Pattern Example

class Cat:
    def __init__(self, age, height):
        self.age = age
        self.height = height

    def speak(self):
        print(f'{self.age} year old, {self.height} cm, meow')

    def createMemento(self):
        cat_memento = CatMemento(self.age, self.height)
        return cat_memento

    def restore(self, memento):
        self.age = memento.age
        self.height = memento.height
  • Cat ํด๋ž˜์Šค
    • __init__: ๋‚ด๋ถ€ ์ƒํƒœ๋กœ age์™€ height๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค.
    • speak(): ํ˜„์žฌ age์™€ height๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
    • createMemento(): ํ˜„์žฌ Cat์˜ ์ƒํƒœ๋ฅผ Memento ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
    • restore(): ์ „๋‹ฌ๋ฐ›์€ Memento ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Cat์˜ ์ƒํƒœ๋ฅผ ๋ณต์›ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

 

import uuid
from datetime import datetime

class CatMemento:
    def __init__(self, age, height):
        self.uuid = uuid.uuid4()
        self.created_time = datetime.now()
        self.age = age
        self.height = height
  • CatMemento ํด๋ž˜์Šค
    • __init__: Memento ๊ฐ์ฒด๋กœ Cat์˜ age์™€ height๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ƒ์„ฑ ์‹œ๊ฐ„(created_time)๊ณผ ๊ณ ์œ  ID(uuid)๋ฅผ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค.
cat_history = []
  • cat_history: Memento ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค.
cat = Cat(0, 10)
cat_history.append(cat.createMemento())
  • Cat ๊ฐ์ฒด ์ƒ์„ฑ: age=0, height=10์ธ Cat ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • Cat์˜ state๋ฅผ ๊ธฐ์–ตํ•˜๊ธฐ ์œ„ํ•ด createMememto()๋ฅผ ํ†ตํ•ด Memento๋ฅผ ์ƒ์„ฑ, cat_hisotry

cat.age = 1
cat.height = 25
cat_history.append(cat.createMemento())
  • Age=1, height=25๊ฐ€ ๋œ ์ƒํƒœ์—์„œ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
  • ์ด ์ •๋ณด๋ฅผ cat_history์— ๋„ฃ์–ด์ฃผ๊ธฐ ์œ„ํ•ด createMemento()๋ฅผ ํ†ตํ•ด Memento๋ฅผ ์ƒ์„ฑํ•ด์„œ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

cat.age = 2
cat.height = 50
cat_history.append(cat.createMemento())
  • Age=2, height=50๊ฐ€ ๋œ ์ƒํƒœ์—์„œ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
  • ์ด ์ •๋ณด๋ฅผ cat_history์— ๋„ฃ์–ด์ฃผ๊ธฐ ์œ„ํ•ด createMemento()๋ฅผ ํ†ตํ•ด Memento๋ฅผ ์ƒ์„ฑํ•ด์„œ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

cat.restore(cat_history[0])
  • cat_history์˜ 0๋ฒˆ์งธ ๊ณผ๊ฑฐ ์ •๋ณด๋กœ ๋ณต๊ตฌํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด restore()์„ ํ˜ธ์ถœํ•˜์—ฌ ๊ทธ argument๋กœ cat_history์˜ 0๋ฒˆ์งธ memento Object๋ฅผ ๋„˜๊ฒจ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

cat.speak() -> # 0year old, 10cm, meow
  • Cat์˜ speak() ํ˜ธ์ถœํ•˜๋ฉด 0๋ฒˆ์จฐ memento๋กœ state ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.
Object์˜ state๋ฅผ ๊ธฐ์–ตํ•˜๊ธฐ ์œ„ํ•ด Memento ์˜ค๋ธŒ์ ํŠธ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ ์˜ค๋ธŒ์ ํŠธ๋“ค์„ ํžˆ์Šคํ† ๋ฆฌ ๋ฆฌ์ŠคํŠธ์—์„œ ๊ด€๋ฆฌํ•˜๋Š” Pattern ์ž…๋‹ˆ๋‹ค.