2. Why Proxy Patern ?
• Let’s say , there is some system that is very complicated and we’ll make it
simple,
• And, lets say , we are only interested in some necessary parts , objects vs
not all of them
• And , lets say , every time , instantiating and initializing all objects needs a
lot of time , another words , costs a lot.
WHO DOWE CALL ?
3. Answer is PROXY
Provide a surrogate or placeholder for another object to control access to it.
4. Where exactly is the Proxy Pattern used ?
This pattern is recommended when either of the following scenarios occur in
your application:
• The object being represented is external to the system.
• Objects need to be created on demand.
• Access control for the original object is required
• Added functionality is required when an object is accessed.
5. Typically, you'll want to use a proxy when communication with a third party
is an expensive operation, perhaps over a network.The proxy would allow
you to hold your data until you are ready to commit, and can limit the
amount of times that the communication is called.
The proxy is also useful if you want to decouple actual implementation code
from the access to a particular library.
Proxy is also useful for access to large files, or graphics. By using a proxy,
you can delay loading the resource until you really need the data inside.
Without the concept of proxies, an application could be slow, and appear
non-responsive.
7. Proxy
Maintains a reference that lets the proxy access the real subject.
Provides an interface identical to subject's so that a proxy can by substituted
for the real subject.
Controls access to the real subject and may be responsible for creating and
deleting it.
8. Subject
Defines the common interface for Real Subject and Proxy so that a Proxy can
be used anywhere a Real Subject is expected.
Real Subject
Defines the real object that the proxy represents.
10. Applicable Situations
• Virtual Proxy :
Imagine a situation where there is multiple database call to extract huge size
image. Since this is an expensive operation we can possibly use the proxy pattern
which would create multiple proxies and point to the huge size memory
consuming object for further processing.The real object gets created only when a
client first requests/accesses the object and after that we can just refer to the
proxy to reuse the object.This avoids duplication of the object and hence saving
memory.
11. • Remote Proxy :
The remote proxy provides a local representation of the object which is
present in the different address location.An example can be providing
interface for remote resources such as web services.
• Protective Proxy :
The protective proxy acts as an authorisation layer to verify if the actual
user has access to appropriate content. An example can be thought about the
proxy server which provides restrictive internet access in office. Only the
websites and contents which are valid will be allowed and the remaining ones
will be blocked.
12. • Smart reference
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 (also called smart pointers)
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.
14. Virtual Proxy Example
• As mentioned earlier virtual proxy is useful to save expensive memory
resources. Let’s take a scenario where the real image contains a huge size
data which clients needs to access.To save our resources and memory the
implementation will be as below:
• We are going to create an Image interface and concrete classes
implementing the Image interface.
• ProxyImage is a a proxy class to reduce memory footprint of
RealImage object loading.
• ProxyPatternDemo, our demo class, will use ProxyImage to get
an Image object to load and display as it needs.
15.
16. Protection Proxy Example
• Let’s assume that companyABC starts a new policy that employees will now
be prohibited internet access based on their roles. All external emails
websites will be blocked. ( this is shown as an output like «user have internet
access» or «user dont have internet access» according to the rollNumber) . In
such situation we create InternetAccess interface which consists of
operation grantInternetAccess().
• The RealInternetAccess class which allows of internet access for all.
However to restrict this access we will use ProxyInternetAccess class which
will check user’s role and grant access based on their roles.