Author - StudySection Post Views - 37 views
Proxy Pattern

Explain this pattern with an example in C# – Proxy Pattern

In the proxy design pattern, the functionality of one class is represented by another class. This design pattern comes under the structural design pattern which deals with class and object structure. The concept of inheritance is used to define methods to create interfaces and compose objects to get new functionality.
flow-chart-09

As you can see in the following image, when the client wants to consume some methods of the real object, it has to pass through the proxy object. This means that the client will call the method of the proxy object and the proxy will be responsible for calling the method of the actual object.

Proxy Pattern:

flow-chart-009

  • Subject: This class provides an interface that both the actual class and the proxy class will implement. This way proxies can be easily used as a substitute for the actual subject.
  • Proxy: This class will be used by the applications and will expose the methods exposed by the topic. The application will use this class and this class will take care of talking to RealSubject internally and getting data to the local application.
  • RealSubject: It’s the actual object that has the actual logic to retrieve the data/functionality. This is the class that the proxy represents at the end of the application.

Code Implementation for Proxy Pattern:

public interface Subject
{
void PerformAction();
}

public class RealSubject: Subject
{
public void PerformAction()
{
Console.WriteLine(“RealSubject action performed.”);
}
}

public class Proxy: Subject
{
private RealSubject _realSubject;

public void PerformAction()
{
if (_realSubject == null)
_realSubject = new RealSubject();

_realSubject.PerformAction();
}
}

If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner-level and expert-level PHP Certification Exams are offered by StudySection along with other programming certification exams.

Leave a Reply

Your email address will not be published.