Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 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

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 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 }

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 Adaptee > Target Adapter Calls adapteeMethod() targetMethod()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); thing = reader.next (); The adaptee is an InputStream ( System.in ) The target is a Scanner The adapter is a Scanner

15 Java Collection Interfaces Collection ListSetMap SortedSetSortedMap

16 The Collection Interface boolean add(E element) boolean addAll(Collection c) void clear() boolean contains(E element) boolean containsAll(Collection c) boolean equals(Object o) int hashCode() boolean isEmpty() Iterator iterator() boolean remove(Element E) boolean removeAll(Collection c) boolean retainAll(Collection 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 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 ListSetMap SortedSetSortedMap TrueStack TrueQueue 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 { private BST tree; public BSTAdapter(BST tree){ this.tree = tree; } public int size() {return tree.size()} public Iterator iterator() {return tree.iterator()} public boolean add(E element) {return tree.add(element)} } BSTAdapter adapter = new BSTAdapter (tree); adapter.addAll(list); adapter.addAll(sortedSet);


Download ppt "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."

Similar presentations


Ads by Google