File tree 3 files changed +93
-3
lines changed
GOFSwift.xcworkspace/xcuserdata/sboldt.xcuserdatad
SOLID.playground/Pages/Interface Segregation.xcplaygroundpage
3 files changed +93
-3
lines changed Original file line number Diff line number Diff line change 67
67
68
68
✅ Open Closed Principle (OCP)
69
69
70
- ❌ Liskov Substitution Principle (LSP )
70
+ ✅ Interface Segregation Principle (ISP )
71
71
72
- ❌ Interface Segregation Principle (ISP )
72
+ ❌ Liskov Substitution Principle (LSP )
Original file line number Diff line number Diff line change 2
2
3
3
import Foundation
4
4
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
+ }
6
96
7
97
//: [Next](@next)
You can’t perform that action at this time.
0 commit comments