jargon

Comparison

Deep copy vs shallow copyvsPrototype pattern

Deep copy vs shallow copy

you copied the object, edited the copy's list, and the original's list changed too.

A shallow copy duplicates the object and shares whatever its fields point at; a deep copy duplicates the reachable graph as well. Almost every default copy in every language is shallow, which is why the surprise is so common. Deep copies are expensive, must decide what to do about cycles and shared identity, and are usually a sign that immutable values would serve better.

Full entry →

Prototype pattern

instead of building a new one from scratch you copy an existing configured one and change two fields.

Creating objects by cloning a prepared instance rather than by calling a constructor, so the expensive or fiddly setup happens once. It shows up where construction is costly, where the configuration is easier to demonstrate than to describe, or where the concrete class is not known to the caller. The whole difficulty of it is the copy itself — how deep it goes, and what happens to shared references.

Full entry →

Related comparisons