Upload
rebecca-fitzgerald
View
217
Download
1
Embed Size (px)
Citation preview
ANU COMP2110 Software Design in 2003 Lecture 16 Slide 1
Lecture 16: Introduction to design patterns
1 What are they?
2 Where do they come from?
3 Why study them?
4 What are they again?
5 More thoughts:
• Abstraction
• Don’t reinvent the wheel
• Consequences
ANU COMP2110 Software Design in 2003 Lecture 16 Slide 2
What are design patterns?
First, some examples of what they are not:
• adjustable templates like dress patterns
• as in “pattern matching”, like regexps
• repeated motifs like in art or music, e.g.• Islamic decorative art• harpsichord cadenza from the 5th
Brandenburg concerto• jazz riffs
ANU COMP2110 Software Design in 2003 Lecture 16 Slide 3
So what are design patterns?
Examples of reusable, excellent object-oriented software design, together with
• the context in which each one applies
• the problem it tries to solve
• the forces at work
• the consequences of adopting it
• hints for implementation
ANU COMP2110 Software Design in 2003 Lecture 16 Slide 4
What is the origin of design patterns?
• Started in architecture with the work of Christopher Alexander and his group at Berkeley.
• He asked: What makes great buildings great and awful buildings awful? When great buildings can be so different, what do they have in common?
• He looked very hard for examples and principles, working at a very subtle level
• Looked at different ways to solve the same problem
• Came up with a whole system for design
ANU COMP2110 Software Design in 2003 Lecture 16 Slide 5
What does this have to do with software?
• In the early 90’ssome software people read Alexander’s work and saw a parallel
• The same problems occur again and again and can be solved the same way
• These examples of partial designs can be reused at the level of ideas, not of code
• Gamma, Helm, Johnson & Vlissides wrote the “Gang of Four” book
• An explosion of interest and activity since then
ANU COMP2110 Software Design in 2003 Lecture 16 Slide 6
Why study design patterns?
• To learn some examples of great design that you can use
• To create a common vocabulary for talking about design
• To help you think about design at a higher level of abstraction
• To learn from them, so that you can apply the same principles to create great designs of your own
ANU COMP2110 Software Design in 2003 Lecture 16 Slide 7
What is a design pattern again?
A solution to a problem in a context
Example: The Observer patternContext: Many objects, of varying number and different types, depend on another object and need to stay synchronised with it.Problem: How to manage that without having to change all the code every time a new type of observer comes along.Solution: See Chris’s lecture, textbook, GoF book
ANU COMP2110 Software Design in 2003 Lecture 16 Slide 8
More thoughts: Abstraction
• Abstraction is about choosing what details to leave out
• Just keep the important ones
• To understand and apply design patterns well, you have to learn to do this
• How hard or easy this is for you depends on your personality type
ANU COMP2110 Software Design in 2003 Lecture 16 Slide 9
More thoughts: Don’t reinvent the wheel
Name: Wheel
Intent: Transport heavy loads long distances
Problem: They’re hard to carry, but it’s really important to move stuff like food around
Solution: Build vehicles with wheels, and smooth roads for them to run on
Consequences: Greatly increased capacity and speed. Need roads. Control of roads = power...
Implementation: Another time...
Alternatives: Carry it, drag it, use a boat or barge
ANU COMP2110 Software Design in 2003 Lecture 16 Slide 10
More thoughts: Consequences
• Need to think in terms of cause and effect
• There is no absolute good and bad, only choices and their consequences
• In software we measure quality against requirements (and meta-requirements like extendability and reusability that often aren’t written down)
• When you make design decisions, look at the practical consequences
• Using a pattern isn’t always a good decision!
ANU COMP2110 Software Design in 2003 Lecture 16 Slide 11
More thoughts: Principles
• Prefer composition over inheritance
• Find what varies and encapsulate it
• Improve modifiability