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();
        }
    }
3
  • 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 Commented Nov 15, 2016 at 9: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 Commented Nov 15, 2016 at 9:11
  • @LordWilmore should i update my understanding of bridge pattern in this question, to have a clear understanding on what i am talking? Commented Nov 15, 2016 at 9:14

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

5 Comments

Then what is the difference between Decorator Pattern and Proxy Pattern? Decorator Pattern also does the same, right?
@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.
@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.
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
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.