1. Structural Patterns
The Adapter Pattern
Design Patterns
Chapter 8A
Dec 21, 2012 Design Patterns Chapter 8A1
2. Objectives
In this lecture, we will
• Review structural design patterns
• Discuss why such patterns are useful
• Introduce the Adapter pattern
• Discuss examples that take advantage of the adapter
pattern
Dec 21, 2012 Design Patterns Chapter 8A2
3. Structural Patterns
• Structural Patterns are used when it is necessary to build
larger structures that are composed of other existing
classes and objects
– Re-use of existing implementations
• Structural class patterns use inheritance
• Structural object patterns use aggregation
• This lecture introduces the adapter pattern
Dec 21, 2012 Design Patterns Chapter 8A3
4. What is an Adapter?
• A simple example of an adapter is an electric razor
adapter
• Most British domestic sockets are standard three pin
sockets
– live, neutral and earth
• Most electric razors come with a two pin plug
– That will not push into a three pin socket
• An electric razor adapter provides the link between the
two
– Modifies the interface of the razor to make it compatible with
the three pin socket
• What about British and Continental sockets?
Dec 21, 2012 Design Patterns Chapter 8A4
5. Software Adapters
• In object oriented programming the same type of problem
often arises
• A class, XClass, has been built and works well
• A system has been built that works with objects that
satisfy a certain interface
• The methods of the XClass provide all the functionality
required of the objects that work with the system
• BUT the interface of the XClass is not the interface
required by the system
• What is the easiest solution that re-uses the XClass?
– Write an adapter!
Dec 21, 2012 Design Patterns Chapter 8A5
6. Dialog Boxes
• How is a dialog box displayed in Java?
JOptionPane.showMessageDialog ( null, “This is a TEST” );
• What is displayed?
• Where does the image come from?
• Can the image be changed?
Dec 21, 2012 Design Patterns Chapter 8A6
7. The showMessageDialog method
• The declaration of this method is:
public static void showMessageDialog
{
Component parent,
Object message,
String title,
int messageType,
Icon anIcon
}
• The last parameter is of type Icon; an interface
– Any object that implements the Icon interface can be used
Dec 21, 2012 Design Patterns Chapter 8A7
8. Using an ImageIcon
• The ImageIcon class implements the Icon interface and can
build an icon from a GIF file
• For example:
JOptionPane.showMessageDialog (
null, // parent
"Hello", // message
"Testing GIF", // title
JOptionPane.INFORMATION_MESSAGE, // messageType
new ImageIcon("c:tempduke.gif") // anIcon
);
Dec 21, 2012 Design Patterns Chapter 8A8
9. What is the Icon interface?
• The definition of an Icon interface is:
public interface Icon
{
int getIconWidth () ;
int getIconHeight ();
void paintIcon ( Component c, Graphics g, int x, int y);
}
Dec 21, 2012 Design Patterns Chapter 8A9
10. A Simple Icon Class
public class PeteIcon implements Icon {
private int size;
public PeteIcon(int aSize) { size = aSize; }
public int getIconWidth() { return size; }
public int getIconHeight() { return size; }
public void paintIcon (Component c, Graphics g, int x, int y)
{
Graphics2D g2 = (Graphics2D) g;
Ellipse2D.Double e = new Ellipse2D.Double (x,y,size,size);
g2.setColor (Color.RED);
g2.fill(e);
g2.setColor(Color.BLACK);
g2.drawString("Pete",size/3,size/2);
}
}
Dec 21, 2012 Design Patterns Chapter 8A10
11. Using the Icon Class
• An instance of PeteIcon can be used anywhere an Icon is
expected:
JOptionPane.showMessageDialog (
null, "Hello", "Testing PeteIcon",
JOptionPane.INFORMATION_MESSAGE, new PeteIcon(100)
);
Dec 21, 2012 Design Patterns Chapter 8A11
12. Building a User Interface
• In building a user interface frames are often used to
contain a set of user interface components
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
JButton b1 = new JButton("Push");
contentPane.add(b1,BorderLayout.NORTH);
JButton b2 = new JButton("STOP");
contentPane.add(b2,BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
• The frame has a container that manages a collection of
components; a button is an example of a JComponent
Dec 21, 2012 Design Patterns Chapter 8A12
13. What is a JComponent
• JComponent is a superclass used in developing user
interface components
– JButton extends JComponent
• JComponent provides implementations for the methods
that are used by a container
• Two key methods are:
– paintComponent that draws the component
– getPrefferedSize that returns the preferred dimension of a
component
• A PeteIcon knows how to draw itself and can return both a
width and a height
– But it cannot be used in place of a JComponent
– The interface does not match
Dec 21, 2012 Design Patterns Chapter 8A13
14. Using an Adapter
• An adapter is needed to use an instance of an Icon to fulfil
the role of a JComponent
– An Icon has the appropriate functionality but an
inappropriate interface
• The adapter must extend the JComponent class and
contain an instance of an icon to do the work
– Calling a method of the adapter may result in a call to a
method of the contained icon
Dec 21, 2012 Design Patterns Chapter 8A14
15. An Icon Adapter Class
public class IconAdapter extends JComponent
{
private Icon icon;
public IconAdapter (Icon aIcon)
{ this.icon = aIcon; }
public void paintComponent(Graphics g)
{ icon.paintIcon(this,g,0,0); }
public Dimension getPreferredSize()
{
return new Dimension( icon.getIconWidth(),
icon.getIconHeight());
}
}
Dec 21, 2012 Design Patterns Chapter 8A15
16. Using the Adapter
• An instance of an IconAdapter can be used as a component
– Built from an instance of any Icon
• For example:
JComponent c = new IconAdapter ( new PeteIcon(100)) ;
contentPane.add(c,BorderLayout.CENTER);
• Although a PeteIcon is used here any Icon
could be used
– The IconAdapter adapts an Icon
Dec 21, 2012 Design Patterns Chapter 8A16
17. Using the Adapter Class
JFrame JComponent
«interface»
+paintComponent() Icon
+getPreferredSize()
IconAdapter PeteIcon
+paintComponent()
1 1
+getPreferredSize()
Dec 21, 2012 Design Patterns Chapter 8A17
18. The Adapter Pattern
• This is an example of the adapter pattern
– Used as a structural object pattern
• The situation in which the pattern is useful can be
summed up as:
– You want to use an existing class, called the adaptee,
without modifying it
– The system in which the class is to be used requires
conformance to a target interface that is different to the
interface of the adaptee
– The adaptee has the functionality required by the target
interface
Dec 21, 2012 Design Patterns Chapter 8A18
19. The Adapter Pattern
• Intent
– Convert the interface of a class into another interface; the
interface expected by a client
– The adapter lets classes work together that could not do so
otherwise because of incompatible interfaces
• Also known as Wrapper
• Applicability
Use the adapter pattern when:
– You want to use an existing class, and its interface does not
match the one that you need
– You want to create a re-useable class that co-operates with
unrelated or unforeseen classes
Dec 21, 2012 Design Patterns Chapter 8A19
20. The Adapter Pattern
Participants
• Target
– Defines the domain-specific interface that the Client uses
• Client
– Collaborates with objects that conform to the Target
interface
• Adaptee
– Defines an existing interface that needs to be adapted
• Adapter
– Adapts the interface of the Adaptee to the Target interface
Dec 21, 2012 Design Patterns Chapter 8A20
21. The Adapter Pattern
• Structure
– An object adapter relies on object composition:
Dec 21, 2012 Design Patterns Chapter 8A21
22. The Adapter Pattern
• Structure
– A class adapter uses multiple inheritance
Dec 21, 2012 Design Patterns Chapter 8A22
23. Consequences – The Class Adapter
• A class Adapter adapts an Adaptee to a Target by
commiting to a concrete Adaptee class
– As a consequence it is not possible to adapt a class and all
of its subclasses
• A class Adapter allows the Adapter to override some of
the Adaptee’s behaviour
– because the Adapter is a subclass of the Adaptee
• A class Adapter introduces only one object and therefore
no additional runtime overhead for method invocation
• Now compare these trade offs with an object adapter
Dec 21, 2012 Design Patterns Chapter 8A23
24. Consequences – The Object Adapter
• An Object Adapter allows a single adapter to work with
many Adpatees
– The Adaptee itself and any subclasses of the Adaptee
– The Adapter can add functionality to all Adaptees at once
• An Object Adapter makes it harder to override Adaptee
behaviour
– It is necessary to subclass the Adpatee and ensure that the
Adapter refers to the subclass rather than to the original
Adaptee
Dec 21, 2012 Design Patterns Chapter 8A24
25. How much adapting does an Adapter
do?
• In the example seen earlier in this lecture the adapter did
little more than interface conversion
– It changed the name of one method
– It implemented another method of the target interface by
calling two methods of the adaptee interface
• In other examples an adapter may do much more
– It may use several methods of an adaptee plus some
additional code to implement a method of the target interface
– It may add additional behaviour, not provided in the adaptee,
in order to fulfil the interface requirements of the target
Dec 21, 2012 Design Patterns Chapter 8A25
26. Java and Design Patterns
• The Java language contains many examples of the use of
design patterns
• For example consider the case where you have an input
stream and you need a reader
– An input stream reads bytes
– A reader reads characters
– The difference between the two is significant in many
languages
• In Java an InputStreamReader adapter is used
– Reader myReader = new InputStreamReader (System.in);
Dec 21, 2012 Design Patterns Chapter 8A26
27. Java InputStreamReader Adapter
• In this case:
Adaptee InputStream
Target Reader
Adapter InputStreamReader
Client the class that wants to read text from
an input stream
• As an exercise search for as many examples of the use of
the adapter pattern as you can find in Java
Dec 21, 2012 Design Patterns Chapter 8A27
28. Summary
In this lecture we have:
• Reviewed structural design patterns
• Discussed why such patterns are useful
• Introduced the Adapter pattern
• Discussed examples that take advantage of the adapter
pattern
Dec 21, 2012 Design Patterns Chapter 8A28
Editor's Notes
Part of this lecture will be reserved for working through solutions to selected exercises from last week. Notes relating to this do not appear in the slides.