CS 210 Adapter Pattern October 17 th, 2006. Adapters in real life Page 236 – Head First Design Patterns.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Winter 2007ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
Computer Science 209 Software Development Iterators.
CS 350 – Software Design Putting Patterns Together Let’s put a lot of patterns together. We need a lot of ducks! public interface Quackable { public void.
Lecture 2: Object Oriented Programming I
Strategy Pattern1 Design Patterns 1.Strategy Pattern How to design for flexibility?
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
Design Patterns. What is a Design Pattern? Generic pattern for solving a certain class of problem Learn to recognize the class of problem and apply a.
1 (More) Pattern Games CS : Software Design Winter /T10.
Feb Ron McFadyen1 Adapter An adapter converts the interface of a class into another interface the client expects. An adapter lets classes work.
CSC 160 Practice Final Review. Question 1 class A { public void blah() { System.out.println("a"); } class B extends A { public void blah() { System.out.println("b");
March Ron McFadyen1 Adapter An adapter converts the interface of a class into another interface clients expect. An adapter lets classes work.
ADAPTER PATTERN Ali Zonoozi Design patterns course Advisor: Dr. Noorhoseini Winter 2010.
CS 280 Data Structures Professor John Peterson. Project 9 Questions? IS280.
Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
Advanced Java Session 3 New York University School of Continuing and Professional Studies.
1 Lecture 09 Iterators and Enumerations Reading for these lectures: Weiss, Section Iterator Interface. Much better is: ProgramLive, Section 12.3.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Nested References 2 inner reference data types Classes-Interfaces.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 8, page 1 Sun Certified Java 1.4 Programmer Chapter 8 Notes Gary Lance
Computer Science II 810:062 Section 01. How is CS I different from CS II? When you teach Java there are a series of decisions that have to be made…
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
COMP 121 Week 12: Decorator and Adapter Design Patterns.
Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet.
1 Functional Visitor Ø Motivation Example Container checking -- Make sure each Container is not overloaded Weight Item + int total( ) w Container + int.
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
Chapter 7: The Adapter Pattern. Object Oriented Adapters Suppose that you have existing software. You have outsourced some of your work and there is a.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.
CS 210 Introduction to Design Patterns August 29, 2006.
Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.
Interfaces and Polymorphism CS 162 (Summer 2009).
What is Iterator Category: Behavioral Generic Way to Traverse Collection Not Related to Collection Implementation Details Not Related to the direction/fashion.
Generic Programming and Inner classes ge·ner·ic 1a : relating or applied to or descriptive of all members of a genus, species, class, or group : common.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
Adapter Pattern. public interface Duck { public void quack(); public void fly(); } public interface Turkey { public void gobble(); public void fly();
Lazy Evaluation Computer Science 3 Gerb Objective: Understand lazy evaluation in Java.
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
CS 210 Introduction to Design Patterns September 14 th, 2006.
Section 2.2 The StringLog ADT Specification. 2.2 The StringLog ADT Specification The primary responsibility of the StringLog ADT is to remember all the.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
SE 461 Software Patterns Welcome to Design Patterns.
Design Patterns C++ Java C#.
Software Development Iterators
Design Patterns C++ Java C#.
Adapter, Fascade, and More
Decision statements. - They can use logic to arrive at desired results
Adapter Pattern 1.
Object Oriented Design Patterns - Structural Patterns
Assignment 7 User Defined Classes Part 2
class PrintOnetoTen { public static void main(String args[]) {
The Adapter Pattern.
Software Design Lecture : 34.
Software Design Lecture : 39.
Objects with ArrayLists as Attributes
Presentation transcript:

CS 210 Adapter Pattern October 17 th, 2006

Adapters in real life Page 236 – Head First Design Patterns

Object-Oriented Adapters Page 237 Head First Design Patterns

Turkey that wants to be a duck example public interface Duck { public void quack(); public void fly(); }

Subclass of a duck – Mallard Duck public class MallardDuck implements Duck { public void quack() { System.out.println("Quack"); } public void fly() { System.out.println("I'm flying"); }

Turkey Interface public interface Turkey { public void gobble(); public void fly(); }

An instance of a turkey public class WildTurkey implements Turkey { public void gobble() { System.out.println("Gobble gobble"); } public void fly() { System.out.println("I'm flying a short distance"); }

Turkey adapter – that makes a turkey look like a duck public class TurkeyAdapter implements Duck { Turkey turkey; public TurkeyAdapter(Turkey turkey) { this.turkey = turkey; } public void quack() { turkey.gobble(); } public void fly() { for(int i=0; i < 5; i++) { turkey.fly(); }

Duck test drive public class DuckTestDrive { public static void main(String[] args) { MallardDuck duck = new MallardDuck(); WildTurkey turkey = new WildTurkey(); Duck turkeyAdapter = new TurkeyAdapter(turkey); System.out.println("The Turkey says..."); turkey.gobble(); turkey.fly(); System.out.println("\nThe Duck says..."); testDuck(duck); System.out.println("\nThe TurkeyAdapter says..."); testDuck(turkeyAdapter); } static void testDuck(Duck duck) { duck.quack(); duck.fly(); }

Test run – turkey that behaves like a duck The Turkey says... Gobble gobble I'm flying a short distance The Duck says... Quack I'm flying The TurkeyAdapter says... Gobble gobble I'm flying a short distance

Adapter Pattern explained Page 241 – Head First Design Patterns

Adapter Pattern defined The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

Adapter Pattern Page 243 – Head First Design Patterns

Object and Class adapters Page 244 – Head First Design Patterns

Real world adapters

Designing the Adapter

Writing the EnumerationIterator adapter public class EnumerationIterator implements Iterator { Enumeration enumeration; public EnumerationIterator(Enumeration enumeration) { this.enumeration = enumeration; } public boolean hasNext() { return enumeration.hasMoreElements(); } public Object next() { return enumeration.nextElement(); } public void remove() { throw new UnsupportedOperationException(); }