Professional Documents
Culture Documents
Object-Oriented Design
1
Notations
A B
A has (contains) B
A B
B inherits from A
A B
A uses B
A B
A uses B
2
SOLID Principles
• Acronym of acronyms:
– SRP: Single Responsibility Principle
3
History of SOLID
4
Benefits of SOLID
5
Detailed Benefits
* https://khalilstemmler.com/articles/solid-principles/solid-typescript/
6
First Pass at Understanding SOLID
• SRP: “A class should have one, and only one,
reason to change”.
GUI
8
SRP: Cont’d
Computational Rectangle Graphical
Geometry App
App
Geometric
Rectangle GUI
9
SRP: Another example
– Connection-related
– Data communication-related
10
SRP: Summary
11
OCP: Open-Closed Principle
• “All systems change during their life cycles.” (Ivar
Jacobson).
12
OCP: Abstraction is Key
• Bad design: need to
change client code when new kinds of server needed.
Client Server
Server
13
OCP: Data-Driven Approach
14
OCP: Foundation for Many
Heuristics
• etc.
15
OCP: Summary
16
LSP: Liskov Substitution Principle
17
LSP: Example
Rectangle Class ←− Square Class
19
LSP: Summary
20
ISP: Interface Segregation Principle
• “Clients should not be forced to depend upon
interfaces that they do not use.”
21
ISP: Example
• Bad design
TimerClient
Door
TimedDoor
• Good design
Abstract Abstract
Door TimerClient
Door TimerClient
Timed DoorTimer
Door Adapter
Timed
Door
23
ISP: Summary
24
DIP: Dependency Inversion Principle
• “A. High level modules should not depend upon low
level modules. Both should depend upon
abstractions.”
25
DIP: The Problem
• Bad design:
– Hard to change (rigidity)
– Unexpected parts break when changing code
(fragility)
– Hard to reuse (immobility)
27
DIP: Diagnosis of Copy()
• Module containing high level policy (Copy) is
dependent upon low level detailed modules it
controls (WritePrinter, ReadKeyboard).
• Good design:
Copy
Abstract Abstract
Reader Writer
Keyboard Printer
Reader Writer
28
DIP: Layering and Better Layering
• Bad Design
Policy Layer
Mechanism Layer
Utility Layer
• Good Design
Abstract
Policy Layer Mechanism
Interface
Mechanism Abstract
Layer Utility Interface
Utility Layer
• Good Design
Abstract Abstract
Button ButtonClient
Button Lamp
Implementation
30
Button Example – Explained
31
DIP: Summary
32
SOLID Principles: Summary
• Abstraction is important
33