CS 210. Final Review November 28, 2006. CS 210. Adapter Pattern. 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();
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
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 I'm flying a short distance I'm flying a short distance I'm flying a short distance I'm flying a short distance
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.
Façade Pattern defined The Façade Pattern provides a unified interface to a set of interfaces in a subsystem. Façade defines a higher level interface that makes the subsystem easier to use.
Design Principle Principle of Least Knowledge talk only to your immediate friends Basically this says minimize your dependencies
Client The client only has one friend – and that is a good thing If the subsystem gets too complicated One can recursively apply the same principle.
Converts one interface to another Decorator Doesn’t alter the interface, But adds responsibility Adapter Facade Makes an interface simpler A little comparison Pattern Intent
Abstract base class public abstract class CaffeineBeverage { final void prepareRecipe() { boilWater(); brew(); pourInCup(); addCondiments(); } abstract void brew(); abstract void addCondiments(); void boilWater() { System.out.println("Boiling water"); } void pourInCup() { System.out.println("Pouring into cup"); } }
Coffee and Tea in terms of the abstract caffeine class public class Coffee extends CaffeineBeverage { public void brew() { System.out.println("Dripping Coffee through filter"); } public void addCondiments() { System.out.println("Adding Sugar and Milk"); } } public class Tea extends CaffeineBeverage { public void brew() { System.out.println("Steeping the tea"); } public void addCondiments() { System.out.println("Adding Lemon"); } }
Template Method Pattern defined The Template Method Pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.
Design Principle The Hollywood Principle Don’t call us, we’ll call you.
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 two implementations of lists in a uniform way? • Example here uses restaurant items
Now if we want to… • printMenu() • Print every item on the menu • printBreakfastMenu() • Print just breakfast items • printLunchMenu() • Print just lunch items • printVegetarianMenu() • isItemVegetarian(name)