Creational Patterns

Singleton

This pattern consists on making sure that there is only one instance of an object and provide a global access to such instance (this would be an static object in C#).

Prototype

Creates a duplicate of an existing instance (clone) so you end up with two instances with the same state at the moment of creating it. Useful if you want to keep a copy of the original. This can be done by having a method like “.Clone()” or by using an injection on the constructor: “Person copy = new Person(personToCopy);”.

Factory method

Allows you to determine the type of the instance on run-time by calling a method that has some internal logic to determine which instance you get. While this was planned as a “parent-child” to determine which child you get, it can also be used with interfaces so that you get an object of X type that implements an instance.

Abstract factory

Going a bit further than last pattern, this Factory is a class that allows you to determine not just the type of object that you get but the factory that you call, so that on run-time you can decide which factory to call to instantiate the object and of course, each factory has its rules and ways of instantiating it or can even decide the type of the object (or more, you can combine this with several factory methods each of whose can give you different types).

Leave a Reply

Close Bitnami banner
Bitnami