Design Patterns – Explained in C#

Hello developers!! I know it’s been a long time now from my last blog post. Better late than never! I’ll be writing a series of articles now. In this first series, I’ll be writing about Design Patterns and I’ll explain the patterns in C#. I had this one question all the time since I started as a developer. What are design patterns and why do we need them to be a smart developer any way??

“Design Patters are general and reusable solution templates for commonly occurring  problems in software design.”

Now we know what are design patterns, we need to remember one important thing. These are just solution templates and we need to fill in the muscle (implementation details) into these templates to make a strong solution for our design problem. They can speed up the design and development process by substantially providing tested and proven design paradigms. Taking cue from these design patterns will help us to prevent issues that can cause major road blocks in the software design/development life-cycle. We can broadly classify design patterns into three categories as per the Gang of Four.

Creational Patterns are solution templates which will help us to decide a strategy for creating and managing objects in our program. This helps in managing memory when we are dealing with huge objects and will give us flexibility in creating objects for any given case.

  • Abstract Factory groups object factories that have a common theme.
  • Builder constructs complex objects by separating construction and representation.
  • Factory Method creates objects without specifying the exact class to create.
  • Prototype creates objects by cloning an existing object.
  • Singleton restricts object creation for a class to only one instance.

Structural Patterns  provides us with a template to deal with relationships between code clusters. These templates will use inheritance and interfaces heavily to  define ways in creating objects for new functionality.

  • Adapter matches interfaces of different classes.
  • Bridge separates an object’s interface from its implementation.
  • Composite defines a tree structure of simple and composite objects.
  • Decorator lets you add responsibilities to objects dynamically.
  • Facade is a single class that represents an entire subsystem.
  • Flyweight is a fine-grained instance used for efficient sharing.
  • Proxy is an object representing another object.

Behavior Patterns deals with communication between objects.

  • Chain of Responsibility deals with the way of passing a data between a chain of objects.
  • Command encapsulates a command request as an object.
  • Interpreter defines a way to include language elements in a program.
  • Iterator sequentially access the elements of a collection.
  • Mediator defines simplified communication between classes.
  • Memento will let you know how to capture and restore an object’s internal state.
  • Observer is a way of notifying change to a number of classes.
  • State will let you alter an object’s behavior when its state changes.
  • Strategy encapsulates an algorithm inside a class.
  • Template Method defers the exact steps of an algorithm to a subclass.
  • Visitor defines a new operation to a class without change.

This will server as an introduction to design patterns and hope will motivate you and me to go along and get smarter in our ways of designing and developing software solutions. I’ll be updating this post with links to each pattern as I learn along. Happy Coding!!!

One thought on “Design Patterns – Explained in C#

  1. Pingback: PROTOTYPE PATTERN | Dev Ramblings

Leave a comment