SlideShare a Scribd company logo
Proxy & adapter pattern
2
3
 Name: Adapter
 Intent: Convert the interface of a class into another
interface clients expect. Adapter lets classes work
together that couldn't otherwise because of
incompatible interfaces.
 Problem: A system has the right data and behavior but
the wrong interface. Typically used when you have to
make something a derivative of an abstract class.
 Solution: The Adapter provides a wrapper with the
desired interface.
4
5
 Target
 defines the domain-specific interface that Client uses.
 Client
 collaborates with objects conforming to the Target
interface.
 Adaptee
 defines an existing interface that needs adapting.
 Adapter
 adapts the interface of Adaptee to the Target interface.
6
 You are given a task to:
 Create classes for squares and Rectangles that have the
behavior "display " and "color ".
 The client objects should not have to know whether they
actually have a square or Rectangle. They just want to
know that they have one of these shapes.
7
8
 Suppose you are now asked to implement a circle, a
new kind of Shape. To do this, You will want to create a
new class Circle that implements the shape "circle"
and derive it from the Shape class so that you can still
get polymorphic behavior.
 …but you remember you have already implemented
Circle…a few months ago!!!
9
10
11
public interface Shape{
public void displayShape();
public void colorShape();
}
class Square implements Shape{
public void displayShape(){ System.out.println("Displaying Square");}
public void colorShape(){ System.out.println("Coloring Square");} }
class Rectangle implements Shape{
public void displayShape(){System.out.println("Displaying
Rectangle");}
public void colorShape(){System.out.println("Coloring Rectangle");} }
12
class MyCircle{
public void displayCircle(){ System.out.println("Displaying Circle"); }
public void colorCircle(){ System.out.println("Coloring Circle");}
}
class Circle implements Shape{
private MyCircle mycir;
public Circle(){mycir = new MyCircle(); }
public void displayShape(){ mycir.displayCircle(); }
public void colorShape(){ mycir.colorCircle(); }
}
13
class Client{
public static void main(String[]args){
Shape s = new Circle();
s.displayShape();
s.colorShape();
}
}
14
15
 Name: Proxy
 Intent: Provide a surrogate or placeholder for another
object to control access to it.
 Problem: You want to control the access to an object
for different reasons. You may want to delay the
creation / initialization of expensive objects or you
may want to provide a local representation of a remote
object.
 Solution: Provide a Stub / placeholder for actual
object.
16
17
 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.
 RealSubject- the real object that the proxy represents.
18
 Consider an image viewer program that lists and
displays high resolution photos. The program has to
show a list of all photos however it does not need to
display the actual photo until the user selects an image
item from a list.
19
20
public interface Image{
public void showImage();
}
class HRImage implements Image{
public HRImage(){
System.out.println("loading a High Resolution image");
}
public void showImage(){
System.out.println("Showing a High Resolution Image");
}
}
21
class ProxyImage implements Image{
private Image proxyImage;
public ProxyImage(){
System.out.println("Loading a proxy image");}
public void showImage(){
proxyImage = (HRImage)new HRImage();
proxyImage.showImage();} }
class ImageViewer{
public static void main(String[]args){
Image HRImage1 = new ProxyImage();
Image HRImage2 = new ProxyImage();
Image HRImage3 = new ProxyImage();
HRImage1.showImage();} }
22
 Virtual Proxies: delaying the creation and
initialization of expensive objects until needed, where
the objects are created on demand
 Remote Proxies: providing a local representation for
an object that is in a different address space. A
common example is Java RMI stub objects. The stub
object acts as a proxy where invoking methods on the
stub would cause the stub to communicate and invoke
methods on a remote object (called skeleton) found on
a different machine.
 Protection Proxies: where a proxy controls access to
RealSubject methods, by giving access to some objects
while denying access to others.
23

More Related Content

Viewers also liked (19)

ODP
Design Patterns Part1
Tom Chen
 
PPTX
Adapter design-pattern2015
Vic Tarchenko
 
PPT
Design patterns structuralpatterns(theadapterpattern)
APU
 
PDF
The 23 gof design patterns in java ,the summary
achraf_ing
 
PPT
Design patterns - Adapter Pattern
Annamalai Chockalingam
 
PDF
Bridge pattern for Dummies
TaekSoon Jang
 
PPTX
Design Patterns - 04 Adapter and Facade Pattern
eprafulla
 
PDF
Java Course 11: Design Patterns
Anton Keks
 
PDF
Design pattern tutorial
Piyush Mittal
 
PPT
Design Patterns
Anuja Arosha
 
ZIP
Adapter Design Pattern
guy_davis
 
PPTX
Let us understand design pattern
Mindfire Solutions
 
PPT
Design Patterns
soms_1
 
PPT
Design patterns ppt
Aman Jain
 
PDF
Design patterns
abhisheksagi
 
PPTX
Adapter Design Pattern
Adeel Riaz
 
PDF
Design Patterns & JDK Examples
Ender Aydin Orak
 
PPT
Design Patterns (Examples in .NET)
Aniruddha Chakrabarti
 
PDF
Design Patterns Illustrated
Herman Peeren
 
Design Patterns Part1
Tom Chen
 
Adapter design-pattern2015
Vic Tarchenko
 
Design patterns structuralpatterns(theadapterpattern)
APU
 
The 23 gof design patterns in java ,the summary
achraf_ing
 
Design patterns - Adapter Pattern
Annamalai Chockalingam
 
Bridge pattern for Dummies
TaekSoon Jang
 
Design Patterns - 04 Adapter and Facade Pattern
eprafulla
 
Java Course 11: Design Patterns
Anton Keks
 
Design pattern tutorial
Piyush Mittal
 
Design Patterns
Anuja Arosha
 
Adapter Design Pattern
guy_davis
 
Let us understand design pattern
Mindfire Solutions
 
Design Patterns
soms_1
 
Design patterns ppt
Aman Jain
 
Design patterns
abhisheksagi
 
Adapter Design Pattern
Adeel Riaz
 
Design Patterns & JDK Examples
Ender Aydin Orak
 
Design Patterns (Examples in .NET)
Aniruddha Chakrabarti
 
Design Patterns Illustrated
Herman Peeren
 

Similar to Proxy & adapter pattern (20)

PPTX
Factory method & strategy pattern
babak danyal
 
PDF
Contoh Factory pattern
Fajar Baskoro
 
PPT
Software Design Patterns
Pankhuree Srivastava
 
PPTX
Factory method, strategy pattern & chain of responsibilities
babak danyal
 
PPTX
Java Assignment Sample: Building Software with Objects, Graphics, Containers,...
Programming Homework Help
 
PPTX
Structural pattern 3
Naga Muruga
 
DOC
Test Engine
guestcdaa2dc
 
PPT
OOP Core Concept
Rays Technologies
 
PDF
Object Oriented Solved Practice Programs C++ Exams
MuhammadTalha436
 
PPTX
Andrei Iacob - SOLID: Strategies for Implementing Object–Oriented Design Prin...
Constanța Developers
 
PPTX
java_for_future_15-Multithreaded-Graphics.pptx
khoand6055
 
PPTX
Object Oriented Programming with Object Orinted Concepts
farhanghafoor5
 
DOC
Test Engine
guestcdaa2dc
 
PPT
Major Java 8 features
Sanjoy Kumar Roy
 
PDF
Presentation on design pattern software project lll
Uchiha Shahin
 
PDF
Dotnet unit 4
007laksh
 
PPTX
20.4 Java interfaces and abstraction
Intro C# Book
 
PDF
Design Patterns
adil raja
 
Factory method & strategy pattern
babak danyal
 
Contoh Factory pattern
Fajar Baskoro
 
Software Design Patterns
Pankhuree Srivastava
 
Factory method, strategy pattern & chain of responsibilities
babak danyal
 
Java Assignment Sample: Building Software with Objects, Graphics, Containers,...
Programming Homework Help
 
Structural pattern 3
Naga Muruga
 
Test Engine
guestcdaa2dc
 
OOP Core Concept
Rays Technologies
 
Object Oriented Solved Practice Programs C++ Exams
MuhammadTalha436
 
Andrei Iacob - SOLID: Strategies for Implementing Object–Oriented Design Prin...
Constanța Developers
 
java_for_future_15-Multithreaded-Graphics.pptx
khoand6055
 
Object Oriented Programming with Object Orinted Concepts
farhanghafoor5
 
Test Engine
guestcdaa2dc
 
Major Java 8 features
Sanjoy Kumar Roy
 
Presentation on design pattern software project lll
Uchiha Shahin
 
Dotnet unit 4
007laksh
 
20.4 Java interfaces and abstraction
Intro C# Book
 
Design Patterns
adil raja
 
Ad

More from babak danyal (20)

DOCX
applist
babak danyal
 
PPT
Easy Steps to implement UDP Server and Client Sockets
babak danyal
 
PPT
Java IO Package and Streams
babak danyal
 
PPT
Swing and Graphical User Interface in Java
babak danyal
 
PPT
Tcp sockets
babak danyal
 
PPTX
block ciphers and the des
babak danyal
 
PPT
key distribution in network security
babak danyal
 
PPT
Lecture10 Signal and Systems
babak danyal
 
PPT
Lecture8 Signal and Systems
babak danyal
 
PPT
Lecture7 Signal and Systems
babak danyal
 
PPT
Lecture6 Signal and Systems
babak danyal
 
PPT
Lecture5 Signal and Systems
babak danyal
 
PPT
Lecture4 Signal and Systems
babak danyal
 
PPT
Lecture3 Signal and Systems
babak danyal
 
PPT
Lecture2 Signal and Systems
babak danyal
 
PPT
Lecture1 Intro To Signa
babak danyal
 
PPT
Lecture9 Signal and Systems
babak danyal
 
PPT
Lecture9
babak danyal
 
PPT
Cns 13f-lec03- Classical Encryption Techniques
babak danyal
 
PPT
Classical Encryption Techniques in Network Security
babak danyal
 
applist
babak danyal
 
Easy Steps to implement UDP Server and Client Sockets
babak danyal
 
Java IO Package and Streams
babak danyal
 
Swing and Graphical User Interface in Java
babak danyal
 
Tcp sockets
babak danyal
 
block ciphers and the des
babak danyal
 
key distribution in network security
babak danyal
 
Lecture10 Signal and Systems
babak danyal
 
Lecture8 Signal and Systems
babak danyal
 
Lecture7 Signal and Systems
babak danyal
 
Lecture6 Signal and Systems
babak danyal
 
Lecture5 Signal and Systems
babak danyal
 
Lecture4 Signal and Systems
babak danyal
 
Lecture3 Signal and Systems
babak danyal
 
Lecture2 Signal and Systems
babak danyal
 
Lecture1 Intro To Signa
babak danyal
 
Lecture9 Signal and Systems
babak danyal
 
Lecture9
babak danyal
 
Cns 13f-lec03- Classical Encryption Techniques
babak danyal
 
Classical Encryption Techniques in Network Security
babak danyal
 
Ad

Recently uploaded (20)

PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PPTX
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 

Proxy & adapter pattern

  • 2. 2
  • 3. 3  Name: Adapter  Intent: Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.  Problem: A system has the right data and behavior but the wrong interface. Typically used when you have to make something a derivative of an abstract class.  Solution: The Adapter provides a wrapper with the desired interface.
  • 4. 4
  • 5. 5  Target  defines the domain-specific interface that Client uses.  Client  collaborates with objects conforming to the Target interface.  Adaptee  defines an existing interface that needs adapting.  Adapter  adapts the interface of Adaptee to the Target interface.
  • 6. 6  You are given a task to:  Create classes for squares and Rectangles that have the behavior "display " and "color ".  The client objects should not have to know whether they actually have a square or Rectangle. They just want to know that they have one of these shapes.
  • 7. 7
  • 8. 8  Suppose you are now asked to implement a circle, a new kind of Shape. To do this, You will want to create a new class Circle that implements the shape "circle" and derive it from the Shape class so that you can still get polymorphic behavior.  …but you remember you have already implemented Circle…a few months ago!!!
  • 9. 9
  • 10. 10
  • 11. 11 public interface Shape{ public void displayShape(); public void colorShape(); } class Square implements Shape{ public void displayShape(){ System.out.println("Displaying Square");} public void colorShape(){ System.out.println("Coloring Square");} } class Rectangle implements Shape{ public void displayShape(){System.out.println("Displaying Rectangle");} public void colorShape(){System.out.println("Coloring Rectangle");} }
  • 12. 12 class MyCircle{ public void displayCircle(){ System.out.println("Displaying Circle"); } public void colorCircle(){ System.out.println("Coloring Circle");} } class Circle implements Shape{ private MyCircle mycir; public Circle(){mycir = new MyCircle(); } public void displayShape(){ mycir.displayCircle(); } public void colorShape(){ mycir.colorCircle(); } }
  • 13. 13 class Client{ public static void main(String[]args){ Shape s = new Circle(); s.displayShape(); s.colorShape(); } }
  • 14. 14
  • 15. 15  Name: Proxy  Intent: Provide a surrogate or placeholder for another object to control access to it.  Problem: You want to control the access to an object for different reasons. You may want to delay the creation / initialization of expensive objects or you may want to provide a local representation of a remote object.  Solution: Provide a Stub / placeholder for actual object.
  • 16. 16
  • 17. 17  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.  RealSubject- the real object that the proxy represents.
  • 18. 18  Consider an image viewer program that lists and displays high resolution photos. The program has to show a list of all photos however it does not need to display the actual photo until the user selects an image item from a list.
  • 19. 19
  • 20. 20 public interface Image{ public void showImage(); } class HRImage implements Image{ public HRImage(){ System.out.println("loading a High Resolution image"); } public void showImage(){ System.out.println("Showing a High Resolution Image"); } }
  • 21. 21 class ProxyImage implements Image{ private Image proxyImage; public ProxyImage(){ System.out.println("Loading a proxy image");} public void showImage(){ proxyImage = (HRImage)new HRImage(); proxyImage.showImage();} } class ImageViewer{ public static void main(String[]args){ Image HRImage1 = new ProxyImage(); Image HRImage2 = new ProxyImage(); Image HRImage3 = new ProxyImage(); HRImage1.showImage();} }
  • 22. 22  Virtual Proxies: delaying the creation and initialization of expensive objects until needed, where the objects are created on demand  Remote Proxies: providing a local representation for an object that is in a different address space. A common example is Java RMI stub objects. The stub object acts as a proxy where invoking methods on the stub would cause the stub to communicate and invoke methods on a remote object (called skeleton) found on a different machine.  Protection Proxies: where a proxy controls access to RealSubject methods, by giving access to some objects while denying access to others.
  • 23. 23