Proxy & adapter pattern
Upcoming SlideShare
Loading in...5
×
 

Proxy & adapter pattern

on

  • 291 views

 

Statistics

Views

Total Views
291
Views on SlideShare
288
Embed Views
3

Actions

Likes
0
Downloads
2
Comments
0

1 Embed 3

http://babakdanyal.blogspot.com 3

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Proxy & adapter pattern Proxy & adapter pattern Presentation Transcript

  • 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