Computer Science 209
description
Transcript of Computer Science 209
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 for using this class requires conformance to an different interface (the target)
• The target and adaptee interfaces are conceptually related
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
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
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
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 need 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
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)); }}
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 } }
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)); }}
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()); }}
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); } }
Distinct Responsibilities
Adaptee
<<Interface>>Target
Adapter
Calls adapteeMethod()
targetMethod() adapteeMethod()
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
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
Java Collection Interfaces
Collection
List Set Map
SortedSet SortedMap
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
AbstractCollection
• All Collection methods are implemented in the class AbstractCollection
• Subclasses can override these methods
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"));
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
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
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);