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.

Slides:



Advertisements
Similar presentations
Computer Science 209 Images and GUIs. Working with Java Colors The class java.awt.Color includes constants, such as Color.red, for some commonly used.
Advertisements

Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
Working With Collections in the AP ™ Java Subset 2003 ACTE Convention © 2003 by Kenneth A. Lambert.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
Computer Science 209 Software Development Iterators.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
15-Jun-15 Lists in Java Part of the Collections Framework.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Graphical User Interfaces
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
12-Jul-15 Lists in Java Part of the Collections Framework.
1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Swing Graphics ● Empty Swing containers have no visual appearance except for a background color ● Every JComponent must have a paintComponent method that.
GUI Tutorial Images. Useful Info – not on final exam.
Java Concepts Chapter 2 – Graphical Applications Mr. Smith AP Computer Science A.
Web Design & Development Lecture 18. Java Graphics.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Graphical User Interface (GUI) Design using Swing Course Lecture Slides.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington User Interface COMP 112 #30.
Introducing Graphics There are generally two types of graphics facilities in Java –Drawing –GUIs We concentrate on drawing here We will draw on a Graphics.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
Interfaces & Polymorphism part 2:
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
tiled Map Case Study: Rendering with JPanel © Allan C. Milne v
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. In order to display a drawing in a frame, define a class that extends.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
The Drawing program – Java Applets
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Even-Driven Programming and basic Graphical User Interface.
1 Even even more on being classy Aaron Bloomfield CS 101-E Chapter 4+
Creating Your Own Widgets CompSci 230 S Software Construction.
More Design Patterns Horstmann ch.10.1,10.4. Design patterns Structural design patterns –Adapter –Composite –Decorator –Proxy Behavioral design patterns.
Lesson 39: More wrapup on GUIs. 1.This presentation will focus on the decisions around software architecture and the decisions that will drive the code.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
SETS AND MAPS Collections of Data. Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary.
Computer Science 209 Software Development Inheritance and Composition.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Java Swing One of the most important features of Java is its ability to draw graphics.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Lesson 33: Layout management and drawing – Java GUI.
Computer Science 209 Software Development Packages.
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Import javax.swing.JOptionPane; public class Rectangle { public static void main(String[] args) { double width, length, area, perimeter; String lengthStr,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Creating Your Own Widgets
Java Graphics.
Java Graphics CS 2511.
Software Development Iterators
Chapter 10 Graphics.
JAVA Collections Framework Set Interfaces & Implementations
Adapter Pattern Context:
Computer Science 209 The Adapter Pattern.
Presentation transcript:

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

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

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 }

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

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()); }

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

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

Java Collection Interfaces Collection ListSetMap SortedSetSortedMap

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

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

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"));

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

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 { 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);