1 / 20

CS 210

CS 210. Introduction to Design Patterns September 14 th , 2006. Head First Design Patterns. Chapter 3 Decorator Pattern. Beverage. description getDescription() Cost(). DarkRoast. HouseBlend. Decaf. Espresso. cost(). cost(). cost(). cost(). A coffee shop example….

trapper
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 Introduction to Design Patterns September 14th, 2006

  2. Head First Design Patterns Chapter 3 Decorator Pattern

  3. Beverage description getDescription() Cost() DarkRoast HouseBlend Decaf Espresso cost() cost() cost() cost() A coffee shop example… What if you want to show the addition of condiments such as steamed milk, soy, mocha and whipped milk?

  4. Page 81 Head First Design Patterns

  5. Beverage class redone Page 83 Head First Design Patterns

  6. Potential problems with the design so far? • Solution is not easily extendable • How to deal with • new condiments • Price changes • New beverages that may have a different set of condiments – a smoothie? • Double helpings of condiments

  7. Design Principle The Open-Closed Principle Classes should be open for extension, but closed for modification.

  8. The Decorator Pattern • Take a coffee beverage object – say DarkRoast object • Decorate it with Mocha • Decorate it with Whip • Call the cost method and rely on delegation to correctly compute the composite cost

  9. Decorator Pattern approach Page 89 Head First Design Patterns

  10. Computing Cost using the decorator pattern Page 90 Head First Design Patterns

  11. Decorator Pattern The decorator pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for extending functionality.

  12. Decorator Pattern Defined Page 91 Head First Design Patterns Decorator Pattern Defined

  13. Decorator Pattern for Beverage Example Page 92 Head First Design Patterns

  14. A look at Java GUI classes • Java I/O uses a lot of decorator pattern

  15. Java I/O Use of Decorator Pattern LineNumberInputStream BufferedInputStream FileInputStream

  16. Decorating Java I/O Classes InputStream FileInputStream ByteArrayInputStream StringBufferInputStream FilterInputStream LineNumberInputStream PushBackInputStream DataInputStream BufferedInputStream Decorators

  17. Look at Java I/O Classes • FilterInputStream • BufferedInputStream

  18. public class LowerCaseInputStream extends FilterInputStream { public LowerCaseInputStream(InputStream in) { super(in); } public int read() throws IOException { int c = super.read(); return (c == -1 ? c : Character.toLowerCase((char)c)); } public int read(byte[] b, int offset, int len) throws IOException { int result = super.read(b, offset, len); for (int i = offset; i < offset+result; i++) { b[i] = (byte)Character.toLowerCase((char)b[i]); } return result; } }

  19. public class InputTest { public static void main(String[] args) throws IOException { int c; try { InputStream in = new LowerCaseInputStream( new BufferedInputStream( new FileInputStream("test.txt"))); while((c = in.read()) >= 0) { System.out.print((char)c); } in.close(); } catch (IOException e) { e.printStackTrace(); } } }

  20. OO Basics Abstraction Encapsulation Inheritance Polymorphism OO Principles Encapsulate what varies Favor composition over inheritance Program to interfaces not to implementations Strive for loosely coupled designs between objects that interact Classes should be open for extension but closed for modification. OO Patterns Strategy Pattern defines a family of algorithms, Encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. Decorator Pattern – attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative for sub-classing for extending functionality Summary so far..

More Related