Java Zero to All All questionsPractise free
Interview questions › Design Patterns in Java › Creational Design Patterns

When is Prototype the right choice, and how does Spring's @Scope('prototype') relate to it?

Level III · MidCreational Design Patterns
Short answer

Prototype fits when constructing an object is expensive and you have a configured exemplar to copy, or you maintain a registry of pre-built prototypes — for cheap or immutable objects just use new, since deep-copying large graphs can cost more than rebuilding.

Explanation

Spring's prototype scope is a different mechanism: it creates a brand-new bean instance per lookup, it does not clone an exemplar, so it scratches the same 'I need fresh instances' itch without GoF copying. Conflating the two is a common slip; the distinction is clone-an-instance versus construct-a-new-one.

Common mistakes
  • Confusing Spring prototype scope with the Prototype pattern.
  • Expecting Spring to destroy prototype beans.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Creational Design Patterns.

Practise free

More on Creational Design Patterns