Design I

advertisement
Design I
Properties of good design
•
•
•
•
•
•
•
•
•
•
•
Minimize complexity
Maintainable
Loose coupling
Extensibility
Reusability
High fan in
Low to medium fan out
Portability
Leanness
Stratification
Standard techniques
Some principles of good design
•
•
•
•
•
Open-Close Principle
Dependency Inversion Principle
Interface Segregation Principle
Single Responsibility Principle
Liskov’s Substitution Principle
Design heuristics
1. Use real-world and synthetic objects
a) identify objects & their attributes
b) determine relationships
c) how they interact to realize use cases
2. Refine your classes based on
responsibilities
1. don’t let an class do too much
2. keep it cohesive
3. identify areas of potential change
Example
• I am developing software that (among other
things) displays geometric primitives
• Initially I only need to support lines
• In the future I may need to add spheres,
triangles, and squares
• Rather than reinvent the wheel, I am going to
use an existing program.
• Actually, I really want the option of choosing at
run time between two different drawing
programs, one that is better at low resolutions
and one that is better at high resolutions
current design
shape
line
draw()
Drawing APIs
Package 1:
drawLine(x1, y1, x2, y2)
Package 2:
drawALine(x1, x2, y1, y2)
solution 1
line.draw() {
if (resolution == high)
drawLine(v1.x, v1.y, v2.x, v2.y)
else
drawALine(v1.x, v2.x, v1.y, v2.y)
}
solution 1
• Advantages
• Disadvantages
solution 1
• Advantages
– simple to implement
– simple to understand
• Disadvantages
– is knowing about
resolution really a
responsibility of
shape?
– we are duplicating
code
Some principles of good design
•
•
•
•
•
•
Open-Close Principle
Dependency Inversion Principle
Interface Segregation Principle
Single Responsibility Principle
Liskov’s Substitution Principle
No forgery principle (keep data in a single
place)
• One rule one place (don’t duplicate code)
solution 2
line
lineDP1
lineDP2
solution 2
• Advantages
– simple to implement
– simple to understand
• Disadvantages
– as additional shapes
and drawing
programs are added
the number of
classes becomes
LARGE
Bridge design pattern
Drawer
Shape
Line
defines the interface
shapes use to draw
Triangle
Hi Res
Low Res
“adapters” for specific
drawing interfaces
Bridge
Problem: Want to support multiple
implementation that have different
interfaces in an extensible way.
Last time
• Choose a game loop at some specific part
of your (core) game
• Develop a domain model
– what are the things (nouns)
– what are their relationships
• Develop your game loop use case
• Use CRC cards to come up with a first
pass at your design
• Act out the game loop use case
This time
• Critique your design
– based on design principles
– properties of good design
– design heuristics
• Report to class
Download