Design Patterns In C#
Design patterns provide general solutions or a flexible way to solve common design problems. This article introduces design patterns and how design patterns are implemented in C# and .NET.
Before starting with design patterns in .NET, let's understand the meaning of design patterns and why they are useful in software architecture and programming.
What are Design Patterns in Software Development?
Design Patterns in the object-oriented world are a reusable solution to common software design problems that repeatedly occur in real-world application development. It is a template or description of how to solve problems that can be used in many situations.
"A pattern is a recurring solution to a problem in a context."
"Each pattern describes a problem that occurs over and over again in our environment and then describes the core of the solution to that problem in such a way that you can use this solution a million times over without ever doing it the same way twice." - Christopher Alexander, A Pattern Language.
Developers use patterns for their specific designs to solve their problems. Pattern choice and usage among various design patterns depend on individual needs and concerns. Design patterns are a very powerful tool for software developers. It is important to understand design patterns rather than memorizing their classes, methods, and properties. Learning how to apply patterns to specific problems is also important to get the desired result. This will require continuous practice using and applying design patterns in software development. First, identify the software design problem, then see how to address these problems using design patterns and determine the best-suited design problem to solve the problem.
There are 23 design patterns, also known as Gang of Four (GoF) design patterns. The Gang of Four is the authors of the book, "Design Patterns: Elements of Reusable Object-Oriented Software". These 23 patterns are grouped into three main categories:

Creational Design Pattern
- Factory Method
- Abstract Factory
- Builder
- Prototype
- Singleton
Structural Design Patterns
- Adapter
- Bridge
- Composite
- Decorator
- Façade
- Flyweight
- Proxy
Behavioral Design Patterns
- Chain of Responsibility
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Visitor
- Template Method
In this article, we learn and understand Creational Design Patterns in detail, including a UML diagram, template source code, and a real-world example in C#. Creational Design Patterns provide ways to instantiate a single object or group of related objects. These patterns deal with the object creation process in such a way that they are separated from their implementing system. That provides more flexibility in deciding which object needs to be created or instantiated for a given scenario. There are the following five such patterns.
Abstract Factory
This creates a set of related objects or dependent objects. The "family" of objects created by the factory is determined at run-time depending on the selection of concrete factory classes.
An abstract factory pattern acts as a super-factory that creates other factories. An abstract factory interface creates a set of related or dependent objects without specifying their concrete classes.
The UML class diagram below describes an implementation of the abstract factory design pattern.

The classes, objects, and interfaces used in the above UML diagram are described below.
- Client This class uses the Abstract Factory and Abstract Product interfaces to create a family of related objects.
- Abstract Factory This is an interface that creates abstract products.
- Abstract Product This is an interface that declares a type of product.
- Concrete factory This class implements the abstract factory interface to create concrete products.
- Concrete Product This class implements the abstract product interface to create products.
The following code shows the basic template code of the abstract factory design pattern implemented using C#:


In the above abstract factory design pattern, the source code template client has two private fields that hold the instances of abstract product classes. These objects will be accessed by inheriting their base class interface. When the client is instantiated, a concrete factory object is passed to its constructor and populated private fields of the client with appropriate data or values.
The Abstract factory is a base class for concrete factory classes that generate or create a set of related objects. This base class contains the definition of a method for each type of object that will be instantiated. The base class is declared Abstract so that other concrete factory subclasses can inherit it.
The concrete factory classes are inherited from the Abstract factory class and override the base class method to generate a set of related objects required by the client. Depending on the software or application requirements, there can be a specified number of concrete factory classes.
Abstractproduct is a base class for the types of objects that the factory class can create. There should be one base type for every distinct type of product required by the client.
The concrete product classes are inherited from Abstractproduct class. Each class contains specific functionality. Objects of these classes are generated from the Abstractfactory to populate the client.
A real-world example of an Abstract factory design pattern using C#
For example, consider a system that does the packaging and delivery of items for a web-based store. The company delivers two types of products. The first is a standard product placed in a box and delivered through the post with a simple label. The second is a delicate item that requires shock-proof packaging and is delivered via a courier. In this situation, two types of objects are required: a packaging object and a delivery documentation object. We could use two factories to generate these related objects. One factory will create packaging and other delivery objects for standard parcels. The second will create packaging and delivery objects for delicate parcels. Class Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractFactoryPatternExample
{
public class Client
{
private Packaging _packaging;
private DeliveryDocument _deliveryDocument;
public Client(PacknDelvFactory factory)
{
_packaging = factory.CreatePackaging();
_deliveryDocument = factory.CreateDeliveryDocument();
}
public Packaging ClientPackaging
{
get { return _packaging; }
}
public DeliveryDocument ClientDocument
{
get { return _deliveryDocument; }
}
}
public abstract class PacknDelvFactory
{
public abstract Packaging CreatePackaging();
public abstract DeliveryDocument CreateDeliveryDocument();
}
public class StandardFactory : PacknDelvFactory
{
public override Packaging CreatePackaging()
{
return new StandardPackaging();
}
public override DeliveryDocument CreateDeliveryDocument()
{
return new Postal();
}
}
public class DelicateFactory : PacknDelvFactory
{
public override Packaging CreatePackaging()
{
return new ShockProofPackaging();
}
public override DeliveryDocument CreateDeliveryDocument()
{
return new Courier();
}
}
public abstract class Packaging { }
public class StandardPackaging : Packaging { }
public class ShockProofPackaging : Packaging { }
public abstract class DeliveryDocument { }
public class Postal : DeliveryDocument { }
public class Courier : DeliveryDocument { }
}





Sagar ChowdhuryPosted Jul 9, 2020, 4:34 PM
Hi Munib, while after renaming that Solution I got the same issue again . Can you clone once again. https://github.com/Sagar2493/FactoryDesignPattern
Munib ButtPosted Jul 1, 2020, 7:41 AM
The code has been fixed. However, I could not push the changes to the Git hub repository. I have created pull requests for you to view and accept. There were two main changes as per your code: 1. var factory = (ThermoStatFactory) Activator.CreateInstance(Type.GetType("DesignPatterns.Factory." + Enum.GetName(typeof(Actions),action) + "ThermoStatFactory")); 2. Ensure that the CoolingThermoStatFactory class is not abstract -- public class CoolingThermoStatFactory : ThermoStatFactory
Munib ButtPosted Jun 29, 2020, 8:32 AM
Kindly ensure the namespace and class name in this line are correct as per your code. For the sample code it is as follows: DesignPatterns.CoolingThermostatFactory and DesignPatterns.WarmingThermostatFactory
Sagar ChowdhuryPosted Jun 28, 2020, 8:35 PM
In Factory pattern example while executing this line I am getting "Value cannot be null" error. var factory = (ThermoStatFactory)Activator.CreateInstance(Type.GetType("DesignPatterns." + Enum.GetName(typeof(Actions),action) + "ThermoStatFactory"))