Skip to content

Commit 060d27d

Browse files
author
Sebastian Boldt
committed
added playground page for interface segregation
1 parent d01b9a6 commit 060d27d

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed
Binary file not shown.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@
6767

6868
✅ Open Closed Principle (OCP)
6969

70-
❌ Liskov Substitution Principle (LSP)
70+
✅ Interface Segregation Principle (ISP)
7171

72-
Interface Segregation Principle (ISP)
72+
Liskov Substitution Principle (LSP)

SOLID.playground/Pages/Interface Segregation.xcplaygroundpage/Contents.swift

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

33
import Foundation
44

5-
var str = "Hello, playground"
5+
// Fat protocol - Problem
6+
7+
protocol FatProtocol {
8+
func tap()
9+
func doubleTap()
10+
func longTap()
11+
}
12+
13+
struct ExtendedButton: FatProtocol {
14+
func tap() {
15+
16+
}
17+
18+
func doubleTap() {
19+
20+
}
21+
22+
func longTap() {
23+
24+
}
25+
}
26+
27+
struct SimpleButton: FatProtocol {
28+
func tap() {
29+
// The SimpleButton just needs to implement this function
30+
// All the other functions are irrelavant
31+
}
32+
33+
func doubleTap() {
34+
}
35+
36+
func longTap() {
37+
}
38+
}
39+
40+
41+
// Fat protocol - Solution
42+
43+
protocol Tappable {
44+
func tap()
45+
}
46+
47+
protocol DoubleTappable {
48+
func doubleTap()
49+
}
50+
51+
protocol LongTappable {
52+
func longTap()
53+
}
54+
55+
56+
struct AnotherSimpleButton: Tappable {
57+
func tap() {
58+
// Now we just need to implement the methods we really need.
59+
}
60+
}
61+
62+
// Fat class - Problem
63+
64+
struct Video {
65+
var title: String
66+
var description: String
67+
var data: Data
68+
var metaData: String
69+
var fileType: String
70+
}
71+
72+
// The log functions just needs title and description.
73+
func log(video: Video) {
74+
print("\(video.title), \(video.description)")
75+
}
76+
77+
// Fat class - Solution
78+
79+
protocol Loggable {
80+
var title: String { get }
81+
var description: String { get }
82+
}
83+
84+
struct BetterVideo: Loggable {
85+
var title: String
86+
var description: String
87+
var data: Data
88+
var metaData: String
89+
var fileType: String
90+
}
91+
92+
// The log functions just needs title and description.
93+
func betterLog(loggable: Loggable) {
94+
print("\(loggable.title), \(loggable.description)")
95+
}
696

797
//: [Next](@next)

0 commit comments

Comments
 (0)