Object Oriented Programming Ch3 SRP, DIP, ISP

Post on 16-Apr-2017

58 views 0 download

Transcript of 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

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

Cohesion & Coupling

Cohesion• Degree to which the elements of a module belong

together • The force that cause a module or a class to change

Coupling

• Degree of interdependence between software modules

Cohesion & Coupling

Cohesion & Coupling

Goal: high cohesion, low coupling

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

Single Responsibility Principle

“A class should have one,

and only one, reason to change.”

SRP

SRP

Responsibility

• A reason for change • More than one responsibility

• coupled responsibilities • Fragile Design

Example

Needless Complexity? When?

Persistence

TDD

Single Responsibility Principle

• One of the simplest and hardest principle • Software Design: The art of separating responsibility

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

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.

Inversion of Control

low level change -> high level change

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

Inversion of ControlWhat they need

Switch+ switch()

SwitchableDevicePolicy

– Robert C. Martin

“Depend on abstractions,

not on concretions.”

- Design Pattern , GoF

“Program to an interface,

not an implementation.”

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

Interface Pollution

Interface Pollution

Adaptor

Interface Segregation Principle

“Client should NOT be forced to depend upon interfaces that they do not use”

Glass door

Separate Clients == Separate Interfaces

“Fat” Interface

• More than one responsibility • Coupled responsibilities • Fragile Design

• Clients depend on what they need only

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

Recap Liskov Violation

Liskov Substitution Principle

Subtypes must be substitutable for their base types.

Rectangle & Square Problem

Segment

Line

Ray

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

It’s the endof the beginning

– Sai Li

“Live for interfaces, not implementations, except Lovers”

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