diff --git a/memento/README.md b/memento/README.md index 4da2a3acc..43e6f799f 100644 --- a/memento/README.md +++ b/memento/README.md @@ -24,7 +24,7 @@ Without violating encapsulation, capture and externalize an object's internal st Real-world example -> We are working on an astrology application where we need to analyze star properties over time. We are creating snapshots of star states using the Memento pattern. +> Imagine a text editor application that allows users to write and edit documents. The text editor uses the Memento design pattern to implement its undo and redo functionality. Each time a user makes a change to the document, the editor creates a snapshot (memento) of the current state of the document. These snapshots are stored in a history list. When the user clicks the undo button, the editor restores the document to the state saved in the most recent memento. This process allows users to revert to previous versions of their document without exposing or altering the internal data structures of the editor. In plain words @@ -36,6 +36,8 @@ Wikipedia says **Programmatic Example** +We are working on an astrology application where we need to analyze star properties over time. We are creating snapshots of star states using the Memento pattern. + Let's first define the types of stars we are capable to handle. ```java @@ -119,7 +121,9 @@ public class Star { And finally here's how we use the mementos to store and restore star states. ```java - var states = new Stack<>(); +public static void main(String[] args) { + var states = new Stack(); + var star = new Star(StarType.SUN, 10000000, 500000); LOGGER.info(star.toString()); states.add(star.getMemento()); @@ -134,30 +138,27 @@ And finally here's how we use the mementos to store and restore star states. states.add(star.getMemento()); star.timePasses(); LOGGER.info(star.toString()); - while (states.size() > 0) { - star.setMemento(states.pop()); - LOGGER.info(star.toString()); + while (!states.isEmpty()) { + star.setMemento(states.pop()); + LOGGER.info(star.toString()); } +} ``` Program output: ``` -sun age: 10000000 years mass: 500000 tons -red giant age: 20000000 years mass: 4000000 tons -white dwarf age: 40000000 years mass: 32000000 tons -supernova age: 80000000 years mass: 256000000 tons -dead star age: 160000000 years mass: 2048000000 tons -supernova age: 80000000 years mass: 256000000 tons -white dwarf age: 40000000 years mass: 32000000 tons -red giant age: 20000000 years mass: 4000000 tons -sun age: 10000000 years mass: 500000 tons +14:09:15.878 [main] INFO com.iluwatar.memento.App -- sun age: 10000000 years mass: 500000 tons +14:09:15.880 [main] INFO com.iluwatar.memento.App -- red giant age: 20000000 years mass: 4000000 tons +14:09:15.880 [main] INFO com.iluwatar.memento.App -- white dwarf age: 40000000 years mass: 32000000 tons +14:09:15.880 [main] INFO com.iluwatar.memento.App -- supernova age: 80000000 years mass: 256000000 tons +14:09:15.880 [main] INFO com.iluwatar.memento.App -- dead star age: 160000000 years mass: 2048000000 tons +14:09:15.880 [main] INFO com.iluwatar.memento.App -- supernova age: 80000000 years mass: 256000000 tons +14:09:15.880 [main] INFO com.iluwatar.memento.App -- white dwarf age: 40000000 years mass: 32000000 tons +14:09:15.881 [main] INFO com.iluwatar.memento.App -- red giant age: 20000000 years mass: 4000000 tons +14:09:15.881 [main] INFO com.iluwatar.memento.App -- sun age: 10000000 years mass: 500000 tons ``` -## Class diagram - -![alt text](./etc/memento.png "Memento") - ## Applicability Use the Memento pattern when @@ -189,5 +190,5 @@ Trade-offs: ## Credits -* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59) -* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b) +* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI) +* [Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/49NGldq)