So I'm learning about design patterns in school. Today I was told about the 'Prototype' design pattern.
I must be missing something, because I don't see the benefits from it. I've seen people online say it's faster than using new
but this doesn't make sense; at some point, regardless of how the new object is created, memory needs to be allocated for it.
Doesn't this pattern run in the same circles as the 'chicken or egg' problem? Since the Prototype pattern essentially is just cloning objects, at some point the original object must be created itself (i.e. not cloned). This would mean that I need to have an existing copy of every object I want to clone already ready to clone?
Can anyone explain what the use of this pattern is?
The Prototype pattern is a creation pattern based on cloning a pre-configured object. The idea is that you pick an object that is configured for either the default or in the ballpark of some specific use case and then you clone this object and configure to your exact needs.
The pattern is useful to remove a bunch of boilerplate code, when the configuration required would be onerous. I think of Prototypes as a preset object, where you save a bunch of state as a new starting point.