2

Suppose if I am trying to access a method of a class through some other class like this

class SuperClass
    {
        public interface ISubject
        {
            void Print();
        }
        private class Subject : ISubject
        {
            public void Print()
            {
                Console.WriteLine("this is a print");
            }
        }
       public class Proxy {
            ISubject subject;
            public void CallOtherMethod() {
                subject = new Subject();
                subject.Print();
            }
        }
    }
    class Client: SuperClass
    {
        static void Main() {
            Proxy proxy = new Proxy();
            proxy.CallOtherMethod();
        }
    } 

is this called as a proxy Class? or does it Require to have a interface as an reference then we have to call the method? for instance like this

class SuperClass
    {
        public interface ISubject
        {
            void Print();
        }
        private class Subject : ISubject
        {
            public void Print()
            {
                Console.WriteLine("this is a print");
            }
        }
        public class Proxy : ISubject
        {
            ISubject subject;
            public void Print()
            {
                subject = new Subject();
                subject.Print();
            }
        }
    }
    class Client : SuperClass
    {
        static void Main()
        {
            ISubject proxy = new Proxy();
            proxy.Print();
        }
    }
Lijin Durairaj
  • 4,910
  • 15
  • 52
  • 85
  • 2
    With a proxy pattern you would pass an instance of `ISubject` to the `proxy.Print()` method, and then the `Proxy` may have extra guards in place, or other related functionality to call, e.g. notifiers. In your example `Proxy` should just be called `Printer` – LordWilmore Nov 15 '16 at 09:07
  • @LordWilmore then what is the difference between Proxy Pattern and Bridge Pattern? In Bridge Pattern we pass the instance of the interface to the abstract class – Lijin Durairaj Nov 15 '16 at 09:11
  • @LordWilmore should i update my understanding of bridge pattern in this question, to have a clear understanding on what i am talking? – Lijin Durairaj Nov 15 '16 at 09:14

1 Answers1

3

Usually, the Proxy pattern is aimed for Interception. That is, catching the call to some (or all) methods of some type and performing some operation before / after the actual call. To achieve that, the Proxy has to inherit from the target type.

For example:

public class Subject
{
    public virtual void Print()
    {
        Console.WriteLine("this is a print");
    }
}

public class SubjectProxy : Subject
{
    public override void Print()
    {
        Console.Write("Before calling base.Print()");
        base.Print();
        Console.Write("After calling base.Print()");
    }
}

Now, when at some point in your code you're expecting a Subject, you may actually get a SubjectProxy and still treat it as Subject:

public Subject GetSubject()
{
    return new SubjectProxy();
}

Subject subject = GetSubject();
subject.Print(); // would use the proxied method

(That's not to say that the only way to achieve interception is through inheritance. I presume there are Proxy flavors that use variations of composition / decoration to achieve that)

haim770
  • 48,394
  • 7
  • 105
  • 133
  • Then what is the difference between Decorator Pattern and Proxy Pattern? Decorator Pattern also does the same, right? – Lijin Durairaj Nov 15 '16 at 09:19
  • 1
    @LijinJohn, As I understand it. Decorator is used mainly to extend the functionality of an object, not to intercept its methods. Yet (as I already mentioned in the answer), there are ways to use decoration to achieve that too. But, if you really are aiming for interception, Proxy is the easiest way. – haim770 Nov 15 '16 at 09:22
  • @LijinJohn, Also, when using Decorator, the target object has to be aware of the decorating object and explicitly call it from within its own code. The Proxy keeps the target object oblivious and mostly un-touched, it only requires `virtual` on the to-be-proxied methods. – haim770 Nov 15 '16 at 09:26
  • so, is my first sample code a Proxy Pattern? If not then, if I Overload CallOtherMethod() with ISubject and calling the method Print Makes it a Proxy Pattern... Please be patient with me and explain... thank you – Lijin Durairaj Nov 15 '16 at 09:30
  • 1
    @LijinJohn, There are lots of misconceptions and contradicting information related to design patterns. Honestly, I don't think I can "certify" a certain design as a "valid proxy" or "valid X". The better approach would be to focus on what you're trying to achieve by introducing this extra class that wraps `Subject` and ask yourself why is it there. With that being said, calling `subject = new Subject()` *in* the method itself it probably not a good design, you better *inject* it into the method instead. – haim770 Nov 15 '16 at 09:37