Chain
of Responsibility:
The
chain of responsibility pattern is a way of communicating between
objects.
class Program
{
static void Main(string[] args)
{
Approver director = new Director();
Approver vicePresident = new VicePresident();
Approver president = new President();
director.SetSuccessor(vicePresident);
vicePresident.SetSuccessor(president);
Purchase p=new Purchase(1,200.00,"Assets");
director.ProcessRequest(p);
p = new Purchase(2, 1010.00, "Assets2");
director.ProcessRequest(p);
p = new Purchase(1, 2010.00, "Assets3");
director.ProcessRequest(p);
Console.ReadKey();
}
}
abstract class Approver
{
protected Approver successor;
public void SetSuccessor(Approver successsor)
{
this.successor = successsor;
}
public abstract void ProcessRequest(Purchase purchase);
}
class Director : Approver
{
public override void ProcessRequest(Purchase purchase)
{
if (purchase.Amount < 1000)
Console.WriteLine("{0} Approved Request# {1}", this.GetType().Name, purchase.Amount);
else if (successor != null)
successor.ProcessRequest(purchase);
}
}
class VicePresident : Approver
{
public override void ProcessRequest(Purchase purchase)
{
if (purchase.Amount < 2000)
Console.WriteLine("{0} Approved Request# {1}", this.GetType().Name, purchase.Amount);
else if (successor != null)
successor.ProcessRequest(purchase);
}
}
class President : Approver
{
public override void ProcessRequest(Purchase purchase)
{
if (purchase.Amount >= 2000)
Console.WriteLine("{0} Approved Request# {1}", this.GetType().Name, purchase.Amount);
else if (successor != null)
successor.ProcessRequest(purchase);
}
}
//Purchase
class Purchase
{
private int _number;
private double _amount;
private string _purpose;
public Purchase(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; }
}
}
class Program
{
static void Main(string[] args)
{
Approver director = new Director();
Approver vicePresident = new VicePresident();
Approver president = new President();
director.SetSuccessor(vicePresident);
vicePresident.SetSuccessor(president);
Purchase p=new Purchase(1,200.00,"Assets");
director.ProcessRequest(p);
p = new Purchase(2, 1010.00, "Assets2");
director.ProcessRequest(p);
p = new Purchase(1, 2010.00, "Assets3");
director.ProcessRequest(p);
Console.ReadKey();
}
}
abstract class Approver
{
protected Approver successor;
public void SetSuccessor(Approver successsor)
{
this.successor = successsor;
}
public abstract void ProcessRequest(Purchase purchase);
}
class Director : Approver
{
public override void ProcessRequest(Purchase purchase)
{
if (purchase.Amount < 1000)
Console.WriteLine("{0} Approved Request# {1}", this.GetType().Name, purchase.Amount);
else if (successor != null)
successor.ProcessRequest(purchase);
}
}
class VicePresident : Approver
{
public override void ProcessRequest(Purchase purchase)
{
if (purchase.Amount < 2000)
Console.WriteLine("{0} Approved Request# {1}", this.GetType().Name, purchase.Amount);
else if (successor != null)
successor.ProcessRequest(purchase);
}
}
class President : Approver
{
public override void ProcessRequest(Purchase purchase)
{
if (purchase.Amount >= 2000)
Console.WriteLine("{0} Approved Request# {1}", this.GetType().Name, purchase.Amount);
else if (successor != null)
successor.ProcessRequest(purchase);
}
}
//Purchase
class Purchase
{
private int _number;
private double _amount;
private string _purpose;
public Purchase(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; }
}
}
No comments:
Post a Comment