Continuing with the series on Design patterns, I will take up Creational
Patterns from today. I have written about Structural
in my previous posts. You can get all the links for them
Creational patterns deals with separation of a system in terms of how
their objects are created, what they are made of and how they must be
represented. There are 5 types of creational patterns as observed by the
GOF. I will write about the Prototype pattern today.
The prototype patterns deals with creation of new objects by cloning one
of the available prototypes. The creation of objects is much faster for
large classes. It also keeps a record of parts of a data structure so
that it can be copied without knowing the subclass from which they were
As you can see from the above example, the prototype pattern is
implemented using cloning and serialization. When we are using the
shallow copied objects, the value of reference types present inside the
class that was cloned will also be changed when we change the cloned
object, where as in deep copied object this is not the case. We can use
the prototype pattern when we want to hide the concrete class from
client or when we want to add or delete new classes dynamically.
In my next post I will talking about the Factory Method pattern.