I am using Composite pattern in my Java program, such that a Component is being extended by three classes.
The composite is currently implemented using a List, where each element can either be a leaf, another Composite or a decorator. I am thinking of adding undo/redo functionality for which I am going to be using Command/Memento patterns.
Now my question, how can I create a deep copy of my list here, so that I can restore it later? Basically, need to find a way that allows me to create a new identical copy of my list at the current time before executing another command. I am thinking there has to be some recursive way of doing it.
Thanks
The cheapest approach to code (but perhaps not the cheapest in terms of the overall CPU and memory use) is to mark your classes serializable, serialize the list into a memory buffer, and then deserialize it back into an object. If you do it right, the result is going to be a deep copy of your list of objects.