То, что вы хотите, это Цепочка ответственности шаблон проектирования.
Вот пример на C #:
using System;
internal sealed class Program
{
private static void Main()
{
// Setup Chain of Responsibility.
Approver larry = new Director();
Approver sam = new VicePresident();
Approver tammy = new President();
larry.Successor = sam;
sam.Successor = tammy;
Purchase purchase;
// Generate and process purchase requests.
purchase = new Purchase {
Number = 2034, Amount = 350.00, Purpose = "Supplies" };
larry.ProcessRequest(purchase);
purchase = new Purchase {
Number = 2035, Amount = 32590.10, Purpose = "Project X" };
larry.ProcessRequest(purchase);
purchase = new Purchase {
Number = 2036, Amount = 122100.00, Purpose = "Project Y" };
larry.ProcessRequest(purchase);
// Wait for user.
Console.ReadKey();
}
}
// Purchase event argument holds purchase info.
internal sealed class PurchaseEventArgs : EventArgs
{
public Purchase Purchase { get; set; }
}
/// <summary>
/// The 'Handler' abstract class.
/// </summary>
internal abstract class Approver
{
// Purchase event .
public event EventHandler<PurchaseEventArgs> Purchase;
// Purchase event handler.
public abstract void PurchaseHandler(Object sender, PurchaseEventArgs e);
// Constructor.
public Approver()
{
Purchase += PurchaseHandler;
}
public void ProcessRequest(Purchase purchase)
{
OnPurchase(new PurchaseEventArgs { Purchase = purchase });
}
// Invoke the Purchase event.
public virtual void OnPurchase(PurchaseEventArgs e)
{
if (Purchase != null)
{
Purchase(this, e);
}
}
// Sets or gets the next approver.
public Approver Successor { get; set; }
}
/// <summary>
/// The 'ConcreteHandler' class.
/// </summary>
internal sealed class Director : Approver
{
public override void PurchaseHandler(Object sender, PurchaseEventArgs e)
{
if (e.Purchase.Amount < 10000.0)
{
Console.WriteLine("{0} approved request# {1}",
this.GetType().Name, e.Purchase.Number);
}
else if (Successor != null)
{
Successor.PurchaseHandler(this, e);
}
}
}
/// <summary>
/// The 'ConcreteHandler' class.
/// </summary>
internal sealed class VicePresident : Approver
{
public override void PurchaseHandler(Object sender, PurchaseEventArgs e)
{
if (e.Purchase.Amount < 25000.0)
{
Console.WriteLine("{0} approved request# {1}",
this.GetType().Name, e.Purchase.Number);
}
else if (Successor != null)
{
Successor.PurchaseHandler(this, e);
}
}
}
/// <summary>
/// The 'ConcreteHandler' class.
/// </summary>
internal sealed class President : Approver
{
public override void PurchaseHandler(Object sender, PurchaseEventArgs e)
{
if (e.Purchase.Amount < 100000.0)
{
Console.WriteLine("{0} approved request# {1}",
sender.GetType().Name, e.Purchase.Number);
}
else if (Successor != null)
{
Successor.PurchaseHandler(this, e);
}
else
{
Console.WriteLine("Request# {0} requires an executive meeting!",
e.Purchase.Number);
}
}
}
/// <summary>
/// Class that holds request details.
/// </summary>
internal sealed class Purchase
{
public Double Amount { get; set; }
public String Purpose { get; set; }
public Int32 Number { get; set; }
}
Надежда, которая помогает.