cs427a: object-oriented programming - yale...
TRANSCRIPT
![Page 1: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/1.jpg)
CS427a: Object-Oriented Programming Design Patterns for Flexible and Reusable design
Michael J. Fischer(from slides by Y. Richard Yang)
Lecture 20November 16, 2010
![Page 2: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/2.jpg)
Reusability, Flexibility, and Maintainability
• One thing constant in software development is CHANGE
• For software that is used over a period of years, the cost of keeping it current in the face of changing needs often exceeds the cost of originally developing it.
• A key need in software design is the ability for maintenance and modification to keep abreast of changes.
![Page 3: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/3.jpg)
The Waterfall Software Processtime
RequirementsAnalysis
Design
Milestone(s)
Phases (activities)
Implementation
Testing
Maintenance
Release product X
Two phases may occur at the same time for a short period
![Page 4: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/4.jpg)
Why a Pure Waterfall Process is Usually Not Practical
Don’t know up front everything wanted and needed– Usually hard to visualize every detail in advance
To gain confidence in an estimate, we need to design and actually implement parts, especially the riskiest ones, this may probably lead to modify requirements as a result
We often need to execute intermediate builds– Stakeholders need to gain confidence– Designers and developers need confirmation they're
building what’s needed and wanted
Team members can't be idle while the requirements are being completed
![Page 5: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/5.jpg)
The Spiral Process time
1 Requirementsanalysis
Design
Coding
Testing
1Iteration #
1
1
2
2
2
3
3
3
Product released XIntermediate version* completed X
*typically a prototype
M I L E S T O N E S
2 3
2 3 1
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 6: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/6.jpg)
Advantage of OO Design
OO systems exhibit recurring structures that promote– Abstraction– Modularity– Flexibility– Extensibility– Elegance
![Page 7: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/7.jpg)
Aspect of Reusability
• Classes – in source code form– Thus, we write generic code whenever possible
• Assemblies of related classes A toolkittoolkit is a library of reusable classes designed to provide useful,
general-purpose functionality.
• E.g., C++ standard library, Boost An application framework application framework is a specific set of classes that
cooperate closely with each other and together embody a reusable design for a category of problems.• E.g., Java APIs (Applet, Thread, etc), gtkmm
• Design pattern
![Page 8: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/8.jpg)
Making a Class Re-usable
Define a useful abstraction
– attain broad applicability
Reduce dependencies on other classes
…
![Page 9: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/9.jpg)
Reducing Dependency Among Classes
Student
Replace …
Course
with …
Student CourseEnrollment
![Page 10: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/10.jpg)
Aspect of Flexibility
• Making small variation to existing functionality
• Adding new kinds of functionality
• Changing functionality
![Page 11: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/11.jpg)
Some Techniques to Achieve Flexibility
Flexibility Aspect: ability to … Some techniques… create objects in variable configurations determined at runtime “Creational” design patterns
… create variable trees of objects or other structures at runtime “Structural” design patterns
… change, recombine, or otherwise capture the mutual behavior of a set of objects
“Behavioral” design patterns
… create and store a possibly complex object of a class. Component
… configure objects of predefined complex classes – or sets of classes – so as to interact in many ways
Component
![Page 12: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/12.jpg)
Roadmap
• We will focus on flexibility and reusability– It is important to remember that real systems also
need to consider efficiency and robustness
• We will start with design patterns, and then look into the design of some OO libraries/toolkit/framework
• We will learn by examples:– Example is not another way to teach, it is the only
way to teach. -- Albert Einstein
![Page 13: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/13.jpg)
What is a Design Pattern
• Abstracts a recurring design structure• Comprises class and/or object
dependencies structures interactions conventions
• Distills design experience• Names & specifies the design structure explicitly
• Language- & implementation-independent• A “micro-architecture”
![Page 14: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/14.jpg)
14
UML/OMT Notation
![Page 15: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/15.jpg)
Example: Duck Game
• A startup produces a duck-pond simulation game
• The game shows a large variety of duck species swimming and making quacking sounds
![Page 16: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/16.jpg)
Initial Design
MillardDuck
display() { looks like a mallard}
RedheadDuck
display() { looks like a redhat}
Other types of ducks
Duck
quack()swim()display() = 0// Other duck-like method
![Page 17: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/17.jpg)
Design Change: add fly()Duck
quack()swim()display() = 0fly()// Other duck-like method
MillardDuck
display() { looks like a mallard}
RedheadDuck
display() { looks like a redhat}
Other types of ducks
![Page 18: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/18.jpg)
Problem• Generalization may lead to
unintended behaviors: a rubber duck is flying and quacks
Duck
quack()swim()display() = 0// Other duck-like method
RubberDuck
display() {looks like a rubber duck}quack() { // sqeak }fly() { // cannot fly }
MillardDuck
display() { looks like a mallard}
![Page 19: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/19.jpg)
Anticipating Changes
• Identify the aspects of your application that may vary– What may change?
• Anticipate that – new types of ducks may appear and – behaviors (quack, swimming, and flying) may
change, even change at run time (swirl flying, circular flying, …)
![Page 20: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/20.jpg)
Handling Varying Behaviors• Solution: take what varies and “encapsulate”
it– Since fly() and quack() vary across ducks, separate
these behaviors from the Duck class and create a new set of classes to represent each behavior
super class of all ducksVarying and run-time changeable behaviors
![Page 21: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/21.jpg)
Design
• Each duck object has a fly behavior
FlyWithWings
fly() { //}
FlyNoWay
fly() { // cannot fly}
<<interface>>FlyBehavior
fly()
![Page 22: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/22.jpg)
Programming to implementation vs. interface/supertype
• Programming to an implementation– Dog d = new Dog();– d.bark();
• Programming to an interface/supertype– Animal a = new Dog();– a.makeSound();
![Page 23: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/23.jpg)
Implementation
FlyWithWings
fly() { //}
FlyNoWay
fly() { // cannot fly}
<<interface>>FlyBehavior
fly()
MallardDuck
display() { looks like a mallard}
RedheadDuck
display() { looks like a redhat}
Duck
quack()swim()display() = 0// Other duck-like method
![Page 24: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/24.jpg)
Exercise
• Add rocket-powered flying?
![Page 25: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/25.jpg)
The Strategy Pattern
• Defines a set of algorithms, encapsulates each one, and makes them interchangeable by defining a common interface
![Page 26: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/26.jpg)
Exercise
![Page 27: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/27.jpg)
Summary: Design Principles
• Identify the aspects of your application that vary and separate them from what stay the same
• Program to an interface not implementation
• Favor composition over inheritance
![Page 28: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/28.jpg)
Example: KitchenViewer Interface
Wallcabinet
Counter
Floorcabinet
Modern Classic Antique Arts & Crafts
menu
display area
styles
Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.
![Page 29: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/29.jpg)
KitchenViewer Example
Modern Classic Antique Arts & Crafts
Wall cabinets Floor cabinetsCountertop
![Page 30: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/30.jpg)
Selecting Antique Style
Modern Classic Antique Arts & Crafts
![Page 31: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/31.jpg)
KitchenViewer Using Standard Inheritance
Kitchen
ClientrenderKitchen()
FloorCabinet
ModernWallCabinet
ModernFloorCabinet AntiqueFloorCabinet
AntiqueWallCabinet
WallCabinet
Aspect of the system thatmay change/vary?
: Reference direction
![Page 32: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/32.jpg)
AntiqueKStylegetWallCabinet()getFloorCabinet()
The Abstract Factory Idea
KitchenStylegetWallCabinet()getFloorCabinet()
ModernKStylegetWallCabinet()getFloorCabinet()
WallCabinet FloorCabinet
AntiqueWallCabinet AntiqueFloorCabinet
FloorCabinet getFloorCabinet() { return new AntiqueFloorCabinet(); }
……
FloorCabinet getFloorCabinet() { return new ModernFloorCabinet(); }
![Page 33: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/33.jpg)
Abstract Factory Design Pattern Applied to KitchenViewer
KitchenStylegetWallCabinet()getFloorCabinet()
KitchengetWallCabinet()getFloorcabinet()
ClientrenderKitchen( KitchenStyle )
ModernKStylegetWallCabinet()getFloorCabinet()
ModernKStylegetWallCabinet()getFloorCabinet()
AntiqueKStylegetWallCabinet()getFloorCabinet()
WallCabinet FloorCabinet
ModernWallCabinet
ModernFloorCabinet
AntiqueWallCabinet
AntiqueFloorCabinet
![Page 34: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/34.jpg)
Abstract Factory Design Pattern
StylegetComponentA()getComponentB()
ClientdoOperation( Style myStyle )
Style1getComponentA()getComponentB()
Style2getComponentA()getComponentB()
ComponentA ComponentB
Style1ComponentA
Style1ComponentB
Style2ComponentA
Style2ComponentB
Collection
![Page 35: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/35.jpg)
Concrete and Abstract Layers
KitchenStyle
Kitchen
Client
ModernKStyle
AntiqueKStyle
WallCabinet
FloorCabinet
ModernWallCabinet
ModernFloorCabinet
AntiqueWallCabinet
AntiqueFloorCabinet
Abstract level
Concrete level
![Page 36: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/36.jpg)
getWallCabinet()
Abstract Factory Application Sequence Diagram
myStyle:KitchenStyleClient
myStyle:ModernKStyle
myStyle:AntiqueKStyle
renderKitchen( myStyle )
wallCabinet1:ModernWallCabinet
wallCabinet1:AntiqueWallCabinet
ModernWallCabinet()getWallCabinet()
AntiqueWallCabinet()
myStyle.getWallCabinet()
-- IF myStyle BELONGS TO ModernKStyle --
-- IF myStyle BELONGS TO AntiqueKStyle --
![Page 37: CS427a: Object-Oriented Programming - Yale Universityzoo.cs.yale.edu/classes/cs427/2010a/lectures/ln20.pdf · need to consider efficiency and robustness • We will start with design](https://reader033.vdocument.in/reader033/viewer/2022041906/5e63b86346d38b2f5c6658cc/html5/thumbnails/37.jpg)
Potential use of this Design Pattern?
StylegetComponentA()getComponentB()
ClientdoOperation( Style myStyle )
Style1getComponentA()getComponentB()
Style2getComponentA()getComponentB()
ComponentA ComponentB
Style1ComponentA
Style1ComponentB
Style2ComponentA
Style2ComponentB
Collection