Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 210 Introduction to Design Patterns September 14 th, 2006.

Similar presentations


Presentation on theme: "CS 210 Introduction to Design Patterns September 14 th, 2006."— Presentation transcript:

1 CS 210 Introduction to Design Patterns September 14 th, 2006

2 Head First Design Patterns Chapter 3 Decorator Pattern

3 A coffee shop example… Beverage description getDescription() Cost() DarkRoast cost() HouseBlend cost() Decaf cost() Espresso cost() 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

16 Decorating Java I/O Classes InputStream FileInputStream StringBufferInputStream ByteArrayInputStream FilterInputStream PushBackInputStream BufferedInputStream DataInputStream LineNumberInputStream 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 Summary so far.. 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


Download ppt "CS 210 Introduction to Design Patterns September 14 th, 2006."

Similar presentations


Ads by Google