Object Oriented Programming Ch3 SRP, DIP, ISP
-
Upload
chihyang-li -
Category
Software
-
view
58 -
download
0
Transcript of Object Oriented Programming Ch3 SRP, DIP, ISP
![Page 1: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/1.jpg)
Object Oriented Programming in Swift
Ch 3 Single Responsibility Principle
Dependency Inversion Principle Interface Segregation Principle
Sai Li @ Yowoo Tech. 2016/1/9
![Page 2: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/2.jpg)
References • Agile Software Development: Principles, Patterns,
and Practice ( : )by Robert C. Martin
• Fundamental Object Oriented Programming by Hsuan-Tien Lin
• by Teddy Chen
• Essential Object-Oriented Programmingby Josh Ko
![Page 3: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/3.jpg)
Cohesion & Coupling
![Page 4: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/4.jpg)
Cohesion• Degree to which the elements of a module belong
together • The force that cause a module or a class to change
![Page 5: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/5.jpg)
Coupling
• Degree of interdependence between software modules
![Page 6: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/6.jpg)
Cohesion & Coupling
![Page 7: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/7.jpg)
Cohesion & Coupling
Goal: high cohesion, low coupling
![Page 8: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/8.jpg)
S.O.L.I.D
• SRP: Single Responsibility Principle • OCP: Open Closed Principle• LSP: Liskov Substitution Principle • ISP: Interface Segregation Principle • DIP: Dependency Inversion Principle
![Page 9: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/9.jpg)
Single Responsibility Principle
“A class should have one,
and only one, reason to change.”
![Page 10: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/10.jpg)
SRP
![Page 11: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/11.jpg)
SRP
![Page 12: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/12.jpg)
Responsibility
• A reason for change • More than one responsibility
• coupled responsibilities • Fragile Design
![Page 13: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/13.jpg)
Example
![Page 14: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/14.jpg)
Needless Complexity? When?
![Page 15: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/15.jpg)
Persistence
TDD
![Page 16: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/16.jpg)
Single Responsibility Principle
• One of the simplest and hardest principle • Software Design: The art of separating responsibility
![Page 17: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/17.jpg)
S.O.L.I.D
• SRP: Single Responsibility Principle • OCP: Open Closed Principle• LSP: Liskov Substitution Principle • ISP: Interface Segregation Principle • DIP: Dependency Inversion Principle
![Page 18: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/18.jpg)
Dependency Inversion Principle
1. High-level modules should not depend on low-level modules. Both should depend on abstractions
2. Abstractions should not depend on details. Details should depend on abstractions.
![Page 19: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/19.jpg)
Inversion of Control
low level change -> high level change
![Page 20: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/20.jpg)
Hollywood Principle: Don’t call me, I’ll call you
Inversion of ControlWhat they need
![Page 21: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/21.jpg)
Switch+ switch()
SwitchableDevicePolicy
![Page 22: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/22.jpg)
![Page 23: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/23.jpg)
– Robert C. Martin
“Depend on abstractions,
not on concretions.”
![Page 24: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/24.jpg)
- Design Pattern , GoF
“Program to an interface,
not an implementation.”
![Page 25: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/25.jpg)
S.O.L.I.D
• SRP: Single Responsibility Principle • OCP: Open Closed Principle• LSP: Liskov Substitution Principle • ISP: Interface Segregation Principle • DIP: Dependency Inversion Principle
![Page 26: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/26.jpg)
Interface Pollution
![Page 27: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/27.jpg)
Interface Pollution
![Page 28: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/28.jpg)
Adaptor
![Page 29: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/29.jpg)
Interface Segregation Principle
“Client should NOT be forced to depend upon interfaces that they do not use”
![Page 30: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/30.jpg)
Glass door
Separate Clients == Separate Interfaces
![Page 31: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/31.jpg)
![Page 32: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/32.jpg)
![Page 33: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/33.jpg)
“Fat” Interface
• More than one responsibility • Coupled responsibilities • Fragile Design
• Clients depend on what they need only
![Page 34: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/34.jpg)
Recap
• SRP: A class should have one, and only one, reason to change.
• DIP: Depend on abstractions, not on concretions.
• ISP: Make fine grained interfaces that are client specific
![Page 35: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/35.jpg)
Recap Liskov Violation
![Page 36: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/36.jpg)
Liskov Substitution Principle
Subtypes must be substitutable for their base types.
![Page 37: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/37.jpg)
Rectangle & Square Problem
![Page 38: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/38.jpg)
![Page 39: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/39.jpg)
Segment
Line
Ray
![Page 40: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/40.jpg)
Line: isOn(point: Intercept) == trueLineSegment: isOn(point: Intercept) == ?
![Page 41: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/41.jpg)
![Page 42: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/42.jpg)
![Page 43: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/43.jpg)
It’s the endof the beginning
![Page 44: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/44.jpg)
– Sai Li
“Live for interfaces, not implementations, except Lovers”
![Page 45: Object Oriented Programming Ch3 SRP, DIP, ISP](https://reader034.fdocuments.us/reader034/viewer/2022052514/58735cfe1a28abe7648b4cb7/html5/thumbnails/45.jpg)
References• https://en.wikipedia.org/wiki/Cohesion_(computer_science) • https://drive.google.com/file/d/0ByOwmqah_nuGNHEtcU5OekdDMkk/view?pli=1 • http://teddy-chen-tw.blogspot.tw/2011/12/1.html • http://www.cs.utexas.edu/users/downing/papers/DIP.1996.pdf • http://howard-lyrics.blogspot.tw/2011/04/test.html • http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod • https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Laptop-hard-drive-exposed.jpg/220px-Laptop-hard-drive-
exposed.jpg • http://sce2.umkc.edu/BIT/burrise/pl/design/cohesion-coupling-abstract.gif • http://freefeast.info/wp-content/uploads//2013/08/cohesion-vs.-coupling.jpg • http://cdn.mos.cms.futurecdn.net/7047e9b7bfca2b3b6f352495885e77c2.jpg • https://upload.wikimedia.org/wikipedia/commons/8/8d/DIPLayersPattern.png • http://flylib.com/books/4/444/1/html/2/images/11_3.jpg • http://flylib.com/books/4/444/1/html/2/images/11_4.jpg • http://stg-tud.github.io/sedc/Lecture/ws13-14/Images/DIP-RegulatorBase.png • http://stg-tud.github.io/sedc/Lecture/ws13-14/Images/DIP-RegulatorDIPCompliant.png • http://www.coolmath.com/sites/cmat/files/images/03-lines-01.gif • http://flylib.com/books/4/444/1/html/2/images/12_2.jpg • http://flylib.com/books/4/444/1/html/2/images/12_1.jpg