Download presentation
Presentation is loading. Please wait.
1
Computer Science 209 The Adapter Pattern
2
Hardware Adapters Three-prong to two-prong
3
Hardware Adapters Firewire 800
4
Hardware Adapters Thunderbolt to Firewire 800
5
Hardware Adapters Thunderbolt 2 to Thunderbolt to Firewire 800
6
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
7
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
8
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
9
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
10
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
11
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)); }
12
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 }
13
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)); }
14
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());
15
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); }
16
Distinct Responsibilities
<<Interface>> Target Adapter Adaptee targetMethod() adapteeMethod() Calls adapteeMethod()
17
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
18
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
19
Java Collection Interfaces
List Set Map SortedSet SortedMap
20
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
21
AbstractCollection All Collection methods are implemented in the class AbstractCollection Subclasses can override these methods
22
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"));
23
Java Collection Interfaces
TrueStack List Set Map TrueQueue SortedSet SortedMap TrueStack and TrueQueue could extend Collection But then they must support the add, size, and iterator methods
24
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
25
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);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.