Date post: | 31-Dec-2015 |
Category: |
Documents |
Upload: | grant-houston |
View: | 218 times |
Download: | 0 times |
Albert Einstein
Two things are infinite: the universe & human stupidity;
and I'm not sure about the universe.
CSC 313 – Advanced Programming Topics
Open-Closed Principle
Classes should be open for extension,but closed to modification
So, what does this mean?
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
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
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
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
Warning
Remainder of lecture unsafe for:Beginning programmersUnwilling to consider new viewpointsStubbornly dogmaticStupidLiberal arts majors
Problem At Hand
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
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
Decorator Pattern Intent
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
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?