You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+68-1Lines changed: 68 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ I believe that’s how coding is learned. Happy learning!
12
12
13
13
There are 28 design patterns divided into SOLID, Creational, Structural, Behavioral categories. You can take code from each of the swift files and run in Xcode playground to see the output.
14
14
15
-
**1)SOLID - Open Closed Principle**
15
+
**1)SOLID - Open Closed Principle**
16
16
17
17
Definition:
18
18
@@ -49,3 +49,70 @@ class TeamRegister : CustomStringConvertible{
49
49
}
50
50
}
51
51
```
52
+
53
+
TeamRegister class conforms to CustomStringConvertible. It has two variables defined, an array named teamMembers of type String and memberCount of type Integer.
54
+
55
+
We also define two methods. checkInGuest method takes the guest name as parameter of type String and appends the guest to teamMembers array and returns array count.
56
+
57
+
checkOutGuest takes index of type Integer as parameter and removes the guest from register.
print("Taking players \n \(teamRegister.description) \n to the Stadium")
64
+
}
65
+
66
+
func dropPlayersBackAtHotel(){
67
+
print("Dropping all the players back at Hotel")
68
+
}
69
+
}
70
+
```
71
+
72
+
TeamConveyance class has two responsibilites majorly. takePlayersToStadium takes paramter of type TeamRegister and drops all the players at the stadium. dropPlayersBackAtHotel gets back all the players to hotel after the match is over. It is not concerned about anything else.
73
+
74
+
Let us now write a function called main and see the code in action:
75
+
76
+
```
77
+
func main(){
78
+
let teamRegister = TeamRegister()
79
+
let player1 = teamRegister.checkInGuest("PlayerOne")
80
+
let player2 = teamRegister.checkInGuest("PlayerTwo")
81
+
82
+
print(teamRegister)
83
+
}
84
+
85
+
main()
86
+
```
87
+
88
+
We take an instance of TeamRegister class and check-in few guests passing their names as parameters.
89
+
90
+
Output in the Xcode console:
91
+
92
+
1 - PlayerOne
93
+
2 - PlayerTwo
94
+
95
+
Let us now check-out a guest and add one more guest to the team. Change the main function to :
96
+
97
+
```
98
+
func main(){
99
+
let teamRegister = TeamRegister()
100
+
let player1 = teamRegister.checkInGuest("PlayerOne")
101
+
let player2 = teamRegister.checkInGuest("PlayerTwo")
102
+
103
+
print(teamRegister)
104
+
105
+
teamRegister.checkOutGuest(1)
106
+
print("------------------------------------")
107
+
print(teamRegister)
108
+
109
+
let player3 = teamRegister.checkInGuest("PlayerThree")
0 commit comments