Advertisement
Guest User

Untitled

a guest
Nov 18th, 2013
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. class differentObj{
  2.     private String nickname;
  3.    
  4.     public differentObj(String nickname){
  5.         this.nickname=nickname;
  6.     }
  7.    
  8.     public void setNickname(String nickname){
  9.     //should be complex operation in here, i just make it simple to understand.
  10.         this.nickname=nickname;
  11.     }
  12.     public String getNickname(){
  13.         return nickname;
  14.     }
  15. }
  16.  
  17. class Originator {
  18.     private String _name;
  19.     private String _phone;
  20.     private differentObj obj;
  21.    
  22.     public Originator(String name, String phone) {
  23.         _name = name;
  24.         _phone = phone;
  25.         this.obj = new differentObj(name);
  26.     }
  27.  
  28.     // Some operations make state changed
  29.     public void someOperation() {
  30.         obj.setNickname("1234");
  31.         _name = "noboby";
  32.         _phone = "911-911";
  33.     }
  34.    
  35.     // recover object's state
  36.     public void setMemento(Memento m) {
  37.         _name = m.getName();
  38.         _phone = m.getPhone();
  39.         //same object with different memeory address but point to same point
  40.         obj = m.getObj();
  41.     }
  42.  
  43.     public Memento createMemento() {
  44.     //how? how to due with complex object memento pattern?
  45.     //how to save this object up?
  46.         return new Memento(_name, _phone,obj);  //<==obj, just give the same memeory allocation to the memento, cannot back up the object.
  47.     }
  48.      
  49.     public void showInfo() {
  50.         System.out.println( "Nicknameobj: " + obj.getNickname() +
  51.                         "\nName: " + _name +
  52.                          "\nPhone: " + _phone + "\n");
  53.     }
  54. }
  55. class Memento {
  56.     private String _name;
  57.     private String _phone;
  58.     private differentObj obj;
  59.  
  60.     public Memento(String name, String phone,differentObj obj) {
  61.         _name = name;
  62.         _phone = phone;
  63.         this.obj=obj;
  64.     }
  65.    
  66.     public differentObj getObj() {
  67.         return obj;
  68.     }
  69.  
  70.     public void setObj(differentObj obj) {
  71.         this.obj=obj;
  72.     }
  73.  
  74.    
  75.     public String getName() {
  76.         return _name;
  77.     }
  78.  
  79.     public String getPhone() {
  80.         return _phone;
  81.     }
  82.  
  83.     public void setName(String name) {
  84.         _name = name;
  85.     }
  86.  
  87.     public void setPhone(String phone) {
  88.         _phone = phone;
  89.     }
  90. }
  91. class Caretaker {
  92.     private Memento _memento;
  93.  
  94.     public void setMemento(Memento memento) {
  95.         _memento = memento;
  96.     }
  97.  
  98.     public Memento getMemento() {
  99.         return _memento;
  100.     }
  101. }
  102. public class test222 {
  103.     public static void main(String[] args) {
  104.         Originator originator = new Originator("Justin", "888-8888");
  105.         Caretaker caretaker = new Caretaker();
  106.    
  107.     // save object's memento
  108.         caretaker.setMemento(originator.createMemento());
  109.  
  110.     originator.showInfo();
  111.         // some operations make the object's state changed
  112.         originator.someOperation();
  113.         originator.showInfo();
  114.  
  115.     // use memento to recover object's state
  116.         originator.setMemento(caretaker.getMemento());
  117.         originator.showInfo();
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement