Proxy design pattern (Class Ambassador)


Published on

This presentation provide information to understand proxy design pattern, it’s structure and it’s implementation.

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Proxy design pattern (Class Ambassador)

  1. 1. Proxy Design Pattern Class Ambassador Sameer Singh Rathoud
  2. 2. About presentation This presentation provide information to understand proxy design pattern, it’s structure and it’s implementation. I have tried my best to explain the concept in very simple language. The programming language used for implementation is c#. But any one from different programming background can easily understand the implementation.
  3. 3. Definition Provide a surrogate or placeholder for another object to control access to it. A proxy, in its most general form, is a class functioning as an interface to something else. Proxy pattern is a structural design pattern.
  4. 4. Motivation and Intent You need to support resource-hungry objects, and you do not want to instantiate such objects unless and until they are actually requested by the client. • Provide a surrogate or placeholder for another object to control access to it. • Use an extra level of indirection to support distributed, controlled, or intelligent access. • Add a wrapper and delegation to protect the real component from undue complexity.
  5. 5. Structure << interface >> Subject Client Operation () Inheritance Real Subject Operation () Proxy Operation () realSubject.Operation()
  6. 6. Participants in structure • Subject (interface): is a interface implemented by real subject, but proxy should also implement this interface, so that where ever real subject is used, there proxy can be used. • Proxy: • Maintain the reference of the real subject, so that it can be accessed through proxy. • Implements the same interface implemented by real subject, so that it can be used as substitute of real subject. • Control access to the real subject and responsible for its creation and deletion. • Can have other responsibilities depends on the proxy. • Real Subject: The real logic that the proxy represents. • Client: The user of the proxy design pattern.
  7. 7. Collaborations • Proxy forwards requests to real subject when appropriate, depending on the kind of proxy. Proxy Client Real Subject new Proxy () Operation() new RealSubject () Operation()
  8. 8. Types • Remote proxies: are responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space. • Virtual proxies: creates expensive objects on demand. • Protection proxies: controls access to the original object. Protection proxies are useful when objects should have different access rights.
  9. 9. Implementation (C#) Subject (Interface) abstract class Subject { public abstract void Operation(); } Here “Subject” is an abstract class with an abstract method “Operation”. Now all the concrete classes implementing this abstract class will override “Operation” method. << interface >> Subject + Operation ()
  10. 10. Implementation (C#) Here the concrete classes “Proxy” and “RealSubject” are implementing ConcreteSubject: RealSubject and Proxy abstract class “Subject” and these concrete classes are overriding class RealSubject : Subject “Operation” method (giving class { public override void Operation() specific definition of function) of { “Subject” class. Additionally “Proxy” Console.WriteLine("Called RealSubject.Operation()"); contains the reference of “RealSubject” } class. } << interface >> Subject class Proxy : Subject { private RealSubject realSubject; public override void Operation() { if (realSubject == null) { realSubject = new RealSubject(); } realSubject.Operation(); } } + Operation () RealSubject Proxy - realSubject (RealSubject) Operation () Operation ()
  11. 11. Implementation (C#) Client class Client { static void Main(string[] args) { Proxy proxy = new Proxy(); proxy.Operation(); } } For using proxy pattern the client has to create a “Proxy” reference and this reference is used to call the “Operation”.
  12. 12. Example A smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed. Typical uses include • counting the number of references to the real object so that it can be freed automatically when there are no more references. • loading a persistent object into memory when it's first referenced. • checking that the real object is locked before it's accessed to ensure that no other object can change it. A check or an bank draft is another example of proxy pattern. Here check represents the fund in the account. A check can be used for making purchases and it is controlling the fund in the account.
  13. 13. End of Presentation . . .