+ All Categories
Home > Documents > Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the...

Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the...

Date post: 31-Dec-2015
Category:
Upload: grant-houston
View: 218 times
Download: 0 times
Share this document with a friend
Popular Tags:
14
Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.
Transcript
Page 1: Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.

Albert Einstein

Two things are infinite: the universe & human stupidity;

and I'm not sure about the universe.

Page 2: Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.

CSC 313 – Advanced Programming Topics

Page 3: Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.

Open-Closed Principle

Classes should be open for extension,but closed to modification

So, what does this mean?

Page 4: Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.

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

Page 5: Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.

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

Page 6: Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.

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

Page 7: Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.

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

Page 8: Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.

Warning

Remainder of lecture unsafe for:Beginning programmersUnwilling to consider new viewpointsStubbornly dogmaticStupidLiberal arts majors

Page 9: Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.

Problem At Hand

Page 10: Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.

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

Page 11: Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.

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

Page 12: Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.

Decorator Pattern Intent

Page 13: Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.

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

Page 14: Albert Einstein Two things are infinite: the universe & human stupidity; and I'm not sure about the universe.

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?


Recommended