Creational design patterns in C# focus on the process of object creation, providing flexibility and decoupling the system from the specific classes it needs to instantiate.
Let's discuss some common creational design
patterns along with examples in C#:
1. Singleton Pattern: Ensures a class has only one instance and provides a global point of
access to it.
2. Factory
Method Pattern: Defines an interface for creating an object but leaves the
choice of its type to the subclasses, creating instances of multiple classes.
3. Abstract
Factory Pattern: Provides an interface for creating families of related or
dependent objects without specifying their concrete classes.
4. Builder
Pattern: Separates the construction of a complex object from its
representation, allowing the same construction process to create different
representations.
5. Prototype
Pattern: Creates new objects by copying an existing object, known as the
prototype, during runtime.
Singleton Pattern:
Ensures that a class has only one instance and provides a global point of access to it.
Explore Singleton's multiple real-time examples
Factory Method Pattern:
Defines an interface for creating an object
but let’s subclasses alter the type of objects that will be created.
Abstract Factory Pattern:
Provides an interface for creating families
of related or dependent objects without specifying their concrete classes.
public
interface IAbstractFactory
{
IProductA CreateProductA();
IProductB CreateProductB();
}
public
interface IProductA
{
void Display();
}
public
interface IProductB
{
void Show();
}
public
class ConcreteFactory1 : IAbstractFactory
{
public IProductA CreateProductA() => new
ConcreteProductA1();
public IProductB CreateProductB() => new
ConcreteProductB1();
}
public
class ConcreteFactory2 : IAbstractFactory
{
public IProductA CreateProductA() => new
ConcreteProductA2();
public IProductB CreateProductB() => new
ConcreteProductB2();
}
//
ConcreteProductA1, ConcreteProductB1, ConcreteProductA2, ConcreteProductB2
classes implement IProductA and IProductB interfaces accordingly.
Builder Pattern:
Separates the construction of a complex
object from its representation, so that the same construction process can
create different representations.
public
class Product
{
private List<string> parts = new
List<string>();
public void AddPart(string part)
{
parts.Add(part);
}
public void Show()
{
Console.WriteLine("Product
Parts:");
foreach (var part in parts)
{
Console.WriteLine(part);
}
}
}
public
abstract class Builder
{
public abstract void BuildPartA();
public abstract void BuildPartB();
public abstract Product GetResult();
}
public
class ConcreteBuilder : Builder
{
private Product product = new Product();
public override void BuildPartA()
{
product.AddPart("Part A");
}
public override void BuildPartB()
{
product.AddPart("Part B");
}
public override Product GetResult()
{
return product;
}
}
public
class Director
{
public void Construct(Builder builder)
{
builder.BuildPartA();
builder.BuildPartB();
}
}
Each pattern addresses specific concerns related to object creation and instantiation, providing a way to create objects in a flexible and maintainable manner.