Download presentation
Presentation is loading. Please wait.
Published byGrant Houston Modified over 9 years ago
1
Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.
2
CSC 313 – Advanced Programming Topics
3
Open-Closed Principle Classes should be open for extension, but closed to modification So, what does this mean?
4
Reality Check Classes should be open for extension… Other’s code can be updated Other is important emphasis Everyone is other to someone Easily create subclasses to: Add functionality Include additional fields Specialize behavior via overriding
5
More Reality Check …But closed to modification Nobody better mess with your code YOUR code generally being beauty incarnate Only add errors & soil your perfection Never let anyone: Add functionality violating its basic assumptions Alter the tasks it performs Anything that might introduce bugs
6
Strategy Pattern & OCP Pattern stays true to open-closed principle Context remains open for extension Strategies add new functionality Subclasses provide additional fields But also remains closed to modification Strategies must use data provided to them Can only perform actions for which defined
7
Observer Pattern & OCP Observer pattern also principle devotee Subject remains open for extension Observers provide ways of handling events In pull model, extend Subject to provide more information to Observers But also remains closed to modification Observers cannot ACT, but only RE-ACT Subject chooses events to expose to Observers
8
Warning Remainder of lecture unsafe for: Beginning programmers Unwilling to consider new viewpoints Stubbornly dogmatic Stupid Liberal arts majors
9
Problem At Hand
10
Add fields for properties Add field for each possible property Does double whip, decaf, soy water make sense? How to handle taxes for multiple states? Adding fields is also inefficient Must modify class for each new property Creates huge number of rarely used fields Class remains open for anyone to play with Violates the open close principle
11
Create subclasses Define subclass for each possible situation Need both MochaWhipCoffee & WhipMochaCoffee? Volunteers writing all pizza topping combinations? Does not violate open-close principle But code duplicated in many places Who’ll modify classes to update New York tax rate? Maintaining this code will be major pain in the ass
12
Decorator Pattern Intent
13
Decorator Pattern Usage Flexibly add adjectives to main class’s noun Decorations provide additional description Each decoration not always required Wrappings can be stacked in any order Add adverbs to enhance main class’s actions Improve result using additional information Include additional tasks that must be performed Invisibly augment main class instances
14
For Next Lecture Lab #3 available on web/Angel Asks you to implement the Observer Pattern Due before lab on Tuesday Read pages 95 – 105 in the book How do we implement the DESIGN PATTERN? Can anyone actually use this? What is the main point of this pattern?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.