Friday, June 7, 2013

Facade Design Patterns in C#

Facade: Knows which subsystem classes are responsible for a request.
     * delegates client requests to appropriate subsystem objects.
     * 
     Subsystem classes   (Bank, Credit, Loan)
    implement subsystem functionality.
    handle work assigned by the Facade object.
    have no knowledge of the facade and keep no reference to it.


class Program
    {
        static void Main(string[] args)
        {
            Facade facade = new Facade();
            facade.MethodA();
            facade.MethodB();

            Console.ReadKey();
        }
    }

    //Subsystem Classes
    class SubSystemOne
    {
        public void MethodOne()
        {
            Console.WriteLine("Subsystem One");
        }
    }
    class SubSystemTwo
    {
        public void MethodTwo()
        {
            Console.WriteLine("Subsystem Two");
        }
    }
    class SubSystemThree
    {
        public void MethodThree()
        {
            Console.WriteLine("Sussystem Three");
        }
    }   
    

    //Facade Class
    class Facade
    {
        private SubSystemOne _one;
        private SubSystemTwo _two;
        private SubSystemThree _three;

        public Facade()
        {
            _one = new SubSystemOne();
            _two = new SubSystemTwo();
            _three = new SubSystemThree();
        }
        public void MethodA()
        {
            Console.WriteLine("\nMethodA");
            _one.MethodOne();
            _two.MethodTwo();
        }
        public void MethodB()
        {
            Console.WriteLine("\nMethodB");
            _three.MethodThree();
        }
    }

No comments:

Post a Comment