From the course: Java Design Patterns: Behavioral Part 1
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Restoring an object's state - Java Tutorial
From the course: Java Design Patterns: Behavioral Part 1
Restoring an object's state
- [Instructor] In this example I have an app for writing text to a document. I want to add Save and Undo operations to it. And I'm going to do that using the memento pattern. So let's have a look at the code so far. Firstly, I have a class called TextDocument. This has a string field called text, which is just set to an empty string to begin with. This class then has two methods, a writes method, which adds more texts to the text field and print, which prints out the text to the console. There's also a class called DocumentsViewer. This has a field of type textDocument, which is the class we just looked at. It also has a main method and in here I'm calling the writes method and passing an hello on the textDocument. Then I'm printing that. Then I'm adding the word world and printing again. So let's run this to see what it is like. So it's printing out hello. Then I'm adding world and it's printing out hello world. I want…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.