Definition -
This pattern provides ability to make efficient use of resources when there are too many objects created by reusing some or all of them.
Scenario -
The most common example and the best suited that example you will see for this is a file editor where in the file gets saved after sometime and if the system shuts down unexpectedly on restart the system will give you an option to recover a saved state. This is one of the best example you can get for this.
Based on the above -
1) There will be a class filesavedstate which will server memory of the previously automated saved state.
2) There will be a class called file which will be modified by a program and saves its state into the filesavedstate class and obtains it if necessary.
Place to use -
1) When you want to save a state of the object and use it for later.
2) Useful in editors.
The code for the above can be found in the link here
This pattern provides ability to make efficient use of resources when there are too many objects created by reusing some or all of them.
Scenario -
The most common example and the best suited that example you will see for this is a file editor where in the file gets saved after sometime and if the system shuts down unexpectedly on restart the system will give you an option to recover a saved state. This is one of the best example you can get for this.
Based on the above -
1) There will be a class filesavedstate which will server memory of the previously automated saved state.
package Memento;
public class filesavedstate {
private String data;
public filesavedstate(String state) {
this.data=state;
}
public String getdata() {
return this.data;
}
}
2) There will be a class called file which will be modified by a program and saves its state into the filesavedstate class and obtains it if necessary.
package Memento;
public class file {
public String filedata;
filesavedstate fs;
public file(String content) {
this.filedata=content;
}
public void savestate() {
fs=new filesavedstate(this.filedata);
}
public void getsaveddata() {
String saveddata=fs.getdata();
System.out.println("The saved data is :"+saveddata);
}
public static void main(String[] args) {
file f=new file("Trying to show a demo");
f.savestate();
f.getsaveddata();
}
}
Place to use -
1) When you want to save a state of the object and use it for later.
2) Useful in editors.
The code for the above can be found in the link here
No comments:
Post a Comment