SlideShare una empresa de Scribd logo
1 de 25
Proxy Design pattern
Control the access to your objects
Motivation
 The need to control the access to a certain object
Intent
 The Proxy pattern provides a surrogate or placeholder for another
object to control access to it
 This ability to control the access to an object can be required for a
variety of reasons:
 To hide information about the real object to the client

 To perform optimization like on demand loading
 To do additional house-keeping job like audit tasks
 Proxy design pattern is also known as surrogate design pattern
Context
Sometimes a client object may not be able to access a service provider
object (also referred to as a target object) by normal means. This could
happen for a variety of reasons depending on:

 The location of the target object
 The target object may be present in a different address space in the same or a
different computer.

 The state of existence of the target object
 The target object may not exist until it is actually needed to render a service or
the object may be in a compressed form.

 Special Behavior
 The target object may offer or deny services based on the access privileges of its
client objects. Some service provider objects may need special consideration
when used in a multithreaded environment
Solution
In such cases, the Proxy pattern suggests using a separate object referred to
as a proxy to provide a means for different client objects to access the target
object in a normal, straightforward manner.
 The Proxy object offers the same interface as the target object.
 The Proxy object interacts with the target object on behalf of a client
object and takes care of the specific details of communicating with the
target object
 Client objects need not even know that they are dealing with Proxy for the
original object.

 Proxy object serves as a transparent bridge between the client and an
inaccessible remote object or an object whose instantiation may have
been deferred.
Implementation
 UML class diagram for the Proxy Pattern
Implementation
The participants classes in the proxy pattern are:
 Subject - Interface implemented by the RealSubject and representing its
services. The interface must be implemented by the proxy as well so that
the proxy can be used in any location where the RealSubject can be
used
 Proxy
 Maintains a reference that allows the Proxy to access the RealSubject.
 Implements the same interface implemented by the RealSubject so that the
Proxy can be substituted for the RealSubject.
 Controls access to the RealSubject and may be responsible for its creation and
deletion.
 Other responsibilities depend on the kind of proxy.

 RealSubject - the real object that the proxy represents
Applicability
 Proxies are useful wherever there is a need for a more sophisticated
reference to a object than a simple pointer or simple reference can
provide
 Use the Proxy Pattern to create a representative object that controls
access to another object, which may be remote, expensive to
create or in need of securing
 There are several use-case scenarios that are often repeatable in
practice
Applicability
 Remote Proxy
 The remote proxy provides a local representation of the object which is present
in the different address location

 Virtual Proxy
 delaying the creation and initialization of expensive objects until needed, where
the objects are created on demand

 Protection Proxy
 The protective proxy acts as an authorization layer to verify if the actual user has
access to appropriate content

 Smart Reference
 provides additional actions whenever a subject is referenced, such as counting
the number of references to an object
Applicability
 Firewall Proxy
 controls access to a set of network resources, protecting the subject from “bad”
clients

 Caching Proxy
 Provides temporary storage for results of operations that are expensive. It can
also allow multiple clients to share the results to reduce computation or network
latency

 Synchronization Proxy
 provides safe access to a subject from multiple threads

 Copy-On-Write Proxy
 controls the copying of an object by deferring the copying of an object until it is
required by a client. This is a variant of the Virtual Proxy

 Complexity Hiding Proxy
Example: Remote Proxy
 Solves the problem when the real subject is a remote object. A remote
object is object that exists outside of the current Java Virtual Machine
whether it be in another JVM process on the same machine or a far
machine accessible by network.
 The Remote Proxy pattern enables communication with minimal
modification to existing code
 The Remote Proxy acts as a local representative to the remote object
 The client call methods of the proxy
 The proxy forwards the calls to the remote object
 To the client, it appears as though it is communicating directly with the remote
object
Example: Remote Proxy
Example: Remote Proxy
public interface ISubject {
public String doHeavyTask(String arguments);
}
public class RealSubject implements ISubject{

@Override
public String doHeavyTask(String arguments) {
//do the heavy task..
//generate results
String results = new String("Results");
return results;
}
}
Example: Remote Proxy
public class Proxy implements ISubject{
@Override
public String doHeavyTask(String arguments) {
//start of the stub routine
//pack arguments and generate the request..
//send the request and wait for the response
//unpack results from the response
String results = new String("Results");
//end of the stub routine
return results;
}
}
Example: Remote Proxy
public class Skeleton {
/*
* */
public void receiveRequest(String request){
//unpack the request
//object?
RealSubject subject = new RealSubject();
//method? arguments?
String result = subject.doHeavyTask("arguments");
//pack response with the result
//send the response
}
}
Example: Remote Proxy
Example: Virtual Proxy
 Solves the problems that arises while working directly with objects
that are expensive to create, initialize and maintain in concern with
time or memory consumption
 In this situations the Virtual Proxy:
 acts as a representative for an object that may be expensive to create
 often defers the creation of the object until it is needed
 also acts as a surrogate for the object before and while it is being
created. After that, the proxy delegates requests directly to the
RealSubject
Example: Virtual Proxy
Example: Virtual Proxy
Example: Virtual Proxy
class ImageProxy implements Icon {
ImageIcon imageIcon;
public int getIconWidth() {
if (imageIcon != null) {
return imageIcon.getIconWidth();
} else {
return 800;
}
}
public int getIconHeight() {
if (imageIcon != null) {
return imageIcon.getIconHeight();
} else {
return 600;
}
}
}
Example: Virtual Proxy
public void paintIcon(final Component c, Graphics g, int x, int y) {
if (imageIcon != null) {
imageIcon.paintIcon(c, g, x, y);
} else {
g.drawString("Loading CD cover, please wait...", x+300, y+190);
if (!retrieving) {
retrieving = true;
retrievalThread = new Thread(new Runnable() {
public void run() {
try {
imageIcon = new ImageIcon(imageURL, "CD Cover");
c.repaint();
} catch (Exception e) {}
}
});
retrievalThread.start();
}
}
}
Example: Virtual Proxy
 Refactoring the code:
URL url = new URL("http://images.amazon.com/images/some_image.jpg");
Icon icon = new ImageIcon(url);
Icon icon = new ImageProxy(url);
int iconHeight = icon.getIconHeight();
int iconWidth = icon.getIconWidth();
icon.paintIcon(c, g, x, y);
Related patterns
Many design patterns can have similar or exactly same structure but
they still differ from each other in their intent.
 Adapter design pattern
 Adapter provides a different interface to the object it adapts and
enables the client to use it to interact with it, while proxy provides the
same interface as the subject

 Decorator design pattern
 A decorator implementation can be the same as the proxy however a
decorator adds responsibilities to an object while a proxy controls access
to it
Questions
“

Thank you
for your attention

”

Sashe Klechkovski
December, 2013

Más contenido relacionado

La actualidad más candente

The Singleton Pattern Presentation
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern PresentationJAINIK PATEL
 
Observer design pattern
Observer design patternObserver design pattern
Observer design patternSara Torkey
 
Design Patterns Presentation - Chetan Gole
Design Patterns Presentation -  Chetan GoleDesign Patterns Presentation -  Chetan Gole
Design Patterns Presentation - Chetan GoleChetan Gole
 
Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternMudasir Qazi
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design PatternAdeel Riaz
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern11prasoon
 
Bridge pattern for Dummies
Bridge pattern for DummiesBridge pattern for Dummies
Bridge pattern for DummiesTaekSoon Jang
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Androidma-polimi
 
Design pattern & categories
Design pattern & categoriesDesign pattern & categories
Design pattern & categoriesHimanshu
 
PATTERNS02 - Creational Design Patterns
PATTERNS02 - Creational Design PatternsPATTERNS02 - Creational Design Patterns
PATTERNS02 - Creational Design PatternsMichael Heron
 

La actualidad más candente (20)

Prototype pattern
Prototype patternPrototype pattern
Prototype pattern
 
Composite Design Pattern
Composite Design PatternComposite Design Pattern
Composite Design Pattern
 
Composite pattern
Composite patternComposite pattern
Composite pattern
 
The Singleton Pattern Presentation
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern Presentation
 
Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
 
jQuery
jQueryjQuery
jQuery
 
Design Patterns Presentation - Chetan Gole
Design Patterns Presentation -  Chetan GoleDesign Patterns Presentation -  Chetan Gole
Design Patterns Presentation - Chetan Gole
 
Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory Pattern
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
 
Flyweight pattern
Flyweight patternFlyweight pattern
Flyweight pattern
 
Factory Method Pattern
Factory Method PatternFactory Method Pattern
Factory Method Pattern
 
Xml parsers
Xml parsersXml parsers
Xml parsers
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
 
Bridge pattern for Dummies
Bridge pattern for DummiesBridge pattern for Dummies
Bridge pattern for Dummies
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Android
 
Design pattern & categories
Design pattern & categoriesDesign pattern & categories
Design pattern & categories
 
Composite design pattern
Composite design patternComposite design pattern
Composite design pattern
 
PATTERNS02 - Creational Design Patterns
PATTERNS02 - Creational Design PatternsPATTERNS02 - Creational Design Patterns
PATTERNS02 - Creational Design Patterns
 
Adapter pattern
Adapter patternAdapter pattern
Adapter pattern
 

Similar a Control Access to Objects with Proxy Design Pattern

Gang of four Proxy Design Pattern
 Gang of four Proxy Design Pattern Gang of four Proxy Design Pattern
Gang of four Proxy Design PatternMainak Goswami
 
Design pattern proxy介紹 20130805
Design pattern proxy介紹 20130805Design pattern proxy介紹 20130805
Design pattern proxy介紹 20130805LearningTech
 
Design pattern proxy介紹 20130805
Design pattern proxy介紹 20130805Design pattern proxy介紹 20130805
Design pattern proxy介紹 20130805LearningTech
 
JAVA design patterns and Basic OOp concepts
JAVA design patterns and Basic OOp conceptsJAVA design patterns and Basic OOp concepts
JAVA design patterns and Basic OOp conceptsRahul Malhotra
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method InvocationSabiha M
 
INTRODUCTION TO CLIENT SIDE PROGRAMMING
INTRODUCTION TO CLIENT SIDE PROGRAMMINGINTRODUCTION TO CLIENT SIDE PROGRAMMING
INTRODUCTION TO CLIENT SIDE PROGRAMMINGProf Ansari
 
Dependency Injection, Zend Framework and Symfony Container
Dependency Injection, Zend Framework and Symfony ContainerDependency Injection, Zend Framework and Symfony Container
Dependency Injection, Zend Framework and Symfony ContainerDiego Lewin
 
Diving in the Flex Data Binding Waters
Diving in the Flex Data Binding WatersDiving in the Flex Data Binding Waters
Diving in the Flex Data Binding Watersmichael.labriola
 
Overview of Microsoft .Net Remoting technology
Overview of Microsoft .Net Remoting technologyOverview of Microsoft .Net Remoting technology
Overview of Microsoft .Net Remoting technologyPeter R. Egli
 
Factory method, strategy pattern & chain of responsibilities
Factory method, strategy pattern & chain of responsibilitiesFactory method, strategy pattern & chain of responsibilities
Factory method, strategy pattern & chain of responsibilitiesbabak danyal
 
Decomposing the Monolith using modern-day .NET and a touch of microservices
Decomposing the Monolith using modern-day .NET and a touch of microservicesDecomposing the Monolith using modern-day .NET and a touch of microservices
Decomposing the Monolith using modern-day .NET and a touch of microservicesDennis Doomen
 
My 70-480 HTML5 certification learning
My 70-480 HTML5 certification learningMy 70-480 HTML5 certification learning
My 70-480 HTML5 certification learningSyed Irtaza Ali
 
SCWCD : Web tier design CHAP : 11
SCWCD : Web tier design CHAP : 11SCWCD : Web tier design CHAP : 11
SCWCD : Web tier design CHAP : 11Ben Abdallah Helmi
 
Developing your first application using FIWARE
Developing your first application using FIWAREDeveloping your first application using FIWARE
Developing your first application using FIWAREFIWARE
 
Design Patterns - Part 2 of 2
Design Patterns - Part 2 of 2Design Patterns - Part 2 of 2
Design Patterns - Part 2 of 2Savio Sebastian
 
WPF - Controls & Data
WPF - Controls & DataWPF - Controls & Data
WPF - Controls & DataSharada Gururaj
 
A serverless IoT Story From Design to Production and Monitoring
A serverless IoT Story From Design to Production and MonitoringA serverless IoT Story From Design to Production and Monitoring
A serverless IoT Story From Design to Production and MonitoringMoaid Hathot
 

Similar a Control Access to Objects with Proxy Design Pattern (20)

Gang of four Proxy Design Pattern
 Gang of four Proxy Design Pattern Gang of four Proxy Design Pattern
Gang of four Proxy Design Pattern
 
Design pattern proxy介紹 20130805
Design pattern proxy介紹 20130805Design pattern proxy介紹 20130805
Design pattern proxy介紹 20130805
 
Design pattern proxy介紹 20130805
Design pattern proxy介紹 20130805Design pattern proxy介紹 20130805
Design pattern proxy介紹 20130805
 
RMI (Remote Method Invocation)
RMI (Remote Method Invocation)RMI (Remote Method Invocation)
RMI (Remote Method Invocation)
 
JAVA design patterns and Basic OOp concepts
JAVA design patterns and Basic OOp conceptsJAVA design patterns and Basic OOp concepts
JAVA design patterns and Basic OOp concepts
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
 
INTRODUCTION TO CLIENT SIDE PROGRAMMING
INTRODUCTION TO CLIENT SIDE PROGRAMMINGINTRODUCTION TO CLIENT SIDE PROGRAMMING
INTRODUCTION TO CLIENT SIDE PROGRAMMING
 
Dependency Injection, Zend Framework and Symfony Container
Dependency Injection, Zend Framework and Symfony ContainerDependency Injection, Zend Framework and Symfony Container
Dependency Injection, Zend Framework and Symfony Container
 
Diving in the Flex Data Binding Waters
Diving in the Flex Data Binding WatersDiving in the Flex Data Binding Waters
Diving in the Flex Data Binding Waters
 
Overview of Microsoft .Net Remoting technology
Overview of Microsoft .Net Remoting technologyOverview of Microsoft .Net Remoting technology
Overview of Microsoft .Net Remoting technology
 
Factory method, strategy pattern & chain of responsibilities
Factory method, strategy pattern & chain of responsibilitiesFactory method, strategy pattern & chain of responsibilities
Factory method, strategy pattern & chain of responsibilities
 
Decomposing the Monolith using modern-day .NET and a touch of microservices
Decomposing the Monolith using modern-day .NET and a touch of microservicesDecomposing the Monolith using modern-day .NET and a touch of microservices
Decomposing the Monolith using modern-day .NET and a touch of microservices
 
My 70-480 HTML5 certification learning
My 70-480 HTML5 certification learningMy 70-480 HTML5 certification learning
My 70-480 HTML5 certification learning
 
Proxy pattern
Proxy patternProxy pattern
Proxy pattern
 
Proxy pattern
Proxy patternProxy pattern
Proxy pattern
 
SCWCD : Web tier design CHAP : 11
SCWCD : Web tier design CHAP : 11SCWCD : Web tier design CHAP : 11
SCWCD : Web tier design CHAP : 11
 
Developing your first application using FIWARE
Developing your first application using FIWAREDeveloping your first application using FIWARE
Developing your first application using FIWARE
 
Design Patterns - Part 2 of 2
Design Patterns - Part 2 of 2Design Patterns - Part 2 of 2
Design Patterns - Part 2 of 2
 
WPF - Controls & Data
WPF - Controls & DataWPF - Controls & Data
WPF - Controls & Data
 
A serverless IoT Story From Design to Production and Monitoring
A serverless IoT Story From Design to Production and MonitoringA serverless IoT Story From Design to Production and Monitoring
A serverless IoT Story From Design to Production and Monitoring
 

Último

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 

Último (20)

Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 

Control Access to Objects with Proxy Design Pattern

  • 1. Proxy Design pattern Control the access to your objects
  • 2. Motivation  The need to control the access to a certain object
  • 3. Intent  The Proxy pattern provides a surrogate or placeholder for another object to control access to it  This ability to control the access to an object can be required for a variety of reasons:  To hide information about the real object to the client  To perform optimization like on demand loading  To do additional house-keeping job like audit tasks  Proxy design pattern is also known as surrogate design pattern
  • 4. Context Sometimes a client object may not be able to access a service provider object (also referred to as a target object) by normal means. This could happen for a variety of reasons depending on:  The location of the target object  The target object may be present in a different address space in the same or a different computer.  The state of existence of the target object  The target object may not exist until it is actually needed to render a service or the object may be in a compressed form.  Special Behavior  The target object may offer or deny services based on the access privileges of its client objects. Some service provider objects may need special consideration when used in a multithreaded environment
  • 5. Solution In such cases, the Proxy pattern suggests using a separate object referred to as a proxy to provide a means for different client objects to access the target object in a normal, straightforward manner.  The Proxy object offers the same interface as the target object.  The Proxy object interacts with the target object on behalf of a client object and takes care of the specific details of communicating with the target object  Client objects need not even know that they are dealing with Proxy for the original object.  Proxy object serves as a transparent bridge between the client and an inaccessible remote object or an object whose instantiation may have been deferred.
  • 6. Implementation  UML class diagram for the Proxy Pattern
  • 7. Implementation The participants classes in the proxy pattern are:  Subject - Interface implemented by the RealSubject and representing its services. The interface must be implemented by the proxy as well so that the proxy can be used in any location where the RealSubject can be used  Proxy  Maintains a reference that allows the Proxy to access the RealSubject.  Implements the same interface implemented by the RealSubject so that the Proxy can be substituted for the RealSubject.  Controls access to the RealSubject and may be responsible for its creation and deletion.  Other responsibilities depend on the kind of proxy.  RealSubject - the real object that the proxy represents
  • 8. Applicability  Proxies are useful wherever there is a need for a more sophisticated reference to a object than a simple pointer or simple reference can provide  Use the Proxy Pattern to create a representative object that controls access to another object, which may be remote, expensive to create or in need of securing  There are several use-case scenarios that are often repeatable in practice
  • 9. Applicability  Remote Proxy  The remote proxy provides a local representation of the object which is present in the different address location  Virtual Proxy  delaying the creation and initialization of expensive objects until needed, where the objects are created on demand  Protection Proxy  The protective proxy acts as an authorization layer to verify if the actual user has access to appropriate content  Smart Reference  provides additional actions whenever a subject is referenced, such as counting the number of references to an object
  • 10. Applicability  Firewall Proxy  controls access to a set of network resources, protecting the subject from “bad” clients  Caching Proxy  Provides temporary storage for results of operations that are expensive. It can also allow multiple clients to share the results to reduce computation or network latency  Synchronization Proxy  provides safe access to a subject from multiple threads  Copy-On-Write Proxy  controls the copying of an object by deferring the copying of an object until it is required by a client. This is a variant of the Virtual Proxy  Complexity Hiding Proxy
  • 11. Example: Remote Proxy  Solves the problem when the real subject is a remote object. A remote object is object that exists outside of the current Java Virtual Machine whether it be in another JVM process on the same machine or a far machine accessible by network.  The Remote Proxy pattern enables communication with minimal modification to existing code  The Remote Proxy acts as a local representative to the remote object  The client call methods of the proxy  The proxy forwards the calls to the remote object  To the client, it appears as though it is communicating directly with the remote object
  • 13. Example: Remote Proxy public interface ISubject { public String doHeavyTask(String arguments); } public class RealSubject implements ISubject{ @Override public String doHeavyTask(String arguments) { //do the heavy task.. //generate results String results = new String("Results"); return results; } }
  • 14. Example: Remote Proxy public class Proxy implements ISubject{ @Override public String doHeavyTask(String arguments) { //start of the stub routine //pack arguments and generate the request.. //send the request and wait for the response //unpack results from the response String results = new String("Results"); //end of the stub routine return results; } }
  • 15. Example: Remote Proxy public class Skeleton { /* * */ public void receiveRequest(String request){ //unpack the request //object? RealSubject subject = new RealSubject(); //method? arguments? String result = subject.doHeavyTask("arguments"); //pack response with the result //send the response } }
  • 17. Example: Virtual Proxy  Solves the problems that arises while working directly with objects that are expensive to create, initialize and maintain in concern with time or memory consumption  In this situations the Virtual Proxy:  acts as a representative for an object that may be expensive to create  often defers the creation of the object until it is needed  also acts as a surrogate for the object before and while it is being created. After that, the proxy delegates requests directly to the RealSubject
  • 20. Example: Virtual Proxy class ImageProxy implements Icon { ImageIcon imageIcon; public int getIconWidth() { if (imageIcon != null) { return imageIcon.getIconWidth(); } else { return 800; } } public int getIconHeight() { if (imageIcon != null) { return imageIcon.getIconHeight(); } else { return 600; } } }
  • 21. Example: Virtual Proxy public void paintIcon(final Component c, Graphics g, int x, int y) { if (imageIcon != null) { imageIcon.paintIcon(c, g, x, y); } else { g.drawString("Loading CD cover, please wait...", x+300, y+190); if (!retrieving) { retrieving = true; retrievalThread = new Thread(new Runnable() { public void run() { try { imageIcon = new ImageIcon(imageURL, "CD Cover"); c.repaint(); } catch (Exception e) {} } }); retrievalThread.start(); } } }
  • 22. Example: Virtual Proxy  Refactoring the code: URL url = new URL("http://images.amazon.com/images/some_image.jpg"); Icon icon = new ImageIcon(url); Icon icon = new ImageProxy(url); int iconHeight = icon.getIconHeight(); int iconWidth = icon.getIconWidth(); icon.paintIcon(c, g, x, y);
  • 23. Related patterns Many design patterns can have similar or exactly same structure but they still differ from each other in their intent.  Adapter design pattern  Adapter provides a different interface to the object it adapts and enables the client to use it to interact with it, while proxy provides the same interface as the subject  Decorator design pattern  A decorator implementation can be the same as the proxy however a decorator adds responsibilities to an object while a proxy controls access to it
  • 25. “ Thank you for your attention ” Sashe Klechkovski December, 2013

Notas del editor

  1. Ponekogas sejavuvapotrebata da kontroliramenekoj process kojtreba da se odvivaili da kontroliramepristap do nekojobjekt. Tukaimameednaslikakojaduri I kajnas e poznata, odnosnonanasitebabi I dedovci bi mu biladobropoznatadokolkugiprasame a toa e kakoodelprocesotnazapoznavanje I sklucuvanjenabrakporano. Slikataopisuva scenario nabracnointervjupomegumladozenecot I nevestatakoe se odvivaprekunekojatetkananevestatakakomedijatorilistrojnikkako so e poznatokajnas
  2. Ponekogas sejavuvapotrebata da kontroliramenekoj process kojtreba da se odvivaili da kontroliramepristap do nekojobjekt. Tukaimameednaslikakojaduri I kajnas e poznata, odnosnonanasitebabi I dedovci bi mu biladobropoznatadokolkugiprasame a toa e kakoodelprocesotnazapoznavanje I sklucuvanjenabrakporano. Slikataopisuva scenario nabracnointervjupomegumladozenecot I nevestatakoe se odvivaprekunekojatetkananevestatakakomedijatorilistrojnikkako so e poznatokajnas
  3. Proxy pattniovozmozuvakreiranje I predavanjenasurogatobjektnamestorealniotobjektilike go enkapsuliravistinskiot object za da ovozmozikontrolanapristapotkonnego.
  4. Koga bi bilakorisnaupotrebatana Proxy
  5. Complexity Hiding Proxy hides the complexity of and controls access to a complex set of classes. This is sometimes called the Facade Proxy for obvious reasons. The Complexity Hiding Proxy differs from the Facade Pattern in that the proxy controls access, while the Façade Pattern just provides an alternative interface.
  6. Slikata sketch 2 poednostavnatacistozapodsetuvanje. Vovedvoproblemotna Virtual proxy.
  7. Nakratkodekapostojatdrugi proxy implementacii koi resavaatdrugi problem. Vovedzosto Proxy nalikuvananekoidrugipaterni.