Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Practical Design Patterns for Java Developers

You're reading from   Practical Design Patterns for Java Developers Hone your software design skills by implementing popular design patterns in Java

Arrow left icon
Product type Paperback
Published in Feb 2023
Publisher Packt
ISBN-13 9781804614679
Length 266 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Miroslav Wengner Miroslav Wengner
Author Profile Icon Miroslav Wengner
Miroslav Wengner
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Part 1: Design Patterns and Java Platform Functionalities
2. Chapter 1: Getting into Software Design Patterns FREE CHAPTER 3. Chapter 2: Discovering the Java Platform for Design Patterns 4. Part 2: Implementing Standard Design Patterns Using Java Programming
5. Chapter 3: Working with Creational Design Patterns 6. Chapter 4: Applying Structural Design Patterns 7. Chapter 5: Behavioral Design Patterns 8. Part 3: Other Essential Patterns and Anti-Patterns
9. Chapter 6: Concurrency Design Patterns 10. Chapter 7: Understanding Common Anti-Patterns 11. Assessments 12. Index 13. Other Books You May Enjoy

Restoring the desired state with the memento pattern

Sometimes, it may be useful to consider keeping minimal information about the state of an object in order to continue or restore it. The memento pattern provides this functionality and was described in the GoF’s book.

Motivation

Without breaking encapsulation, the internal state of an object, a memento, needs to be captured and externalized so that the object can later be restored to that state. The memento pattern provides a client function to restore the desired state of an object, a memento, on demand.

Finding it in the JDK

The Date class that comes with the java.base module and its java.util package is a nice implementation of the memento pattern. An instance of a class represents a specific point on a timeline, and the date can be restored to that timeline referring to the calendar or zone used.

Sample code

Let us look at the example of air conditioning in a vehicle. The controller gives us several options...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime