Friday, June 7, 2013

Facade design pattern in c#

Facade design pattern:
     * The facade pattern is a stuctural design pattern that is used to simplify access to functionality in complex designed subsystems. 
     * **The facade class provides a simple, interface that hides the implementation details of the underlying code.
     * It create relationship between classes and enities.
     * 
     * One example use of the facade pattern is for ATM system.ATM System shows only interface and hides all implmentation details.
     * ATM perform various operation like Withdraw,Tranfer etc but user can't see the what code written behind the interface.

     * 
     * Facade:This class contains the set of simple functions that are made available to its users and that hide the complexities of the difficult-to-use subsystems.
     * Class: These classes contain the functionality that is being presented via the facade.


 class Program
    {
        static void Main(string[] args)
        {
            BankOperations bank = new BankOperations();
            bank.FacedBankOperations();
            Console.ReadKey();
        }
    }
    public class WithDraw   //subsystem class
    {
        public void WithDrawAmount(int WDmoney, int Bal)
        {
            int balance;
            balance = Bal - WDmoney;
            Console.WriteLine("Successfully withdrow");
            Console.WriteLine("Balance is {0}", balance);
        }
    }
    public class Diposite //Subsystem class
    {
        public void DipositeAmount(int DPmoney, int Bal)
        {
            int balance;
            balance = Bal + DPmoney;
            Console.WriteLine("Successfully diposited");
            Console.WriteLine("Balance is {0}", balance);
        }
    }


    //Facade class
    public class BankOperations
    {
        public void FacedBankOperations()
        {
            string Operation;
            int balance = 5000;
            Console.WriteLine("Enter the choice");
            Operation = Console.ReadLine();
            switch (Operation)
            { 
                case "Withdraw":
                    WithDraw withdraw = new WithDraw();
                    Console.WriteLine("Enter the amount For Withdrow");
                    int wdAmount = Console.Read();
                    withdraw.WithDrawAmount(wdAmount, balance);
                    break;
                case "Deposite":
                    Diposite diposite = new Diposite();
                    Console.WriteLine("Enter the amount For Deposite");
                    int dpAmount = Console.Read();
                    diposite.DipositeAmount(dpAmount, balance);
                    break;
            }
        }
    }

No comments:

Post a Comment