Skip to content

Commit 6ff3f75

Browse files
author
Sebastian Boldt
committed
Added SRP example code
1 parent 91ece77 commit 6ff3f75

File tree

6 files changed

+75
-9
lines changed

6 files changed

+75
-9
lines changed
Binary file not shown.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363

6464
✅ Dependency Inversion Principle (DIP)
6565

66-
Single Responsibilty Principle (SRP)
66+
Single Responsibilty Principle (SRP)
6767

6868
❌ Open Closed Principle (OCP)
6969

SOLID.playground/Pages/Dependency Inversion.xcplaygroundpage/Contents.swift

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import Foundation
44

5+
/*
6+
A. HIGH LEVEL MODULES SHOULD NOT DEPEND UPON LOW LEVEL MODULES. BOTH SHOULD DEPEND UPON ABSTRACTIONS.
7+
B. ABSTRACTIONS SHOULD NOT DEPEND UPON DETAILS. DETAILS SHOULD DEPEND UPON ABSTRACTIONS.
8+
*/
9+
510
// The Problem - Tightly coupled classes
611

712
struct Wrong {

SOLID.playground/Pages/Open Closes Principle.xcplaygroundpage/Contents.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
import Foundation
44

5-
var str = "Hello, playground"
5+
// SOFTWARE ENTITIES (CLASSES, MODULES, FUNCTIONS, ETC.) SHOULD BE OPEN FOR EXTENSION, BUT CLOSED FOR MODIFICATION.
66

77
//: [Next](@next)

SOLID.playground/Pages/Single Responsibility.xcplaygroundpage/Contents.swift

+68-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,71 @@
22

33
import UIKit
44

5-
var str = "Hello, playground"
5+
// THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A CLASS TO CHANGE.
6+
7+
struct Wrong {
8+
class APIHandler {
9+
func handle() {
10+
let data = requestDataFromAPI()
11+
let array = parse(data: data)
12+
saveToDB(array: array)
13+
}
14+
15+
private func requestDataFromAPI() -> Data {
16+
// send API request and wait the response
17+
return Data()
18+
}
19+
20+
private func parse(data: Data) -> [String] {
21+
// parse the data and create the array
22+
return []
23+
}
24+
25+
private func saveToDB(array: [String]) {
26+
// save the array in a database (CoreData/Realm/...)
27+
}
28+
}
29+
}
30+
31+
//TODO: We should use the Dependency Inversion here too ...
32+
33+
struct Right {
34+
class APIHandler {
35+
let requestHandler: RequestHandler
36+
let parseHandler: ParseHandler
37+
let dbHandler: DataBaseHandler
38+
39+
init(requestHandler: RequestHandler, parseHandler: ParseHandler, dbHandler: DataBaseHandler) {
40+
self.requestHandler = requestHandler
41+
self.parseHandler = parseHandler
42+
self.dbHandler = dbHandler
43+
}
44+
45+
func handle() {
46+
let data = requestHandler.requestDataFromAPI()
47+
let array = parseHandler.parse(data: data)
48+
dbHandler.saveToDB(array: array)
49+
}
50+
}
51+
52+
class RequestHandler {
53+
func requestDataFromAPI() -> Data {
54+
// send API request and wait the response
55+
return Data()
56+
}
57+
}
58+
59+
class ParseHandler {
60+
func parse(data: Data) -> [String] {
61+
// parse the data and create the array
62+
return []
63+
}
64+
}
65+
66+
class DataBaseHandler {
67+
func saveToDB(array: [String]) {
68+
// save the array in a database (CoreData/Realm/...)
69+
}
70+
}
71+
}
72+

SOLID.playground/Pages/Single Responsibility.xcplaygroundpage/timeline.xctimeline

-6
This file was deleted.

0 commit comments

Comments
 (0)