Upload
sameer-rathoud
View
1.333
Download
0
Tags:
Embed Size (px)
DESCRIPTION
This presentation provide information to understand proxy design pattern, it’s structure and it’s implementation.
Citation preview
Proxy Design PatternClass Ambassador
Sameer Singh Rathoud
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.
Definition
Provide a surrogate or placeholder for another object to control access to it.
Proxy pattern is a structural design pattern.
A proxy, in its most general form, is a class functioning as an interface to
something else.
http://en.wikipedia.org/wiki/Proxy_pattern
Motivation and Intent
• 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.
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.
Structure
Client
Inheritance
<< interface >>
Subject
Operation ()
Proxy
Operation ()
Real Subject
Operation ()realSubject.Operation()
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.
Collaborations
• Proxy forwards requests to real subject when appropriate, depending on the kind of proxy.
Client Proxy Real Subject
new Proxy ()
Operation() new RealSubject ()
Operation()
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.
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 abstractclass will override “Operation” method.
<< interface >> Subject
+ Operation ()
Implementation (C#)ConcreteSubject: RealSubject and Proxy
class RealSubject : Subject
{
public override void Operation()
{
Console.WriteLine("Called RealSubject.Operation()");
}
}
class Proxy : Subject
{
private RealSubject realSubject;
public override void Operation()
{
if (realSubject == null)
{
realSubject = new RealSubject();
}
realSubject.Operation();
}
}
Here the concrete classes “Proxy” and
“RealSubject” are implementing
abstract class “Subject” and these
concrete classes are overriding“Operation” method (giving class
specific definition of function) of“Subject” class. Additionally “Proxy”
contains the reference of “RealSubject”
class.
<< interface >> Subject
+ Operation ()
RealSubject
Operation ()
Proxy
Operation ()
- realSubject (RealSubject)
Implementation (C#)Client
class Client
{
static void Main(string[] args)
{
Proxy proxy = new Proxy();
proxy.Operation();
}
}
For using proxy pattern the client has tocreate a “Proxy” reference and this
reference is used to call the “Operation”.
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.
End of Presentation . . .