Adapter Pattern. public interface Duck { public void quack(); public void fly(); } public interface Turkey { public void gobble(); public void fly();

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

$100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300.
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.
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.
Building Java Programs Chapter 5
Strategy Pattern1 Design Patterns 1.Strategy Pattern How to design for flexibility?
Down on the Farm An Animated Book for 5K Farm Animal Unit by Kim Entzminger.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Public class MyThread1 extends Thread { public void start() { } public void run() { System.out.print(“Hello \n”) } public class MyThread2 extends Thread.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
In State: A Design Pattern James Goodrich. You Might Like: SDP.
1 Algorithms and Problem Solving. 2 Outline  Problem Solving  Problem Solving Strategy  Algorithms  Sequential Statements  Examples.
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.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
$100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300 $400 $500 $100 $200 $300.
1 Apr 5, 2012 ForkJoin New in Java 7. Incrementor I package helloWorld; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveTask;
CS 415 N-Tier Application Development By Umair Ashraf June 21,2013 National University of Computer and Emerging Sciences Lecture # 2 Introduction to Design.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
Logical OperatorstMyn1 Logical Operators Using logical operators, we can combine a series of comparisons into a single expression. if(letter>=‘A’ && letter
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Random numbers. 2 The Random class A Random object generates pseudo-random numbers. –Class Random is found in the java.util package. import java.util.*;
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.
Inheritance CSCE 190 – Java Instructor: Joel Gompert Mon, August 2, 2004.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
CS 210 Introduction to Design Patterns August 29, 2006.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization Codes.
Interfaces and Polymorphism CS 162 (Summer 2009).
CSCI S-1 Section 4. Deadlines for Homework 2 Problems 1-8 in Parts C and D – Friday, July 3, 17:00 EST Parts E and F – Tuesday, July 7, 17:00 EST.
Lab 3. Why Compressed Row Storage –A sparse matrix has a lot of elements of value zero. –Using a two dimensional array to store a sparse matrix wastes.
All About the Farm. Who am I? Raise your hand if you know the animals that I am talking about. Listen!!!
Constructor OverloadingtMyn1 Constructor Overloading One context in which you will regularly need to use overloading is when you write constructors for.
Methods.
Façade Design Pattern by Ali Alkhafaji Unified interface for a set of interfaces to promote readability and usability.
Floating Point ValuestMyn1 Floating Point Values Internally, floating point numbers have three pairs: a sign (positive or negative), a mantissa (has a.
CS001 Introduction to Programming Day 6 Sujana Jyothi
Turkey Circle Dance Sung to the tune of Hokey Pokey
CS 210 Adapter Pattern October 17 th, Adapters in real life Page 236 – Head First Design Patterns.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
WAP to find out the number is prime or not Import java.util.*; Class Prime { public static void main(string args[]) { int n,I,res; boolean flag=true;
Computer Science 209 Software Development Packages.
SE 461 Software Patterns. FACTORY METHOD PATTERN.
SE 461 Software Patterns Welcome to Design Patterns.
using System; namespace Demo01 { class Program
Math and String Examples
Interface.
Polymorphism and Observers
Decision statements. - They can use logic to arrive at desired results
ФОНД ЗА РАЗВОЈ РЕПУБЛИКЕ СРБИЈЕ
ForkJoin New in Java 7 Apr 5, 2012.
Review for the Final Exam
חלק ה שימוש במציין שלם לערך תווי
ForkJoin New in Java 7 Apr 5, 2012.
Java Threads אליהו חלסצ'י תכנות מתקדם תרגול מספר 6
References, Objects, and Parameter Passing
Recursive GCD Demo public class Euclid {
'III \-\- I ', I ,, - -
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
The Random Class The Random class is part of the java.util package
Exceptions April 19, 2006 ComS 207: Programming I (in Java)
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Philosopher Example class PhilosophersShare {
22C:21 Problem 2.3 Solution Outline.
,, 'III \-\-
PROGRAMMING ASSIGNMENT I
MIS 222 – Lecture 12 10/9/2003.
Instructor: Mainak Chaudhuri
Presentation transcript:

Adapter Pattern

public interface Duck { public void quack(); public void fly(); } public interface Turkey { public void gobble(); public void fly(); }

Adapter Pattern

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

MallardDuck public class MallardDuck implements Duck { public void quack() { System.out.println("Quack"); } public void fly() { System.out.println("I'm flying"); }

Turkey Adapter 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 Adapter import java.util.Random; public class DuckAdapter implements Turkey { Duck duck; Random rand; public DuckAdapter(Duck duck) { this.duck = duck; rand = new Random(); } public void gobble() { duck.quack(); } public void fly() { if (rand.nextInt(5) == 0) { duck.fly(); } } }

Turkey Test Drive public class TurkeyTestDrive { public static void main(String[] args) { MallardDuck duck = new MallardDuck(); Turkey duckAdapter = new DuckAdapter(duck); for(int i=0;i<10;i++) { System.out.println("The DuckAdapter says..."); duckAdapter.gobble(); duckAdapter.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(); }