SlideShare a Scribd company logo
1 of 16
Download to read offline
Memento Pattern 
객체의 상태정보를 저장 및 복원하기위해 사용하는 패턴. 
NHN NEXT 
김우진
Memento? 
[noun] an object or item that serves to remind one of a person.
남자가 문신을 한 이유?
남자가 문신을 한 이유? 
바로 직전(기억을 잃기 전)의 상태로 
돌아가기 위해.
문신이 과거의 상태를 저장하는 
하나의 메멘토라고 할 수 있다.
UML Diagram 
Originator 
• 상태를 가지고 있는 클래스 
• 현재의 상태를 가지는 메멘토 인스턴스 생성 
• 메멘토 인스턴스를 사용하여 상태를 이전으로 돌림 
Memento 
Caretaker 
• Memento를 관리하고 유지 
• Memento의 내부엔 접근불가 
• 한 시점의 상태를 저장하는 클래스 
• Originator만 내부에 접근가능 함
UML Diagram 
Originator = 남자(Man) 
• 상태를 가지고 있는 클래스 
• 현재의 상태를 가지는 메멘토 인스턴스 생성 
• 메멘토 인스턴스를 사용하여 상태를 이전으로 돌림 
Caretaker = 신 또는 작가(God) 
• Memento를 관리하고 유지 
• Memento의 내부엔 접근불가 
Memento = 문신(Tattoo) 
• 한 시점의 상태를 저장하는 클래스 
• Originator만 내부에 접근가능 함
! 
Example
God Class(Caretaker) 
public class God { 
public static void main(String[] args) { 
List<Tattoo> tattooList = new ArrayList<Tattoo>(); 
Man man = new Man("Leonard", 35); 
! 
man.introduceMyself(); // My name is Leonard and I'm 35 years old 
man.resetMemory(); // 10분에 한번씩 호출하는 병에 걸림 
man.introduceMyself(); // My name is null and I'm null years old 
man.know("Leonard", 35); 
tattooList.add(man.tattoo("Leonard", 35)); 
! 
// 10분 후 
man.resetMemory(); 
man.introduceMyself(); // My name is null and I'm null years old 
man.seeTattoo(tattooList.get(0)); 
man.introduceMyself(); // My name is Leonard and I'm 35 years old 
} 
}
Man Class(Originator) 
public class Man { 
String name; 
Integer age; 
! 
public Man(String name, Integer age) { 
this.name = name; 
this.age = age; 
} 
public void know(String name, int age) { 
this.name = name; 
this.age = age; 
} 
! 
public void resetMemory() { 
this.name = null; 
this.age = null; 
} 
! 
public Tattoo tattoo(String name, int age) { 
Tattoo tattoo = new Tattoo(name, age); 
return tattoo; 
} 
public void introduceMyself() { 
System.out.println("My name is " + name + 
" and I'm " + age + " years old"); 
} 
public void seeTattoo(Tattoo tattoo) { 
this.name = tattoo.getName(); 
this.age = tattoo.getAge(); 
} 
}
Tattoo Class 
public static class Tattoo { 
private final String name; 
private final int age; 
public Tattoo(String name, int age) { 
this.name = name; 
this.age = age; 
} 
! 
public Integer getAge() { 
return age; 
} 
! 
public String getName() { 
return name; 
} 
}
Real World Example - Database Transaction 
Memento Pattern을 사용하여 Rollback을 수행함.
Memento Pattern이 없었다면… 
• Man Class가 과거의 상태를 다 가지고 있게돼서 코드가 
복잡해졌을 것이다. 
• 클래스가 하는 일이 많아져서 통제가 힘들어졌을 것이다. 
• 상태를 저장하는 공간과 사용하는 공간이 같아서 안정성에 
문제가 생겼을 것이다.
Memento Pattern이 있어서… 
• Man(Originator)과 Tattoo(Memento)를 저장하는 공간이 
분리되어 있어서 안전하게 저장 할 수 있다. 
• 캡슐화를 위배하지 않고 내부 상태를 저장 할 수 있다. 
• God(Caretaker)가 Tattoo(Memento)에 신경 안쓰고 저장 
하고 복원 할 수 있다.
Command Pattern 
• Memento Pattern과 함께 사용 될 수 있다. 
• 다음 주에 함께 알아 보겠습니다.
Q & A

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Memento pattern

  • 1. Memento Pattern 객체의 상태정보를 저장 및 복원하기위해 사용하는 패턴. NHN NEXT 김우진
  • 2. Memento? [noun] an object or item that serves to remind one of a person.
  • 4. 남자가 문신을 한 이유? 바로 직전(기억을 잃기 전)의 상태로 돌아가기 위해.
  • 5. 문신이 과거의 상태를 저장하는 하나의 메멘토라고 할 수 있다.
  • 6. UML Diagram Originator • 상태를 가지고 있는 클래스 • 현재의 상태를 가지는 메멘토 인스턴스 생성 • 메멘토 인스턴스를 사용하여 상태를 이전으로 돌림 Memento Caretaker • Memento를 관리하고 유지 • Memento의 내부엔 접근불가 • 한 시점의 상태를 저장하는 클래스 • Originator만 내부에 접근가능 함
  • 7. UML Diagram Originator = 남자(Man) • 상태를 가지고 있는 클래스 • 현재의 상태를 가지는 메멘토 인스턴스 생성 • 메멘토 인스턴스를 사용하여 상태를 이전으로 돌림 Caretaker = 신 또는 작가(God) • Memento를 관리하고 유지 • Memento의 내부엔 접근불가 Memento = 문신(Tattoo) • 한 시점의 상태를 저장하는 클래스 • Originator만 내부에 접근가능 함
  • 9. God Class(Caretaker) public class God { public static void main(String[] args) { List<Tattoo> tattooList = new ArrayList<Tattoo>(); Man man = new Man("Leonard", 35); ! man.introduceMyself(); // My name is Leonard and I'm 35 years old man.resetMemory(); // 10분에 한번씩 호출하는 병에 걸림 man.introduceMyself(); // My name is null and I'm null years old man.know("Leonard", 35); tattooList.add(man.tattoo("Leonard", 35)); ! // 10분 후 man.resetMemory(); man.introduceMyself(); // My name is null and I'm null years old man.seeTattoo(tattooList.get(0)); man.introduceMyself(); // My name is Leonard and I'm 35 years old } }
  • 10. Man Class(Originator) public class Man { String name; Integer age; ! public Man(String name, Integer age) { this.name = name; this.age = age; } public void know(String name, int age) { this.name = name; this.age = age; } ! public void resetMemory() { this.name = null; this.age = null; } ! public Tattoo tattoo(String name, int age) { Tattoo tattoo = new Tattoo(name, age); return tattoo; } public void introduceMyself() { System.out.println("My name is " + name + " and I'm " + age + " years old"); } public void seeTattoo(Tattoo tattoo) { this.name = tattoo.getName(); this.age = tattoo.getAge(); } }
  • 11. Tattoo Class public static class Tattoo { private final String name; private final int age; public Tattoo(String name, int age) { this.name = name; this.age = age; } ! public Integer getAge() { return age; } ! public String getName() { return name; } }
  • 12. Real World Example - Database Transaction Memento Pattern을 사용하여 Rollback을 수행함.
  • 13. Memento Pattern이 없었다면… • Man Class가 과거의 상태를 다 가지고 있게돼서 코드가 복잡해졌을 것이다. • 클래스가 하는 일이 많아져서 통제가 힘들어졌을 것이다. • 상태를 저장하는 공간과 사용하는 공간이 같아서 안정성에 문제가 생겼을 것이다.
  • 14. Memento Pattern이 있어서… • Man(Originator)과 Tattoo(Memento)를 저장하는 공간이 분리되어 있어서 안전하게 저장 할 수 있다. • 캡슐화를 위배하지 않고 내부 상태를 저장 할 수 있다. • God(Caretaker)가 Tattoo(Memento)에 신경 안쓰고 저장 하고 복원 할 수 있다.
  • 15. Command Pattern • Memento Pattern과 함께 사용 될 수 있다. • 다음 주에 함께 알아 보겠습니다.
  • 16. Q & A