Skip to content

Commit a03fdd9

Browse files
Update README.md
1 parent 4a6492c commit a03fdd9

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

README.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ I believe that’s how coding is learned. Happy learning!
1212

1313
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.
1414

15-
**1)SOLID - Open Closed Principle**
15+
**1) SOLID - Open Closed Principle**
1616

1717
Definition:
1818

@@ -49,3 +49,70 @@ class TeamRegister : CustomStringConvertible{
4949
}
5050
}
5151
```
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.
58+
59+
```
60+
class TeamConveyance {
61+
62+
func takePlayersToStadium(_ teamRegister : TeamRegister){
63+
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")
110+
111+
print("------------------------------------")
112+
print(teamRegister)
113+
}
114+
115+
main()
116+
```
117+
118+

0 commit comments

Comments
 (0)