Object Oriented Programming Ch3 SRP, DIP, ISP

45
Object Oriented Programming in Swift Ch 3 Single Responsibility Principle Dependency Inversion Principle Interface Segregation Principle Sai Li @ Yowoo Tech. 2016/1/9

Transcript of Object Oriented Programming Ch3 SRP, DIP, ISP

Page 1: Object Oriented Programming Ch3 SRP, DIP, ISP

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

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

Cohesion & Coupling

Page 4: Object Oriented Programming Ch3 SRP, DIP, ISP

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

Coupling

• Degree of interdependence between software modules

Page 6: Object Oriented Programming Ch3 SRP, DIP, ISP

Cohesion & Coupling

Page 7: Object Oriented Programming Ch3 SRP, DIP, ISP

Cohesion & Coupling

Goal: high cohesion, low coupling

Page 8: Object Oriented Programming Ch3 SRP, DIP, ISP

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

Single Responsibility Principle

“A class should have one,

and only one, reason to change.”

Page 10: Object Oriented Programming Ch3 SRP, DIP, ISP

SRP

Page 11: Object Oriented Programming Ch3 SRP, DIP, ISP

SRP

Page 12: Object Oriented Programming Ch3 SRP, DIP, ISP

Responsibility

• A reason for change • More than one responsibility

• coupled responsibilities • Fragile Design

Page 13: Object Oriented Programming Ch3 SRP, DIP, ISP

Example

Page 14: Object Oriented Programming Ch3 SRP, DIP, ISP

Needless Complexity? When?

Page 15: Object Oriented Programming Ch3 SRP, DIP, ISP

Persistence

TDD

Page 16: Object Oriented Programming Ch3 SRP, DIP, ISP

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

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

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

Inversion of Control

low level change -> high level change

Page 20: Object Oriented Programming Ch3 SRP, DIP, ISP

Hollywood Principle: Don’t call me, I’ll call you

Inversion of ControlWhat they need

Page 21: Object Oriented Programming Ch3 SRP, DIP, ISP

Switch+ switch()

SwitchableDevicePolicy

Page 22: Object Oriented Programming Ch3 SRP, DIP, ISP
Page 23: Object Oriented Programming Ch3 SRP, DIP, ISP

– Robert C. Martin

“Depend on abstractions,

not on concretions.”

Page 24: Object Oriented Programming Ch3 SRP, DIP, ISP

- Design Pattern , GoF

“Program to an interface,

not an implementation.”

Page 25: Object Oriented Programming Ch3 SRP, DIP, ISP

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

Interface Pollution

Page 27: Object Oriented Programming Ch3 SRP, DIP, ISP

Interface Pollution

Page 28: Object Oriented Programming Ch3 SRP, DIP, ISP

Adaptor

Page 29: Object Oriented Programming Ch3 SRP, DIP, ISP

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

Glass door

Separate Clients == Separate Interfaces

Page 31: Object Oriented Programming Ch3 SRP, DIP, ISP
Page 32: Object Oriented Programming Ch3 SRP, DIP, ISP
Page 33: Object Oriented Programming Ch3 SRP, DIP, ISP

“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

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

Recap Liskov Violation

Page 36: Object Oriented Programming Ch3 SRP, DIP, ISP

Liskov Substitution Principle

Subtypes must be substitutable for their base types.

Page 37: Object Oriented Programming Ch3 SRP, DIP, ISP

Rectangle & Square Problem

Page 38: Object Oriented Programming Ch3 SRP, DIP, ISP
Page 39: Object Oriented Programming Ch3 SRP, DIP, ISP

Segment

Line

Ray

Page 40: Object Oriented Programming Ch3 SRP, DIP, ISP

Line: isOn(point: Intercept) == trueLineSegment: isOn(point: Intercept) == ?

Page 41: Object Oriented Programming Ch3 SRP, DIP, ISP
Page 42: Object Oriented Programming Ch3 SRP, DIP, ISP
Page 43: Object Oriented Programming Ch3 SRP, DIP, ISP

It’s the endof the beginning

Page 44: Object Oriented Programming Ch3 SRP, DIP, ISP

– Sai Li

“Live for interfaces, not implementations, except Lovers”

Page 45: Object Oriented Programming Ch3 SRP, DIP, ISP

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