Overview Object-Oriented Design Principles and Design Patterns... OO Design Principles• To create reusable, extensible and maintainable OO systems • Underlie the patterns, help you to
Trang 1Overview Object-Oriented Design Principles
and Design Patterns
Trang 2What goes wrong with software?
• You start with a clear picture in your mind what the software should be The clarity of the design helps you to get to the first release.
• Then something goes wrong,
the software starts to rot:
– Changes and additions are harder
and harder to make,
– You are forced to let go of the original design, eventually even the simplest changes terrify you because of rippling unexpected effects, and you must redesign the whole
software
Trang 3Bad signs, symptoms of rotting design
• Rigidity (Sự cứng nhắc))
– Code difficult to change (no continuity)
– Changes propagate via dependencies
• Fragility (Sự yếu ớt)
– Even small changes can cause cascading effects
– Code breaks in unexpected places having no conceptual relationship with the area that was changed (no protection)
• Immobility (Sự không linh động)
– Code is so tangled that it's impossible to reuse anything – No composability
Trang 4Bad signs, symptoms of rotting design
• Viscosity
– Design preserving ways of working are harder than hacks – Developement environment is slow and inefficient
• Needless complexity (Sự phức tạp không cần thiết)
– Design contains elements that are not currently useful
– Results from too much anticipating future needs
• Opacity (Sự tối nghĩa)
– The tendency of a module to become more difficult to
understand
– A constant effort is needed to keep code readable
Trang 5Causes of rotting design
• Inevitably changing requirements
– "All systems change during their life-cycles
This must be born in mind when developing systems
expected to last longer than the first version."
Ivar Jacobson, OOSE 1992
• Poor dependency management
– Dependencies can be controlled
• Tools:
– OO-design principles, desing patterns
– Guidelines: coupling, cohesion
Trang 6Object-Oriented Design Principles
Trang 7OO Design Principles
• To create reusable, extensible and
maintainable OO systems
• Underlie the patterns, help you to cope changes.
• Design Principles
– Encapsulate what varies
– Favor Composition Over Inheritance
– Program To An Interface, Not An Implementation
– (SRP) The Single Responsibility Principle
– (OCP) The Open Closed Principle
– (LSP) The Liskov Substitution Principle
– (DIP) The Dependency Inversion Principle
– (ISP) The Interface Segregation Principle
Trang 8Design Patterns
Trang 9What are Patterns ?
• The 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 (Alexander)
• The pattern is at the same time a thing, which happens
in the world, and the rule which tells us how to create
that thing, and when we must create it (R Gabriel)
• A pattern involves a general discription of a recurring
solution to a recurring problem with various goal and
constraints It identify more than a solution, it also
explains why the solution is needed (J Coplien)
Trang 10Learning Patterns
• Successful solutions to many areas of human
endeavor are deeply rooted in patterns
– In fact, an important goal of education is transmitting
patterns of learning from generation to generation
• Learning to develop good software is similar to
learning to play good chess!
– Though the consequences of failure are often far less
dramatic!
Trang 11Becoming a Chess Master
• First learn the rules
– e.g., names of pieces, legal movements, chess board
geometry and orientation etc
• Then learn the principles
– e.g., relative value of certain pieces, strategic value of
center squares, power of a threat etc
• However, to become a master of chess, one must study the games of other masters
– These games contain patterns that must be understood, memorized and applied repeatedly
• There are hundred of these patterns
Trang 12Becoming a Software Design Master
• First learn the rules
– e.g., the algorithms, data structures and languages of
software
• Then learn the principles
– e.g., structured programming, modular programming,
object-oriented programming, generic programming,
principles of OO etc
• However, to become a master of software design, one must study the designs of other masters
– These designs contain patterns that must be understood,
Trang 13Overview of Patterns and Frameworks
• Patterns support reuse of software architecture and design
– Patterns capture the static and dynamic structures and
collaborations of successful solutions to problems that arise when building applications in a particular domain.
• Frameworks support reuse of detailed design and code
– A framework is an integrated set of components that collaborate to provide a reusable architecture for a family of related applications
• Together design patterns and frameworks help to improve software quality and reduce development time
– E.g reuses, extensibility, modularity, performance
Trang 14Design Patterns
• Design patterns represent solutions to problems that arise when developing software within a particular context
– “Pattern == problem/solution pair in a context”
• Patterns capture the static and dynamic structure
and collaboration among key participants in
software designs
– They are particularly useful for articulating how and why to resolve non-functional forces
• Patterns facilitate reuse of successful software
Trang 15Design Pattern Space
• Creational Patterns
– Deal with initializing and configuring classes and objects
• Structural Patterns
– Deal with decoupling interface and implementation of classes and objects.
• Behavioral Patterns
– Deal with interactions among classes and objects
• Scope: what the pattern applies to
– Class Patterns
• Focus on the relationships between classes and their subclasses
• Involve inheritance reuse
– Object Patterns
• Focus on relationships between objects
• Involve composition reuse
Trang 16Design Pattern Space