Академический Документы
Профессиональный Документы
Культура Документы
and II
Design Principles*
LN Set #7
* Some slides are shared by Dr. Bimlesh and are used in CS3219
30/8/2016
A muddy set of
requirements
30/8/2016
Design Principles
30/8/2016
Design Principles
Abstraction
Information hiding
Cohesion
Coupling
Redundancy
Separation of Concerns (SoC)
Design to Interface (D2I)
30/8/2016
Abstraction
An abstraction denotes the essential characteristics of
an object that distinguish it from all other kinds of object
and thus provide crisply defined conceptual
boundaries, relative to the perspective of the viewer.
Abstraction denotes a model, a view, or some other
focused representation for an actual item.
G. Booch,
leader in object-oriented analysis and the development of
the Unified Modeling Language [UML]
30/8/2016
Abstraction
Manage complexity
Emphasize essential characteristics
Suppress implementation details
30/8/2016
Cohesion
Quality of single-mindedness or singleness
of purpose
How much (or how little) the internal parts of
something are working on the same issue
and how well they relate to each other
Signs of weak cohesion:
Difficulty in naming
Large tests
Large classes and methods
30/8/2016
Method Cohesion
Be as cohesive as possible in levels of
perspective
Methods accomplish functionality by
Code logic in the method (implementation
perspective)
Calling other methods (specification perspective)
Martin Fowlers Perspectives :
conceptual, specification, implementation
30/8/2016
10
Fowlers perspectives
Conceptual
What I am responsible for?
Specification
How I am used by others ?
Implementation
How I accomplish my responsibilities ?
Martin Fowlers Blog:
http://www.martinfowler.com/
30/8/2016
11
Class Cohesion
A strongly cohesive class is difficult to break
up into multiple classes.
Ask :
How hard it would be?
How much of each component class would have
to be exposed through public methods to other
component classes ? If the number is high, then
likely the class is cohesive to begin with.
30/8/2016
12
Coupling
One part of a system affects another part of
the system
Its not bad in all cases
We do not want coupling that
We did not intend
We did intend but we dont actually need
13
Coupling
Indicators of accidental coupling
Unexpected side effects of a change
Hesitancy in making a change
Comments about what the code is doing
30/8/2016
14
But not:
low cohesion and
high coupling
15
30/8/2016
17
SoC
Understand one concern separately from
other concerns
Some concerns are easier to separate than
other:
User interface from business logic
But, it is difficult to separate performance concern
from other aspects of a software system (i.e.,
other concerns)
18
SoC
Example: Loan Quote System(LQS) Roles
When shopping for a loan, a customer may call several banks to find the
deal with the best possible interest rate. Each bank asks the customer for
his/her IC number, the amount of the loan and the desired term. Each bank
then investigates the customers credit background and responds with an
interest rate quote to the customer (or declines). Once the customer has
received quotes from all banks, he or she can then select the best offer.
Because contacting multiple banks is a tedious task, loan brokers offer this
service to customers. A loan broker is not affiliated with any one bank but
has access to many lending institutions. The broker gathers the customer
data once and contact the credit bureau to obtain the customers credit
history. Based on the credit history, the broker presents the request to a
number of banks that are best suited to meet the customers criteria. The
broker gathers the resulting quotes from the banks and select the best offer to
30/8/2016
19
pass
back to the customer.
SoC
Example: Loan Quote System(LQS)
Responsibilities and Interactions
21
30/8/2016
22
Dependency
Inversion (DI)
SOLID
(SRP, OCP,
LSP, IS, DI)
30/8/2016
Liskov
Substitution
Principle (LSP)
Redundancy
Dont Repeat
Yourself (DRY)
CS3201_02 LN Set #7 Design Principles
Open Close
Principle
(OCP)
Design to
Interface (D2I)
24
Techniques
Decomposition, modularization, layered design
Low coupling and high cohesion
Information hiding and APIs
Inheritance (sometimes)
Parameterization
Function parameters
Class: generics (Java) or templates (C++)
Typedef
Table-driven technique
Store many related parameters in a data structure (table)
Implement generic code that refers to the parameter table
CS3202 Set#3 Design
26
How to Decompose?
This is the key to success
Most experienced developers in a company do that!
27
Why?
28
29
Public: interface
Hidden, Private:
implementation
30
Reminder
Do not shift all the principles to extremes,
because in real cases it is impossible to
achieve them from all point of views.
--The End--