Add

Proxy Pattern

According to Proxy Pattern, if you want to be allowed to provide an interface to other objects by creating a wrapper class as the proxy then this pattern can be used.


Proxy Pattern Example


Here is an example of Proxy Pattern.



Proxy Pattern CD


    // Subject Interface
    public interface IImageEditor
    {

        void DrawImage();

    }

    // Real Subject
    class ImageEditor : IImageEditor
    {

        public void DrawImage()
        {

            Console.WriteLine("Draw() of ImageEditor has been called!");

        }

    }

    // Proxy
    class ImageEditorProxy : IImageEditor
    {

        private ImageEditor _realSubject;


        public void DrawImage()
        {

            // Here Lazy Intialization has been used

            if (_realSubject == null)
            {

                _realSubject = new ImageEditor();

            }

            _realSubject.DrawImage();

        }

    }


    class Program
    {
        static void Main(string[] args)
        {

            ImageEditorProxy proxy = new ImageEditorProxy();
            proxy.DrawImage();


            Console.ReadKey();

        }

    }



Proxy Pattern Output


Proxy Pattern Output