Friday, June 7, 2013

Bridge Design Pattern in c#

Bridge: Decouples an abstraction from its implementation so that the two can vary 
independently.

class Program
    {
        static void Main(string[] args)
        {
            //create a teacher and a director
            Teacher A = new Teacher();
            Director B = new Director();

            Kid Param = new Kid();
            Param.Name = "Paramesh";
            Param.Age = 5;
            Param.FavorFood = "Chicken";

            Kid Ram = new Kid();
            Ram.Name = "Ram";
            Ram.Age = 6;
            Ram.FavorFood = "Fish";

            //Teacher A starts talking with param
            Console.WriteLine("Mr.A starts talking to param");
            A.objToTalk = Param;
            A.StartChatting();
            Console.WriteLine();

            //Directory B starts talking with ram
            Console.WriteLine("Directory B starts taking to Ram");
            B.objToTalk = Ram;
            B.StartChatting();
            Console.WriteLine();

            //Teacher A starts talking with ram
            Console.WriteLine("Teacher A starts taking to Ram");
            A.objToTalk = Ram;
            A.StartChatting();
            Console.WriteLine();

            //Direcoty B starts talking with param
            Console.WriteLine("Directory A starts taking to param");
            B.objToTalk = Param;
            B.StartChatting();

            Console.Read();
        }
    }


    //it is going to be consumed by the Communicator as a bridge to talk to any 
    //ITalkable objects. And also all the ITalkable objects must provide the below activities as well.
    public interface ITalkable
    {
        void YourName();
        void YourAge();
        void YourFavorFood();
    }

    public class Kid : ITalkable
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string FavorFood { get; set; }

        public void YourName()
        {
            Console.WriteLine("My Name is {0}", Name);
        }

        public void YourAge()
        {
            Console.WriteLine("My Age is {0}", Age);
        }

        public void YourFavorFood()
        {
            Console.WriteLine("My Favor Food is {0}", FavorFood);
        }
    }

    public interface ICommunicator
    {
        //build a bridge between a Communicator and a talkable object (Kid)
        ITalkable objToTalk { get; set; }

        //start chatting process
        void StartChatting();
    }

    public class Teacher : ICommunicator
    {
        Kid aKid = new Kid();

        public ITalkable objToTalk
        {
            get
            {
                return aKid;
            }
            set
            {
                aKid=(Kid)value;
            }
        }

        //Implementing the chatting procedures.
        public void StartChatting()
        {
            Console.WriteLine("What's your name?");
            aKid.YourName();

            Console.WriteLine("How old are you?");
            aKid.YourAge();

            Console.WriteLine("What's your favor food?");
            aKid.YourFavorFood();

            //I use objToTalk as Kid object to use Name property here
            string name = aKid.Name;
        }
    }

    public class Director : ICommunicator
    {
        ITalkable aKid;
        public ITalkable objToTalk
        {
            get
            {
                return aKid;
            }
            set
            {
                aKid = value;
            }
        }

        //Director has different questions than teacher.
        public void StartChatting()
        {
            Console.WriteLine("Hi, can you tell me your name?");
            aKid.YourName();

            Console.WriteLine("Hi, can you tell me your age?");
            aKid.YourAge();
        }

    }

No comments:

Post a Comment