Friday, May 10, 2019

C# Design Pattern : Decorator.



Decorator design Pattern allows you to change objects behavior/functionality at run time. in other words you can decorate it. for example you have a window (consider for a moment that window doesn't have border feature inbuilt) and we want to decorate it with border.one way we can create a derived class of Window and override it but important point to remember here is that, we have an object, we are decorating object at run time we don't/can't want to change it's template( i.e.class). below example will show you how to change object at run time without changing it's interface. (copy-paste below code to visual studio/code to see comments properly).

 public interface IWindows
    {
        void Draw();
        void Maximize();
        void Minimize();
    }
    public class Windows : IWindows
    {
        public void Draw()
        {
            Console.WriteLine("Drawing Window ...");
        }

        public void Maximize()
        {
            Console.WriteLine("Windows Maximized");
        }

        public void Minimize()
        {
            Console.WriteLine("Windows Minimized");
        }
    }

    class WindowsBorderDecorator : IWindows
    {
        // As you are implementing Iwindows interface so, you have to implement all of it's methods.
        // but as you are getting Window Objects from client which has already implemented Iwindow,
        //it means now you are free to use exsting implementation or you can override any method, as per your wish.
        // As we want to draw border around window so we will override Draw() method.

        private IWindows windowToDecorate;

        public WindowsBorderDecorator(IWindows window)
        {
            windowToDecorate = window;
        }
        public  void Draw()
        {
            // changing the drawing logic..now ,drawing window with border.
            // it's our logic or situation weather we are able to use some functionality
            //from existing window object ('windowToDecorate')
            //or we want to write logic from scratech.

            SetBorderAroundWindow();
            Console.Write("Decorating window with Border!");

        }

        private void SetBorderAroundWindow()
        {
           //Additional logic as per your requirement;
        }

        public  void Maximize()
        {
            windowToDecorate.Maximize();
        }

        public virtual void Minimize()
        {
            windowToDecorate.Minimize();
        }
    }
    class DecoratorDesignPatternTest
    {
        public static void Main()
        {
            IWindows window = new Windows();
            window.Draw();// actaul implementation. OUTPUT:Drawing Window ...
            //--------------instead of above code you can use like below (Note: interface is same)---------
            //want to decorate it with  border.
            IWindows decoratedWindow= new WindowsBorderDecorator(window);
            decoratedWindow.Draw(); //new impelemetation with border. OUTPUT:Decorating window with Border!
            Console.Read();
        }
    }  

No comments:

Post a Comment

C# Record type: Something to remember while using record types

  Record in c# provide a concise and expressive way to create immutable data types, record is a keyword in c#, we can use this keyword with ...