static void Main(string[] args) { //create handlers var bills50s = new CurrencyBill(50, 1); var bills20s = new CurrencyBill(20, 2); var bills10s = new CurrencyBill(10, 5); //set handlers pipeline bills50s.RegisterNext(bills20s) .RegisterNext(bills10s); //client code that uses handler while (true) { Console.WriteLine("Please enter amount to dispense:"); var isParsed = int.TryParse(Console.ReadLine(), out var amount); if (isParsed) { //sender pass the request to first handler in the pipeline var isDepensible = bills50s.DispenseRequest(amount); if (isDepensible) { Console.WriteLine($"Your amount ${amount} is dispensable!"); } else { Console.WriteLine($"Failed to dispense ${amount}!"); } } else { Console.WriteLine("Please enter a valid amount to dispense"); } } }
//Method that set next handler in the pipeline public CurrencyBill RegisterNext(CurrencyBill currencyBill) { next = currencyBill; return(next); }
//A static contructor that initialize static Zero property //This property is used as default next handler instead of null object static CurrencyBill() { Zero = new ZeroCurrencyBill(); }