3

Thank you for reading this question. Please help me to solve this complex issue.

Here is the situation:

Oringator holds a object to execute some method.

Memento pattern needs to backup the complex object state in oringator.How?

Example code:

http://pastebin.com/4rV2aw3B

The problem is , java does pass by value, do not pass by reference. But it is a bit tricky for object passing. If I passed an object into the method, although the method holds a new memeory address of object,the memory address is still pointing to the same object which is passed in. See this example: http://www.javaworld.com/javaqa/2000-05/03-qa-0526-pass.html

If I create the memento and pass the object to the new memento object, the memento object still holds the orignal complex object(which is needed to backup).

So, how to create the memento pattern for backup the complex object which is in command pattern?

Thank you.

Peter Choi
  • 45
  • 6
  • I'm pretty sure that Java passes objects by reference. The value of the reference itself is passed by value. http://stackoverflow.com/questions/40480/is-java-pass-by-reference?rq=1 – Anders Johansen Nov 19 '13 at 07:58

2 Answers2

6

Memento pattern needs to backup the complex object state in oringator.How?

I assume that the intention is that the Memento should hold a copy of the differentObj state ...

There is no magical solution. Rather, it depends on what differentObj is.

  • If it is immutable, then there is no need to copy it at all.

  • If it has a copy constructor or a clone method you could use that, modulo that the constructor / method has the required copying semantics.

  • If it implements Serializable or Externalizable, then you can us Java Object Serialization, and use the serialized object to represent the saved state,

  • You could potentially write a custom serializer, or use a Java to JSON or XML binding.


Note that (hypothetical) call by reference wouldn't necessarily solve the problem. You'd also need some form of copying constructor (or similar) to allow the copy to be made ... and you'd be dependent on that constructor having the right semantics.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    Thank you for command this problem. But how about if the differentObj has another complex obj inside there? how to backup this Obj? – Peter Choi Nov 18 '13 at 17:33
0

Have you tried cloning the Memento object?

Chaffers
  • 176
  • 9
  • Yes, it can be clone the object, but in this situation, i assumed that the object is very very complex and can not be cloned. So.. no . – Peter Choi Nov 18 '13 at 17:26
  • I have a project which is due with the same problem like this. so i can not clone it. – Peter Choi Nov 18 '13 at 17:26
  • In that case serialize and de-serialize will give you an identical object with different references. So long of course as all objects within the complex one are fully serializeable. Watch out for transient etc... – Chaffers Nov 18 '13 at 17:33
  • No:( that object was too complex, can't just clone, the references of all objects need to be updated. – Peter Choi Feb 18 '14 at 12:36
  • Did the serialize and deserialize nor work? What errors are you seeing? – Chaffers Feb 24 '14 at 11:10
  • serialize cant solve the problem, the main problem is , inside the objects, there are some link between another objects, so it cant be cloned . the objects reference is still there even it is cloned. – Peter Choi Mar 31 '14 at 14:24