Computer Science 209 The Adapter Pattern.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

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.
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
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.
Graphical User Interfaces
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.
Contructing GUI’s in Java Implemented in the Swing API Imported into your programs by: import javax.swing.*; Most Swing programs also need the AWT packages.
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.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Computer Science 209 The Strategy Pattern II: Emulating Higher-Order Functions.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
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.
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
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
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.
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
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.
Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet.
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.
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.
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.
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.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
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.
Creating Your Own Widgets
Chapter 8 Graphics.
Java Graphics.
Java Graphics CS 2511.
CSC 1051 – Data Structures and Algorithms I
Software Development Packages
Chapter 5 Ordered List.
Software Development Iterators
Chapter 4 Interface Types and Polymorphism Part 2
Software Development Inheritance and Composition
Java Programming: Guided Learning with Early Objects
Chapter 10 Graphics.
Chapter 5 Ordered List.
JAVA Collections Framework Set Interfaces & Implementations
Adapter Pattern Context:
Steps to Creating a GUI Interface
Chapter 8 Graphics.
Chapter 4 Interface Types and Polymorphism Part 1
Presentation transcript:

Computer Science 209 The Adapter Pattern

Hardware Adapters Three-prong to two-prong

Hardware Adapters Firewire 800

Hardware Adapters Thunderbolt to Firewire 800

Hardware Adapters Thunderbolt 2 to Thunderbolt to Firewire 800

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 <<Interface>> Target Adapter Adaptee targetMethod() adapteeMethod() Calls 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 List Set Map SortedSet SortedMap

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

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

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

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

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