Ejemplo n.º 1
0
        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");
                }
            }
        }
Ejemplo n.º 2
0
 //Method that set next handler in the pipeline
 public CurrencyBill RegisterNext(CurrencyBill currencyBill)
 {
     next = currencyBill;
     return(next);
 }
Ejemplo n.º 3
0
 //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();
 }