Object-Oriented Design Principles and Patterns

advertisement
Object-Oriented Design Principles
and Patterns
How Do You Design?
• What principles guide you when you create a
design?
• What considerations are important?
• When have you done enough design and can
begin implementation?
Take a piece of paper and write down two
principles that guide you - considerations that are
important or indicators that you have a good
design.
Object-Oriented Design Principles and
Patterns
© 2005, James R. Vallino
2
What Have You Heard About Design?
• First principles of OOP




Find the nouns  objects/state
Find the verbs  behaviors; methods/functions
Encapsulation, inheritance
Programming
• Object Interactions
 Static  collaboration diagrams
 Dynamic  sequence diagrams
Object-Oriented Design Principles and
Patterns
© 2005, James R. Vallino
3
What is the Next Level of Design?
• How do you decide on the class collaborations?
 Simple: Follow principles of design
• But there are problems here!
 What if the principles conflict with each other?
– A good class decomposition may have too many connections
between classes
– Strong encapsulation may lead to performance penalties
 Does each new design start from first principles?
– Abstraction has been our friend
– It can help again
 How can we reliably follow the principles?
– Learn from previous designers
– Stand on each others' shoulders, not each others' toes.
Object-Oriented Design Principles and
Patterns
© 2005, James R. Vallino
4
Key Design Concepts
• General
 Cohesion
 Coupling
 Information hiding
– Encapsulation
– Creation
 Binding time
• OO Specific
 Behaviors follow data
 Class vs. Interface Inheritance
– Class = implementation
– Interface = type
 Inheritance / composition / delegation
Object-Oriented Design Principles and
Patterns
© 2005, James R. Vallino
5
Principles of Successful Design
• Increase cohesion where possible
 Separation of concerns
 Focused experts
• Decrease coupling where possible
 Simplify interfaces
 Reduce connections in number and volume
• Employ and support reuse
 Reuse existing designs and code where possible
 Increase reusability where possible
 Design for portability
Object-Oriented Design Principles and
Patterns
© 2005, James R. Vallino
6
Principles of Successful Design – 2
• Design for Flexibility
 Orthogonal evolution in many dimensions
 Anticipate obsolescence
• Design for testability
• Use priorities and objectives to decide among
alternatives
Object-Oriented Design Principles and
Patterns
© 2005, James R. Vallino
7
Principles of Successful OO Design
• Prefer type (interface) inheritance over class
(implementation) inheritance.
• Program to the interface, not the
implementation.
• Prefer composition to inheritance:
 Deferred binding
 Cost: Performance
• Use delegation to “simulate” runtime
inheritance.
Object-Oriented Design Principles and
Patterns
© 2005, James R. Vallino
8
What Are Patterns?
Each pattern describes a problem which occurs over and over
again in our environment, and then describes the core of the
solution to that problem, in such a way that you can use this
solution a million times over, without ever doing it the same
way twice.
Christopher Alexander
A pattern is a general solution to a problem in a context
• general -- outline of approach only
• problem -- a recurring issue
• context -- consider the expected design evolution
Object-Oriented Design Principles and
Patterns
© 2005, James R. Vallino
9
Do You Reinvent the Wheel Each Time?
• Consider coding idioms!
• How do you walk through an array in Java?
for (i = 0; i < array.length; i++) {
// use the array element
}
Object-Oriented Design Principles and
Patterns
© 2005, James R. Vallino
10
What Are Design Patterns About?
• Standard patterns of interactions between
classes
 Design patterns
• How to apply them to your application
 Deal with subsystems at the higher level of abstraction
provided by the patterns
• What to do when it does not fit exactly
 Evaluate options and analyze the trade-offs
Object-Oriented Design Principles and
Patterns
© 2005, James R. Vallino
11
Level for Design Patterns
• Higher than what you've done before
 Not specific data structures
 Not algorithmic approaches
• Lower than whole architectures or
frameworks
 Not financial systems
 Not air-traffic control
 Not J2EE
• Interactions of 3-10 classes in solution
domain, i.e., the small subsystem
Object-Oriented Design Principles and
Patterns
© 2005, James R. Vallino
12
Why Patterns?
• Design for re-use is difficult
• Experienced designers:
 Rarely start from first principles
 Apply a working "handbook" of approaches
• Patterns make this ephemeral knowledge
available to all
• Support evaluation of alternatives at higher
level of abstraction
Object-Oriented Design Principles and
Patterns
© 2005, James R. Vallino
13
Design Activity
• Quick design activity
• This class
 Individually create a design for the stated problem
 Collaborate with others, if desired
• Next class
 Submit individual design at start of class
 Groups of students create a consensus design
 Designs will be presented
 Designs will be compared and contrasted
Object-Oriented Design Principles and
Patterns
© 2005, James R. Vallino
14
Download