The creational patterns aim to separate a system from how its objects are created, composed, and represented. They increase the system's flexibility in terms of the what, who, how, and when of object creation. Creational patterns encapsulate the knowledge about which classes a system uses, but they hide the details of how the instances of these classes are created and put together. Programmers have come to realize that composing systems with inheritance makes those systems too rigid. The creational patterns are designed to break this close coupling.
Abstract Factory Pattern
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
In this example demonstrates the creation of different animal worlds for a computer game using different factories. Although the animals created by the Continent factories are different, the interactions among the animals remain the same.
Lion eats Wildebeest
Wolf eats Bison
Separate the construction of a complex object from its representation so that the same construction process can create different representations and and provides a high level of control over the assembly of the objects.
In this example demonstrates the Builder pattern in which different vehicles are assembled in a step-by-step fashion. The Shop uses VehicleBuilders to construct a variety of Vehicles in a series of sequential steps.
Factory Method is one of creational design patterns. It is used to deal with the problem of creating objects without specifying exact result type. This document will teach you how to use Factory Method DP properly.
Let me explain the idea of it to you on a simple example. Imagine you're working in a factory that produces three types of devices - Ammeter, Voltmeter and resistance meter. You are writing a program for a central computer that will create selected device, but you don't know final decision of your boss on what to produce.
Let's create an interface IDevice with some common functions that all devices have:
Now, we can create classes that represent our devices. Those classes must implement IDevice interface:
Now we have to define factory method. Let's create DeviceFactory class with static method inside:
Great! Let's test our code:
This is the example output you might see after running this code:
AmMeter turns on...
AmMeter flashes lights saying good bye!
VoltMeter turns on...
VoltMeter flashes lights saying good bye!
OhmMeter turns on...
OhmMeter flashes lights saying good bye!
Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.
In this example demonstrates the Prototype pattern in which new Color objects are created by copying pre-existing, user-defined Colors of the same type.
Cloning color RGB: 255, 0, 0
Cloning color RGB: 128,211,128
Cloning color RGB: 211, 34, 20
The Singleton pattern is designed to restrict creation of a class to exactly one single instance.
This pattern is used in a scenario where it makes sense to have only one of something, such as:
a single class that orchestrates other objects' interactions, ex. Manager class
or one class that represents a unique, single resource, ex. Logging component
One of the most common ways to implement the Singleton pattern is via a static factory method such as a CreateInstance() or GetInstance() (or a static property in C#, Instance), which is then designed to always return the same instance.
The first call to the method or property creates and returns the Singleton instance. Thereafter, the method always returns the same instance. This way, there is only ever one instance of the singleton object.
Preventing creation of instances via new can be accomplished by making the class constructor(s) private.
Here is a typical code example for implementing a Singleton pattern in C#:
To illustrate this pattern further, the code below checks whether an identical instance of the Singleton is returned when the Instance property is called more than once.
In C#, you have the ability to make a class static, which makes all members static, and the class cannot be instantiated. Given this, it is common to see static classes used in place of the Singleton pattern.