Relations, Coupling, Cohesion, and Refactoring Relations
Relations, Coupling, Cohesion, and Refactoring Relations
refactoring
type-hierarchies - organize semantically similar entities
Real-world objects related in many ways =>
other ways to abstract reality
Objects/classes need other objects/classes
Accessing functionality of other objects
Lecture 9: OOP, autumn 2003 Pass other object (its reference) as method parameter
Use super-class’s functionality
Access static methods
Use relationships between objects
3 4
association class
5 6
1
Aggregation Composition
Part-whole relationship Like aggregation
Objects exist independently But stronger - lifetime of part controlled by whole
No cycles in objects (anti-symmetry) Aggregate cardinality == 1
Implement through object references
Implement through object containment
7 8
9 10
11 12
2
Code duplication Cohesion (1)
Sign of bad design A module should provide a well-defined task (service)
A module should have well-defined responsibilities
High cohesion facilitates reuse (well-defined modules)
Same code in two places => high coupling High cohesion simplifies modification (all relevant code in one
place)
High cohesion low coupling to other modules (but high coupling
Makes it difficult to modify and extend
within the module..)
Reducing coupling higher cohesion
Duplicated code often is an indication that a
When should we subdivide (and thus try to attain low coupling
better solution (in the implementation) is
and between the different parts)?
possible (cf. “refactoring”) Subdivide when: You cannot keep all aspects of the module in
memory
13 14
17 18
3
Factorization by inheritance Factorization by delegation
19 20
23