Behavioral Patterns are Categorized as under:

1. Chain Of Responsibility:
In this pattern there is a single chain of passing a request with many handlers. Each sender keeps a single reference to the head of the chain, and each receiver keeps a single reference to its immediate successor in the chain. This pattern is used to process those requests that are varied.
Model:

UML Diagram:

Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace chainofResposibility
{
class Example
{
static void Main()
{
Approver Sameer = new President();
Approver Abhi = new CEO();
Approver Rahul = new Manager();
Rahul.SetSuccessor(Sameer);
Sameer.SetSuccessor(Abhi);
// Generate and process purchase requests
Project p = new Project(3333, 350.00, "Test Postponed");
Rahul.ProcessRequest(p);
p = new Project(5353, 32590.10, "Annual Function");
Rahul.ProcessRequest(p);
p = new Project(2036, 122100.00, "Renovation of Classes");
Rahul.ProcessRequest(p);
Console.ReadKey();
}
}
//Handler abstract class
abstract class Approver
{
protected Approver successor;
public void SetSuccessor(Approver successor)
{
this.successor = successor;
}
public abstract void ProcessRequest(Project Project);
}
//ConcreteHandler class
class Manager : Approver
{
public override void ProcessRequest(Project Project)
{
if (Project.Amount < 10000.0)
{
Console.WriteLine("{0} approved request# {1}",
this.GetType().Name, Project.Number);
}
else if (successor != null)
{
successor.ProcessRequest(Project);
}
}
}
class President : Approver
{
public override void ProcessRequest(Project Project)
{
if (Project.Amount < 25000.0)
{
Console.WriteLine("{0} approved request# {1}",
this.GetType().Name, Project.Number);
}
else if (successor != null)
{
successor.ProcessRequest(Project);
}
}
}
class CEO : Approver
{
public override void ProcessRequest(Project Project)
{
if (Project.Amount < 100000.0)
{
Console.WriteLine("{0} approved request# {1}",
this.GetType().Name, Project.Number);
}
else
{
Console.WriteLine(
"Request# {0} requires an executive meeting!",
Project.Number);
}
}
}
// Class holding request details
class Project
{
private int _number;
private double _amount;
private string _purpose;
// Constructor
public Project(int number, double amount, string purpose)
{
this._number = number;
this._amount = amount;
this._purpose = purpose;
}
public int Number
{
get { return _number; }
set { _number = value; }
}
public double Amount
{
get { return _amount; }
set { _amount = value; }
}
public string Purpose
{
get { return _purpose; }
set { _purpose = value; }
}
}
}
2. Command:
To call a method later we need some information like method name, its object and its values, to represent that information command pattern is used. It allows a request to be saved in a queue. The Command pattern has three main components: the Invoker, the Command, and the Receiver. Here is an example of Chat application:
Model:
UML Diagram:
Example:
using System;
using System.Collections.Generic;
namespace Command
class Example
{
static void Main()
{
User user = new User(); // Creating User
// User presses calculator buttons
user.Compute('+', 100);
user.Compute('-', 50);
user.Compute('*', 10);
user.Compute('/', 2);
user.Undo(4);
user.Redo(3);
Console.ReadKey();
}
}
abstract class Command
{
public abstract void Execute();
public abstract void UnExecute();
}
//ConcreteCommand' class
class CalculatorCommand : Command
{
private char _operator;
private int _operand;
private Calculator _calculator;
// Constructor
public CalculatorCommand(Calculator calculator,
char @operator, int operand)
{
this._calculator = calculator;
this._operator = @operator;
this._operand = operand;
}
public char Operator
{
set { _operator = value; }
}
public int Operand
{
set { _operand = value; }
}
// Execute new command
public override void Execute()
{
_calculator.Operation(_operator, _operand);
}
// Unexecute last command
public override void UnExecute()
{
_calculator.Operation(Undo(_operator), _operand);
}
private char Undo(char @operator)
{
switch (@operator)
{
case '+': return '-';
case '-': return '+';
case '*': return '/';
case '/': return '*';
default: throw new
ArgumentException("@operator");
}
}
}
// The 'Receiver' class
class Calculator
{
private int _curr = 0;
public void Operation(char @operator, int operand)
{
switch (@operator)
{
case '+': _curr += operand; break;
case '-': _curr -= operand; break;
case '*': _curr *= operand; break;
case '/': _curr /= operand; break;
}
Console.WriteLine(
"Current value = {0,3} (following {1} {2})",
_curr, @operator, operand);
}
}
// The 'Invoker' class
class User
{
private Calculator _calculator = new Calculator();
private List<Command> _commands = new List<Command>();
private int _current = 0;
public void Redo(int levels)
{
Console.WriteLine("\n---- Redo {0} levels ", levels);
for (int i = 0; i < levels; i++)
{
if (_current < _commands.Count - 1)
{
Command command = _commands[_current++];
command.Execute();
}
}
}
public void Undo(int levels)
{
Console.WriteLine("\n---- Undo {0} levels ", levels);
for (int i = 0; i < levels; i++)
{
if (_current > 0)
{
Command command = _commands[--_current] as Command;
command.UnExecute();
}
}
}
public void Compute(char @operator, int operand)
{
// Create command operation and execute it
Command command = new CalculatorCommand(
_calculator, @operator, operand);
command.Execute();
// Add command to undo list
_commands.Add(command);
_current++;
}
}
}
3. Interpreter:
Interpreter pattern defines how the communication between the entities is controlled in a good manner and also defines the instructions grammar. The interpreter design pattern is useful for simple languages where performance is not critical.
Model:
UML Diagram: 
Example:
using System;
using System.Collections.Generic;





Maq SaidPosted Feb 5, 2019, 9:35 PM
Great article. Amazing stuff on Behavioral Patterns relating with .Net C#
RaananeditedPosted Nov 25, 2011, 4:15 PMEdited Nov 25, 2011, 4:16 PM
Keep share great articles. :) Tx