Here is Part 1
Behavioral Design patterns are the patterns for .Net in which there is a way through which we can pass the request between the chain of objects, or we can say that it defines the manner to communicate between classes and object.
Behavioral Patterns are Categorized as under:

6. Memento:
This pattern proves to be helpful in saving the internal state of the object in to external state and enabling us to restore the state later when needed means it easily restore an object to its previous state with minimal lines of coding. Memento pattern helps us to store a snapshot which can be reverted at any moment of time by the object. A word processor providing undo operation or Ctrl+Z is the example of this pattern.
Model:

UML Diagram:

Example:
using System;
using System.Data;
namespace Memento
{
class Example
{
static void Main()
{
Originator o = new Originator();
o.State = "On";
Caretaker c = new Caretaker(); // Store internal state
c.Memento = o.CreateMemento();
o.State = "Off"; // Continue changing originator
// Restore saved state
o.SetMemento(c.Memento);
Console.ReadKey();
}
}
class Originator
{
private string _state;
public string State
{
get { return _state; }
set
{
_state = value;
Console.WriteLine("State = " + _state);
}
}
// Creates memento
public Memento CreateMemento()
{
return (new Memento(_state));
}
public void SetMemento(Memento memento)
{
Console.WriteLine("Restoring state...");
State = memento.State;
}
}
class Memento
{
private string _state;
public Memento(string state)
{
this._state = state;
}
public string State
{
get { return _state; }
}
}
class Caretaker //The 'Caretaker' class
{
private Memento _memento;
public Memento Memento
{
set { _memento = value; }
get { return _memento; }
}
}
}
7. Observer:
This pattern allows us to plug objects into a framework at runtime, which allows for highly flexible, extensible, and reusable software. It is used when it is necessary to ensure that multiple components (called observers or subscribers) are kept in sync with a master set of data (the subject or publisher).
Model:
UML Diagram:
Example:

using System;
using System.Collections.Generic;
namespace Observer
{
class Example
{
static void Main()
{
ConcreteSubject s = new ConcreteSubject();
s.Attach(new ConcreteObserver(s, "X"));
s.Attach(new ConcreteObserver(s, "Y"));
s.Attach(new ConcreteObserver(s, "Z"));
s.SubjectState = "ABC";
s.Notify();
Console.ReadKey();
}
}
abstract class Subject
{
private List<Observer> _observers = new List<Observer>();
public void Attach(Observer observer)
{
_observers.Add(observer);
}
public void Detach(Observer observer)
{
_observers.Remove(observer);
}
public void Notify()
{
foreach (Observer o in _observers)
{
o.Update();
}
}
}
class ConcreteSubject : Subject
{
private string _subjectState;
public string SubjectState
{
get { return _subjectState; }
set { _subjectState = value; }
}
}
abstract class Observer
{
public abstract void Update();
}
class ConcreteObserver : Observer
{
private string _name;
private string _observerState;
private ConcreteSubject _subject;
public ConcreteObserver(
ConcreteSubject subject, string name)
{
this._subject = subject;
this._name = name;
}
public override void Update()
{
_observerState = _subject.SubjectState;
Console.WriteLine("Observer {0}'s new state is {1}",
_name, _observerState);
}
public ConcreteSubject Subject
{
get { return _subject; }
set { _subject = value; }
}
}
}
8. State:
State Pattern allows an object to alter its behavior dynamically according to its internal state. For this we directly move the logic to property "State" which has method Handle() and call this method. Vending Machine is one of the example of it:
Model:

UML Diagram:

Example:
using System;
using System.Data;
using System.Collections.Generic;
namespace State
{
class Example
{
static void Main()
{
// Open a new account
Account account = new Account("Mr.Chabbra");
// Apply financial transactions
account.Deposit(500.0);
account.Deposit(300.0);
account.Deposit(550.0);
account.PayInterest();
account.Withdraw(2000.00);
account.Withdraw(1100.00);
Console.ReadKey();
}
}
abstract class State
{
protected Account account;
protected double balance;
protected double interest;
protected double lowerLimit;
protected double upperLimit;
public Account Account
{
get { return account; }
set { account = value; }
}
public double Balance
{
get { return balance; }
set { balance = value; }
}
public abstract void Deposit(double amount);
public abstract void Withdraw(double amount);
public abstract void PayInterest();
}
//Red indicates that account is overdrawn
class RedState : State
{
private double _serviceFee;
public RedState(State state)
{
this.balance = state.Balance;
this.account = state.Account;
Initialize();
}
private void Initialize() // come from a datasource
{
interest = 0.0;
lowerLimit = -100.0;
upperLimit = 0.0;
_serviceFee = 15.00;
}
public override void Deposit(double amount)
{
balance += amount;
StateChangeCheck();
}
public override void Withdraw(double amount)
{
amount = amount - _serviceFee;
Console.WriteLine("No funds available for withdrawal!");
}
public override void PayInterest()
{ // No interest is paid }
private void StateChangeCheck()
{
if (balance > upperLimit)
{
account.State = new SilverState(this);
}
}
}
//Silver indicates a non-interest bearing state
class SilverState : State
{
// Overloaded constructors
public SilverState(State state) :
this(state.Balance, state.Account)
{ }
public SilverState(double balance, Account account)
{
this.balance = balance;
this.account = account;
Initialize();
}
private void Initialize() //come from a datasource
{
interest = 0.0;
lowerLimit = 0.0;
upperLimit = 1000.0;
}
public override void Deposit(double amount)
{
balance += amount;
StateChangeCheck();
}
public override void Withdraw(double amount)
{
balance -= amount;
StateChangeCheck();
}
public override void PayInterest()
{
balance += interest * balance;
StateChangeCheck();
}
private void StateChangeCheck()
{
if (balance < lowerLimit)
{
account.State = new RedState(this);
}
else if (balance > upperLimit)
{
account.State = new GoldState(this);
}
}
}







Join the conversation! Your thoughts help the community grow.