Hello all,
I am trying to implement memento design pattern in my project to add and remove products from cart.
I have abstract product class and sub classes like Book, Software, Phones etc..

I googled and found some examples using three classes :
1) Memento
2) Originator
3) Caretaker

I still can't figure how do I use them to add and remove products(undo last product products) each time.

Will appreciate,if you could provide me the structure or an example would be better.

Recommended Answers

All 3 Replies

Create a Memento before each time you add or remove. Use the latest Memento to roll back the latest change. The WikiPedia article on memento design pattern has an example in Java.

Please have a look at below code I made,

import java.util.List;
import java.util.ArrayList;

class Originator {
    private Product product;

    public void set(Product product) {
        System.out.println("Originator: Setting state to " + product);
        this.product = product;
    }

    public Memento saveToMemento() {
        System.out.println("Originator: Saving to Memento.");
        return new Memento(this.product);
    }

    public void restoreFromMemento(Memento memento) {
        this.product = memento.getSavedProduct();
        System.out.println("Originator: State after restoring from Memento: "
                + product);
    }

    public static class Memento {
        private final Product product;

        public Memento(Product productToSave) {
            product = productToSave;
        }

        public Product getSavedProduct() {
            return product;
        }
    }
}

public class Caretaker {
    public static void main(String[] args) {
        List<Originator.Memento> savedStates = new ArrayList<Originator.Memento>();

        Originator originator = new Originator();
        originator.set(new Product(10, "CD"));
        originator.set(new Product(15, "iPhone"));
        savedStates.add(originator.saveToMemento());

        originator.set(new Product(20, "Microsoft Office"));
        savedStates.add(originator.saveToMemento());
        originator.set(new Product(25, "Java Programing Book"));

        originator.restoreFromMemento(savedStates.get(1));
    }
}

class Product {
    int id;
    String name;

    public Product(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public String toString() {
        return "id=" + id + ", name=" + name;
    }
    // getters setters
}

Actually I want something like :
1) 3 Products added
2) last product removed (Now I would have first 2 products)
3) 1 product added (Now I would have 3 products)
4) 1 product added (Now I would have 4 products)
5) last product removed (Now would have 3 products)

Then why not code a test case that looks like that in your Caretaker, rather than the different sequence of events you have progrmmed in lines 40-49?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.