SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

Tendances (20)

Adapter pattern
Adapter patternAdapter pattern
Adapter pattern
 
Design Pattern - Chain of Responsibility
Design Pattern - Chain of ResponsibilityDesign Pattern - Chain of Responsibility
Design Pattern - Chain of Responsibility
 
Observer Software Design Pattern
Observer Software Design Pattern Observer Software Design Pattern
Observer Software Design Pattern
 
Memento pattern
Memento patternMemento pattern
Memento pattern
 
Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
 
Builder pattern
Builder patternBuilder pattern
Builder pattern
 
Prototype pattern
Prototype patternPrototype pattern
Prototype pattern
 
Bridge Design Pattern
Bridge Design PatternBridge Design Pattern
Bridge Design Pattern
 
Command Pattern
Command PatternCommand Pattern
Command Pattern
 
Facade pattern
Facade patternFacade pattern
Facade pattern
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design Pattern
 
Chain of responsibility
Chain of responsibilityChain of responsibility
Chain of responsibility
 
Java RMI
Java RMIJava RMI
Java RMI
 
The Singleton Pattern Presentation
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern Presentation
 
Composite pattern
Composite patternComposite pattern
Composite pattern
 
Decorator design pattern
Decorator design patternDecorator design pattern
Decorator design pattern
 
Facade Pattern
Facade PatternFacade Pattern
Facade Pattern
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Unit iv
Unit ivUnit iv
Unit iv
 

Similaire à 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
 

Similaire à 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
 
Proxy pattern
Proxy patternProxy pattern
Proxy 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
 

Dernier

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 

Dernier (20)

Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

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

Notes de l'éditeur

  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.