SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design PatternAdeel Riaz
 
Chain of responsibility
Chain of responsibilityChain of responsibility
Chain of responsibilityShakil Ahmed
 
Serialization & De-serialization in Java
Serialization & De-serialization in JavaSerialization & De-serialization in Java
Serialization & De-serialization in JavaInnovationM
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux IntroductionNikolaus Graf
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJSBrainhub
 
Ajax ppt - 32 slides
Ajax ppt - 32 slidesAjax ppt - 32 slides
Ajax ppt - 32 slidesSmithss25
 
Flyweight Design Pattern
Flyweight Design PatternFlyweight Design Pattern
Flyweight Design PatternVarun MC
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAMaulik Borsaniya
 
Introduction to Design Patterns and Singleton
Introduction to Design Patterns and SingletonIntroduction to Design Patterns and Singleton
Introduction to Design Patterns and SingletonJonathan Simon
 
Basics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdfBasics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdfKnoldus Inc.
 

Was ist angesagt? (20)

Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Iterator
IteratorIterator
Iterator
 
Chain of responsibility
Chain of responsibilityChain of responsibility
Chain of responsibility
 
Serialization & De-serialization in Java
Serialization & De-serialization in JavaSerialization & De-serialization in Java
Serialization & De-serialization in Java
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
 
Adapter pattern
Adapter patternAdapter pattern
Adapter pattern
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Ajax ppt - 32 slides
Ajax ppt - 32 slidesAjax ppt - 32 slides
Ajax ppt - 32 slides
 
Flyweight Design Pattern
Flyweight Design PatternFlyweight Design Pattern
Flyweight Design Pattern
 
Visitor pattern
Visitor patternVisitor pattern
Visitor pattern
 
Factory Method Pattern
Factory Method PatternFactory Method Pattern
Factory Method Pattern
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 
Observer Pattern
Observer PatternObserver Pattern
Observer Pattern
 
Flyweight pattern
Flyweight patternFlyweight pattern
Flyweight pattern
 
Introduction to Design Patterns and Singleton
Introduction to Design Patterns and SingletonIntroduction to Design Patterns and Singleton
Introduction to Design Patterns and Singleton
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
PHP - Introduction to PHP AJAX
PHP -  Introduction to PHP AJAXPHP -  Introduction to PHP AJAX
PHP - Introduction to PHP AJAX
 
Basics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdfBasics of React Hooks.pptx.pdf
Basics of React Hooks.pptx.pdf
 
Composite Design Pattern
Composite Design PatternComposite Design Pattern
Composite Design Pattern
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptxJonalynLegaspi2
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
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
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleCeline George
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 

Kürzlich hochgeladen (20)

Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptx
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
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
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 

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

Hinweis der Redaktion

  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.