computer science 209 the adapter pattern. the context of the adapter pattern i want to use an...

21
Computer Science 209 The Adapter Pattern

Upload: debra-roberts

Post on 17-Jan-2016

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Computer Science 209

The Adapter Pattern

Page 2: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

The Context of the Adapter Pattern

• I want to use an existing class (the adaptee) without modifying it

• The context for using this class requires conformance to an different interface (the target)

• The target and adaptee interfaces are conceptually related

Page 3: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Solution of the Adapter Pattern

• I define an adapter class that implements the target interface

• The adapter class contains a reference to the adaptee and translates target methods to adaptee methods

• The client wraps an adapter around an adaptee

Page 4: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Example Problem

• I want to add an icon as a component to a GUI’s container

• An icon is similar to a component (both are involved with painting and determining dimensions of a rectangular area)

• I want to get my icon to behave just like a component but I don’t want to change my icon itself

Page 5: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Solution

• Define an adapter class that extends JComponent

• This class contains a reference to my icon

• Override the paintComponent and getPreferredSize methods to paint the icon and get its dimensions

Page 6: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

The Icon Interfaceimport java.awt.*;

public interface Icon{

public int getIconWidth();

public int getIconHeight();

public void paintIcon(Component c, Graphics g, int x, int y);}

The component that displays an icon needs to be able to get its width and height and to paint the icon at a given position

paintIcon can use the Component parameter to obtain properties like the background color that might be useful

Page 7: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Using a New Icon for Starsimport javax.swing.JOptionPane;

public class IconTest{

public static void main(String[] args){ JOptionPane.showMessageDialog(null, "Here is a Star", "Message", JOptionPane.INFORMATION_MESSAGE, new StarIcon(100)); }}

Page 8: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Implementing the Icon Interfaceimport java.awt.*;import javax.swing.Icon;

public class StarIcon implements Icon{

private int size;

public StarIcon(int size) {this.size = size;}

public int getIconWidth() {return size;}

public int getIconHeight() {return size;}

public void paintIcon(Component c, Graphics g, int x, int y){ // Code for drawing a star } }

Page 9: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Using a New Icon for Starsimport javax.swing.JOptionPane;

public class IconTest{

public static void main(String[] args){ JOptionPane.showMessageDialog(null, "Here is a Star", "Message", JOptionPane.INFORMATION_MESSAGE, new StarIcon(100)); }}

Page 10: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Code for IconAdapterimport javax.swing.*;import java.awt.*;

public class IconAdapter extends JComponent{

private Icon icon;

public IconAdapter(Icon icon){ this.icon = icon; }

public void paintComponent(Graphics g){ icon.paintIcon(this, g, 0, 0); }

public Dimension getPreferredSize(){ return new Dimension(icon.getIconWidth(), icon.getIconHeight()); }}

Page 11: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Using IconAdapterimport javax.swing.*;import java.awt.*;

public class IconAdapterTest{

public static void main(String[]args){ Icon icon = new StarIcon(); JComponent component = new IconAdapter(icon); JFrame frame = new JFrame(); Container pane = frame.getContentPane(); pane.add(component); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }

Page 12: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Distinct Responsibilities

Adaptee

<<Interface>>Target

Adapter

Calls adapteeMethod()

targetMethod() adapteeMethod()

Page 13: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Another Example: Input Streams

• Input streams return data as bytes

• We want characters or integers or doubles or strings

• Turn an input stream into a scanner, which reads the bytes from the input stream and returns values of these types

Page 14: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Wrapping an Input Stream in a Scanner

java.util.Scanner reader = new Scanner(System.in);

<int, double, String> thing = reader.next<Int, Double, Line>();

The adaptee is an InputStream (System.in)

The target is a Scanner

The adapter is a Scanner

Page 15: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Java Collection Interfaces

Collection

List Set Map

SortedSet SortedMap

Page 16: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

The Collection Interfaceboolean add(E element)boolean addAll(Collection<E> c)void clear()boolean contains(E element)boolean containsAll(Collection<E> c)boolean equals(Object o)int hashCode()boolean isEmpty()Iterator iterator()boolean remove(Element E)boolean removeAll(Collection<E> c)boolean retainAll(Collection<E> c)int size()Object[] toArray()Object[] toArray(Object[] a)

The mutators (in green) are optional operations

Page 17: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

AbstractCollection

• All Collection methods are implemented in the class AbstractCollection

• Subclasses can override these methods

Page 18: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Example: containspublic boolean contains(E element){ Iterator<E> iter = this.iterator(); while (iter.hasNext()) if (element.equals(iter.next())) return true; return false;}

System.out.println(list.contains("hi there"));

Page 19: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Java Collection Interfaces

Collection

List Set Map

SortedSet SortedMap

TrueStack

TrueQueue

TrueStack and TrueQueue could extend Collection

But then they must support the add, size, and iterator methods

Page 20: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Or We Could Adapt, Instead

• Define an adapter class that extends AbstractCollection and contains a reference to the adaptee collection

• Override the add, size, and iterator methods in the adapter class

Page 21: Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context

Example: Adapting a Binary Search Tree

public BSTAdapter extends AbstractCollection<E>{

private BST<E> tree;

public BSTAdapter(BST<E> tree){ this.tree = tree; }

public int size() {return tree.size()} public Iterator<E> iterator() {return tree.iterator()} public boolean add(E element) {return tree.add(element)}}

BSTAdapter<E> adapter = new BSTAdapter<E>(tree);adapter.addAll(list);adapter.addAll(sortedSet);