Please enable JavaScript.
Coggle requires JavaScript to display documents.
Prototype Pattern - Coggle Diagram
Prototype Pattern
Problem
It's a tedious process to copy objects, you first have to create a new object and copy all the fields from the previous object.
Another problem is that not everything can be copied over, for example some fields may be private, etc...
You also have to know the concrete class of the object, making your code dependent on it.
You can also depend on the interface, but this will introduce a problem that you don't know the concrete class.
Solution
-
Similar how it works in PHP, where every object can be cloned.
-
You can have different configurations of objects, and when you want a clone of one. You don't have to create one from scratch, but simply copy one over.
Pros
-
-
-
Alternative to inheritance, just have different configs and copy them
-
Opportunities
Use the prototype pattern when you client doesn't need to depend on concrete classes to copy objects
Use the pattern when you want to limit the number of subclasses that differ only in their initial field values.
-
-