The proxy design pattern provides a surrogate or placeholder for another object to control access to it. It works by adding an extra layer of indirection between clients and the real subject. This allows the proxy to perform additional tasks like lazy initialization, access control, caching and logging before forwarding the request to the real subject. There are different types of proxies like remote, virtual and protection proxies. The proxy pattern implementation in C# creates a proxy class that implements the same interface as the real subject and holds a reference to an instance of the real subject. The proxy forwards requests to the real subject while also performing other operations like access control.
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. 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.
http://en.wikipedia.org/wiki/Proxy_pattern
Proxy pattern is a structural design pattern.
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.
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. 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. 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. 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. 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. 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. 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.