1 / 105

CS 210

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

cole-moody
Download Presentation

CS 210

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 210 Final Review November 28, 2006

  2. CS 210 Adapter Pattern

  3. Adapters in real life Page 236 – Head First Design Patterns

  4. Object-Oriented Adapters Page 237 Head First Design Patterns

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

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

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

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

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

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

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

  12. Adapter Pattern explained Page 241 – Head First Design Patterns

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

  14. Adapter Pattern Page 243 – Head First Design Patterns

  15. Façade Pattern Simplifying complex subsystems

  16. Page 255 – Head First Design Patterns

  17. Watching the movie the hard way…. Page 256 – Head First Design Patterns

  18. What needs to be done to watch a movie….

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

  20. Façade pattern – Class Diagram

  21. Design Principle Principle of Least Knowledge talk only to your immediate friends Basically this says minimize your dependencies

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

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

  24. CS 210 Template Method Pattern

  25. Abstracted Recipe method

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

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

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

  29. Design Principle The Hollywood Principle Don’t call us, we’ll call you.

  30. CS 210 Iterator Pattern

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

  32. 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)

  33. Iterating through breakfast items

  34. Iterating through lunch items

  35. Encapsulating iteration • Instead of using multiple ways to iterate through the lists, define ONE interface to iterate through the lists …

  36. Using iterator to get breakfast items

  37. Using iterator to get lunch items

  38. Meet the iterator pattern…

More Related