Tải bản đầy đủ (.pptx) (16 trang)

OO principles, patterns (THIẾT kế đối TƯỢNG SLIDE)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (291.22 KB, 16 trang )

Overview Object-Oriented
Design Principles
and Design Patterns

1


What 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.

• What went wrong?
2


Bad 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

3


Bad 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

4


Causes 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

5


Object-Oriented Design Principles

6


OO 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
7


Design Patterns

49


What 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)
50


Learning 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!

51


Becoming 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
52


Becoming 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,
memorized and applied repeatedly

• There are hundred of these patterns
53


Overview 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

54


Design 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
architectures and designs.
55



Design 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
56


Design Pattern Space


57



×