1) Observer Pattern
Notifies multiple objects when one object changes state.
Example
Event notification system.
using System;
using System.Collections.Generic;
public interface IObserver
{
void Update(string message);
}
public class Subscriber : IObserver
{
private string _name;
public Subscriber(string name)
{
_name = name;
}
public void Update(string message)
{
Console.WriteLine(_name + " received: " + message);
}
}
public class Publisher
{
private List<IObserver> _observers = new List<IObserver>();
public void Subscribe(IObserver observer)
{
_observers.Add(observer);
}
public void Notify(string message)
{
foreach (var observer in _observers)
{
observer.Update(message);
}
}
}
// Usage
Publisher pub = new Publisher();
pub.Subscribe(new Subscriber("User1"));
pub.Notify("New Notification");
2) Strategy Pattern
Defines a family of algorithms and makes them interchangeable.
Example
Different payment methods.
public interface IPaymentStrategy
{
void Pay(int amount);
}
public class CreditCardPayment : IPaymentStrategy
{
public void Pay(int amount)
{
Console.WriteLine("Paid " + amount + " using Credit Card");
}
}
public class UpiPayment : IPaymentStrategy
{
public void Pay(int amount)
{
Console.WriteLine("Paid " + amount + " using UPI");
}
}
public class ShoppingCart
{
private IPaymentStrategy _paymentStrategy;
public ShoppingCart(IPaymentStrategy paymentStrategy)
{
_paymentStrategy = paymentStrategy;
}
public void Checkout(int amount)
{
_paymentStrategy.Pay(amount);
}
}
// Usage
ShoppingCart cart = new ShoppingCart(new UpiPayment());
cart.Checkout(1000);
3) Command Pattern
Encapsulates a request as an object.
Example
public interface ICommand
{
void Execute();
}
public class Light
{
public void On()
{
Console.WriteLine("Light ON");
}
}
public class LightOnCommand : ICommand
{
private Light _light;
public LightOnCommand(Light light)
{
_light = light;
}
public void Execute()
{
_light.On();
}
}
// Usage
Light light = new Light();
ICommand command = new LightOnCommand(light);
command.Execute();
4) Chain of Responsibility Pattern
Passes request along a chain of handlers.
Example
public abstract class Handler
{
protected Handler _next;
public void SetNext(Handler next)
{
_next = next;
}
public abstract void HandleRequest(int level);
}
public class LevelOneHandler : Handler
{
public override void HandleRequest(int level)
{
if (level == 1)
Console.WriteLine("Handled by Level 1");
else if (_next != null)
_next.HandleRequest(level);
}
}
5) Mediator Pattern
Controls communication between objects.
Example
public class ChatRoom
{
public static void ShowMessage(string user, string message)
{
Console.WriteLine(user + ": " + message);
}
}
6) State Pattern
Changes behavior when internal state changes.
Example
public interface IState
{
void Handle();
}
public class StartState : IState
{
public void Handle()
{
Console.WriteLine("Machine Started");
}
}
7) Template Method Pattern
Defines skeleton of algorithm in base class.
Example
public abstract class DataProcessor
{
public void Process()
{
ReadData();
ProcessData();
SaveData();
}
protected abstract void ReadData();
protected abstract void ProcessData();
protected void SaveData()
{
Console.WriteLine("Data Saved");
}
}
8) Iterator Pattern
Provides sequential access without exposing structure.
Example: foreach in C# uses Iterator internally.
9) Memento Pattern
Saves and restores object state.
10) Visitor Pattern
Adds new operations without modifying classes.
11) Interpreter Pattern
Defines grammar and interpreter for language.
Simple Understanding
Observer → Notification system
Strategy → Change algorithm at runtime
Command → Request as object
Chain → Pass request step by step
Mediator → Central communication
State → Behavior changes by state
Template → Fixed algorithm structure
Iterator → Loop through collection
Memento → Undo feature
Visitor → Add new operation
Interpreter → Language parsing