proxy & adapter pattern

23

Upload: babak

Post on 18-Jul-2015

126 views

Category:

Education


0 download

TRANSCRIPT

2

3

Name: Adapter

Intent: Convert the interface of a class into anotherinterface clients expect. Adapter lets classes worktogether that couldn't otherwise because ofincompatible interfaces.

Problem: A system has the right data and behavior butthe wrong interface. Typically used when you have tomake something a derivative of an abstract class.

Solution: The Adapter provides a wrapper with thedesired 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 theyactually have a square or Rectangle. They just want toknow that they have one of these shapes.

7

8

Suppose you are now asked to implement a circle, anew kind of Shape. To do this, You will want to create anew class Circle that implements the shape "circle"and derive it from the Shape class so that you can stillget polymorphic behavior.

…but you remember you have already implementedCircle…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 RealSubjectand representing its services. The interface must beimplemented by the proxy as well so that the proxy canbe used in any location where the RealSubject can beused.

Proxy- Maintains a reference that allows the Proxy toaccess the RealSubject. Implements the same interfaceimplemented by the RealSubject so that the Proxy canbe substituted for the RealSubject. Controls access tothe RealSubject and may be responsible for its creationand 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 andinitialization of expensive objects until needed, wherethe objects are created on demand

Remote Proxies: providing a local representation foran object that is in a different address space. Acommon example is Java RMI stub objects. The stubobject acts as a proxy where invoking methods on thestub would cause the stub to communicate and invokemethods on a remote object (called skeleton) found ona different machine.

Protection Proxies: where a proxy controls access toRealSubject methods, by giving access to some objectswhile denying access to others.

23