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.
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.
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!!!
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(); }
}
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.
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.
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.