Presentation is loading. Please wait.

Presentation is loading. Please wait.

OO Design Patterns - Decorator

Similar presentations


Presentation on theme: "OO Design Patterns - Decorator"— Presentation transcript:

1 OO Design Patterns - Decorator
Object-Oriented Programming

2 Starbuzz Coffee current design
Đại học Công nghệ - ĐHQG HN OO Design Pattern

3 Starbuzz Coffee problem
In addition to coffee, customers can ask for condiments like milk, mocha, soy,… then topped with whipped cream How to modify the current system to allow that? Đại học Công nghệ - ĐHQG HN OO Design Pattern

4 Đại học Công nghệ - ĐHQG HN
OO Design Pattern

5 Đại học Công nghệ - ĐHQG HN
Inheritance

6 Better, but there’re still potential issues
What requirements or other factors might change that will impact this design? Price changes for condiments New condiments New beverages What about a double mocha? And ??? Đại học Công nghệ - ĐHQG HN Inheritance

7 Open-Closed Principle
Open to extension: you can create subclasses Closed to modification: don’t modify existing code But be careful! Apply OCP principle EVERYWHERE is wasteful, unnecessary, and can lead to complex, hard to understand code Đại học Công nghệ - ĐHQG HN OO Design Pattern

8 Decorator Pattern A customer wants a Dark Roast with Mocha and Whip, we’ll: Take a DarkRoast object Decorate it with a Mocha object Decorate it with a Whip object Call the cost() method and rely on delegation to add on the condiment costs. Đại học Công nghệ - ĐHQG HN OO Design Pattern

9 Đại học Công nghệ - ĐHQG HN
OO Design Pattern

10 Đại học Công nghệ - ĐHQG HN
OO Design Pattern

11 Đại học Công nghệ - ĐHQG HN
OO Design Pattern

12 Đại học Công nghệ - ĐHQG HN
OO Design Pattern

13 Decorating our beverages
Đại học Công nghệ - ĐHQG HN OO Design Pattern

14 Question Why should Beverage be an abstract class instead of an interface? Đại học Công nghệ - ĐHQG HN OO Design Pattern

15 Question Why should Beverage be an abstract class instead of an interface? Answer: Interface would work just as fine, but the existing code is an abstract class. Don’t alter existing code if it works fine. Đại học Công nghệ - ĐHQG HN OO Design Pattern

16 Question How to make a double mocha, soy latte with whip?
Đại học Công nghệ - ĐHQG HN OO Design Pattern

17 Realworld Decorator: Java I/O
Đại học Công nghệ - ĐHQG HN OO Design Pattern

18 Đại học Công nghệ - ĐHQG HN
OO Design Pattern

19 OO Design Principles We’ve learned so far Encapsulate what varies
Favor composition over inheritance Program to interfaces, not implementations Strive for loosely coupled designs between objects that interact. Open for extension, closed for modification Class is about behavior And more…. in the textbook. Đại học Công nghệ - ĐHQG HN OO Design Pattern


Download ppt "OO Design Patterns - Decorator"

Similar presentations


Ads by Google