1 / 21

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 )

adlai
Download Presentation

Computer Science 209

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 209 The Adapter Pattern

  2. 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

  3. 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

  4. 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

  5. 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

  6. The Icon Interface import 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

  7. Using a New Icon for Stars import 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)); } }

  8. Implementing the Icon Interface import java.awt.*; import javax.swing.Icon; public class StarIcon implements Icon{ private int size; public StarIcon(int size) {this.size = size;} public intgetIconWidth() {return size;} public intgetIconHeight() {return size;} public void paintIcon(Component c, Graphics g, int x, int y){ // Code for drawing a star } }

  9. Using a New Icon for Stars import 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)); } }

  10. Code for IconAdapter import 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()); } }

  11. Using IconAdapter import 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); } }

  12. Distinct Responsibilities <<Interface>> Target Adapter Adaptee targetMethod() adapteeMethod() Calls adapteeMethod()

  13. 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

  14. 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

  15. Java Collection Interfaces Collection List Set Map SortedSet SortedMap

  16. The Collection Interface boolean 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

  17. AbstractCollection • All Collection methods are implemented in the class AbstractCollection • Subclasses can override these methods

  18. Example: contains public 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"));

  19. Java Collection Interfaces Collection TrueStack List Set Map TrueQueue SortedSet SortedMap TrueStack and TrueQueue could extend Collection But then they must support the add, size, and iterator methods

  20. 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

  21. 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);

More Related