Design Patterns Java - Memento Php

1) What have we got at the beginning?

We have a simple class "ClassA" with an one attribute(with setter and set by constructor)


public class ClassA
{
    private String attribue;

    public ClassA(String attribue)
    {
        this.attribue = attribue;
    }

    public void doSomething()
    {
        System.out.println("Doing something with attribute " + attribue);
    }

    public void setAttribue(String attribue)
    {
        this.attribue = attribue;
    }
}




We have some state of this objects (which is defined by attributes) for clarity in our example is only on attribute.

2) What is our goal?

Let's see client.


public class Client
{
    public static void main(String[] args)
    {
        ClassA classA = new ClassA("value1");
        classA.setAttribue("value2"); // we want to possibility of undo this action
        classA.doSomething();
    }
}




we have simple three steps here:
- first we created object of "ClassA" with "value"
- then we change state - by calling setter
- finally we call some business action

We would like to have possibility of undoing changing state

3) How we can do this?

For this purpose is created "Memento" pattern in which we save states of "ClassA" let's see this in step2