Question: [Java] [Memento Pattern] In-Memory Database with Persistence 1. We will create an in-memory database for a bookstore inventory. The bookstore sells books. Each book has
[Java] [Memento Pattern] In-Memory Database with Persistence
1. We will create an in-memory database for a bookstore inventory. The bookstore sells books. Each book has a name, price, unique id and a quantity. The store uses sequential integers for unique ids. We need to be able to
Add new books.
Sell a book in the inventory.
Add new copies of existing books
Change the price of a book
Find the price and/or quantity of a book by either name or id.
Create an Inventory class to keep track of the store inventory.
2. Use thememento patterntocopy the datain an Inventory object. Make the memento serializable so it can be saved in a file. Given an Inventory object and a memento you can restore the Inventory object to a previous state.
So now we can periodically create and save a memento of the Inventory object.
3. For each operation that changes the state of the Inventory object creates a command. Make the commands serializable.
Now every time we perform an operation on an Inventory object, we can create a command, perform the command and save the command to disk. This way we will have a history of all the operations. If our program were to crash we can recover the last state by first loading the last memento and then replaying all the commands done since the last memento was created.
4. Create a decorator for Inventory objects. For every operation that changes the Inventory object's state, the decorator will create the command, perform the command and save the command to a file.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
