Friday, June 7, 2013

Bridge Design Patterns in C#

Bridge Pattern: It is some kind of bridge between two different implementations. But this is not true. 
            This pattern is designed specifically to let the abstraction and the implementation vary independently.
     
     GoF defines Bridge Pattern as "Decouple an abstraction from its implementation so that the two can vary independently."
    

 class Program
    {
        static void Main(string[] args)
        {
            MySuperSmartTV myTV = new MySuperSmartTV();

            Console.WriteLine("Select a souce to get TV Guide and Play");
            Console.WriteLine("1.Local cable TV\n2.Local Dish TV\n3.IP TV");

            ConsoleKeyInfo input = Console.ReadKey();
            switch (input.KeyChar)
            {
                case '1':
                    myTV.VideoSource = new LocalCableTV();
                    break;
                case '2':
                    myTV.VideoSource = new LocalDishTV();
                    break;
                case '3':
                default:
                    myTV.VideoSource = new IPTVService();
                    break;
            }
            Console.WriteLine();
            myTV.ShowTVGuide();
            myTV.PlayTV();

            Console.Read();
        }
    }

    interface IVideoSource
    {
        string getTVGuide();
        string playVideo();
    }

    class LocalCableTV : IVideoSource
    {
        const string source_name = "Local cable TV";

        public string getTVGuide()
        {
            return string.Format("Getting TV Guid from {0}" , source_name);
        }

        public string playVideo()
        {
            return string.Format("Playing {0}" , source_name);
        }
    }
    class LocalDishTV : IVideoSource
    {
        const string souce_name = "Local DISH TV";
        public string getTVGuide()
        {
            return string.Format("Getting TV Guid from {0}" + souce_name);
        }

        public string playVideo()
        {
            return string.Format("Plahing {0}" + souce_name);
        }
    }
    class IPTVService : IVideoSource
    {
        const string source_name = "IP TV";
        public string getTVGuide()
        {
            return string.Format("Getting TV Guid from {0}" , source_name);
        }
        public string playVideo()
        {
            return string.Format("IP TV {0}" , source_name);
        }
    }

    class MySuperSmartTV
    {
        IVideoSource currentVideoSource = null;
        public IVideoSource VideoSource
        {
            get { return currentVideoSource; }
            set { currentVideoSource = value; }
        }

        public void ShowTVGuide()
        {
            if (currentVideoSource != null)
                Console.WriteLine(currentVideoSource.getTVGuide());
            else
                Console.WriteLine("Please select a Video source to get TV guide from");
        }
        public void PlayTV()
        {
            if (currentVideoSource != null)
                Console.WriteLine(currentVideoSource.playVideo());
            else
                Console.WriteLine("Please select a Video source to Play");
        }
    }

No comments:

Post a Comment