Creation Design Patterns : All Pattern related with the creation of the objects
- Prototype :
When to Use ? : If we can create a object with minimal effort (resource , class interaction complexity, object number), then use simple object creation technique, else use prototype pattern . e.g chess board create board, create 16 white pieces, 16 black pieces, position them correctly, too much of effort, hence use a prototype pattern to duplicate existing boards and reset position
A fully initialized instance to be copied or cloned. For example in a chess, we always start with the Fully setup chess board. One way is to instantiate a Fully set-upped initial chess board every-time a game starts. And let’s say in your game, there is one game starting every minute or that the initial setup is computationally intensive, why waste all that time and effort.Prototype is where the solution comes in. With prototype, you create a initial chess board once and for every game , you clone the object, thus removing all the hassles, time and computational complexity.
Rule of Thumb : Use simple new object creation technique. If using too many many new then its time for using prototype pattern.
- Builder : e.g A Builder which builds a full course meal. It abstracts complex objects for a single object. For example a Full course meal will contain drink, starters, main course and desert. When the details are complex i.e 4 diff objects , then it will be easier for the users to abstract all those complexities with a single interface. That is where the builder patterns comes in.
- Singleton : e.g President in a country. There can be only one instance of that particular class in your system. In java System class, you can only have one instance of.
- Factory : e.g Person Factory generator generating male or female person type.Factory pattern allows for extension. For example if you want to introduce the “third Sex” Gender, then you can simple do so, without affecting the earlier male , female codes.