OO Summary
OO Principles:
- Encapsulation – information and implementation hiding
- Inheritance – interface and implementation inheritance
- Polymorphism – ability of different objects to respond differently
to the same message. Inheritance polymorphism, operational polymorphism.
Design Principles:
- Single Responsibility Principle (SRP) – cohesion
- Open/Closed Principle – open to extension but closed to modification
- Liskov Substitution Principle – subclass can be substitute for their
base classes. Design by contract – polymorphic method of a subclass
can only replace its pre-condition by a weaker one and its
post condition by a stronger one. - Dependency Inversion Principle (DIP) – high level modules shouldn’t depend
on low-level modules. abstraction should not depend on details.
dependencies should point in the direction of abstraction. - Interface Separation Principle
Stability and Instability
- Acyclic Dependency Principle – prohibits cyclic dependencies among packages
The dependencies among mdoules must be arranged in Directed Acyclic Graph - Package Cohesion
- Common Closure Principle – classes that change together belong together
Code with different kinds of volatility should be placed in different modules.
Modules should not depend on other modules that are more volatile than they
are.
volatile code goes into instable modules, vice verse. - Reuse/Release Equivalency Principle – release granularity equal to reuse granularity
- Common Reuse Principle – classes that aren’t reused jointly shouldn’t be grouped together
- Common Closure Principle – classes that change together belong together
- Stable Dependency Principle – reinforce package stability
no modules should depend on a module that is less stable than it is
Ce # of modules that this module depend on
Ca # of modules that depend on this module
Instability (I) = Ce / (Ca – Ce)
If I is 1 the module is instable and 0 means module is very stable. - Stable Abstraction Principle – stable packages should be abstract
more stable module is, the more abstract it should be
Abstractness = Na / Nc, where Na is the number of abstract/interfaces and Nc
is number of classes in the module
D = |A + I – 1|
0 means module is on the main sequence and 1 means it is far from the sequence
Golden Rules
- it contains no dependencies cycles (ADP)
- Every dependency between modules should terminate on a module whose I metric is less than or equal to the depending mdoules’ I metric (SDP).
- Every dependency between modules should terminate on a module whose A metric is greater than or equal to the depending module’s A metric (SAP).