1

Why saying that Memento is doing its job without violates the encapsulation, while I can implement the simple way but also without violate the encapsulation? What is the use of Memento? I have a sample program, which will save the student details while user press the save button, and undo the action when user press then undo button.
Sample code below is implementation without using Memento pattern:
Student.java

public class Student
{
    private String name;
    private String gender;
    private int age;
    private Contact contact;

    public Student(String name, String gender, int age, Contact contact)
    {
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.contact = contact;
    }
    //getter and setter
}

Main.java

public class Main extends javax.swing.JFrame implements DocumentListener
{
    private Student sCopy, student;

    private void btnUndoActionPerformed(java.awt.event.ActionEvent evt)                                        
    {                                            
        txtName.setText(sCopy.getName());
        txtGender.setText(sCopy.getGender());
        txtAge.setText(sCopy.getAge() + "");
        txtPhone.setText(sCopy.getContact().getPhoneNo());
        txtEmail.setText(sCopy.getContact().getEmail());
        txtAddress.setText(sCopy.getContact().getAddress());
        student = sCopy;
    }                                       

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt)                                        
    {                                            
        sCopy = student;
        Contact c = new Contact(txtPhone.getText(), txtEmail.getText(), txtAddress.getText());
        student = new Student(txtName.getText(), txtGender.getText(), Integer.parseInt(txtAge.getText()), c);
    }    

Sample code above works perfectly, but why we need memento while it can be done so easily? I don't see where the implementation above violets the encapsulation...

Summary
Does approach above violates the encapsulation? If not, then what is the purpose of Memento? allow multiple undo? Although implementation above does not allow multiple undo, but that also can be done without applying memento.

Newbie
  • 1,584
  • 9
  • 33
  • 72
  • 1
    your version will only ever allow one undo while the memento pattern can allow for potentially unlimited undos and also redos – Blake Thingstad Jun 21 '17 at 16:59
  • In your code, you can only undo one single edition (the last, using the scopy variable). With Memento, you can save a lof of editions of student and undo each one. – Dherik Jun 21 '17 at 17:06
  • We are not looking for multiple undo for now. The intent of Memento is restore states without violating encapsulation, but this simple implementation also doesn't violates it right? – Newbie Jun 21 '17 at 17:07
  • Where did you read that Memento violates the encapsulation? – Dherik Jun 21 '17 at 17:08
  • @Dherik I didn't said that Memento violates encapsulation. What I mean is why apply memento pattern while undo can be done with easier implementation, and also without violates encapsulation? – Newbie Jun 21 '17 at 17:14
  • I do not think it voilates encapsulation. But Memento pattern does several things too: it separates which part of the internal state has to be saved; it encapsulates your saved internal state, which can be passed to other objects, while only the originator can access the saved state. – Gábor Szarka May 05 '19 at 18:14

2 Answers2

1

In your approach the instance referenced by sCopy exposes all the available setters. If they are used to change values, the undo will not work correctly. This violates encapsulation, since the correctness of the undo depends on the client of your class.

A memento object would not expose any (mutating) methods, and would always be safe to use for exactly restoring the object's state.

bowmore
  • 10,842
  • 1
  • 35
  • 43
0

Does approach above violates the encapsulation?

Probably. We will see.

First of all, pick some definition of what violates the encapsulation:

Having getters and setters does not in itself break encapsulation. What does break encapsulation is having a getter and a setter for every data member (every field, in java lingo). That is one step away from making all data members public.

Is it your case? You have some getters and setters in the Student, maybe exposing some methods that are not necessary in your application, like gender or name. Even they are necessary, in the future can appear new fields in Student and your solution can't work anymore without violating the encapsulation.

Allow multiple undo?

It's one of the advantages.

Although implementation above does not allow multiple undo, but that also can be done without applying memento.

Yes, but maybe violating the encapsulation.

Dherik
  • 17,757
  • 11
  • 115
  • 164