Skip to content

Commit bd0ee4c

Browse files
Behavioral updated
1 parent a7a5aa8 commit bd0ee4c

4 files changed

+269
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import UIKit
2+
import Foundation
3+
4+
protocol Interpreter{
5+
func hasNext() -> Bool
6+
func next() -> String
7+
}
8+
9+
protocol Container{
10+
func getInterpreter() -> Interpreter
11+
}
12+
13+
class NameRepo : Container{
14+
let names = ["India" ,"Australia", "England", "NewZealand"]
15+
func getInterpreter() -> Interpreter {
16+
return NameInterpreter(names)
17+
}
18+
}
19+
20+
private class NameInterpreter : Interpreter{
21+
var index = -1
22+
var names = [String]()
23+
24+
init(_ names : [String]){
25+
self.names = names
26+
}
27+
28+
func hasNext() -> Bool {
29+
if index < names.count {
30+
return true
31+
}
32+
return false
33+
}
34+
35+
func next() -> String {
36+
if self.hasNext(){
37+
index = index + 1
38+
return names[index]
39+
} else{
40+
return ""
41+
}
42+
}
43+
}
44+
45+
func main(){
46+
let nr = NameRepo()
47+
let interpreter = NameInterpreter(nr.names)
48+
49+
for _ in nr.names{
50+
interpreter.hasNext()
51+
print(interpreter.next())
52+
}
53+
}
54+
55+
main()
56+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import UIKit
2+
import Foundation
3+
4+
protocol Command{
5+
func displayStatus()
6+
}
7+
8+
protocol RemoteUmpire{
9+
func registerTVDisplay(tvDisplay :TVDisplay)
10+
func registerTVOperator(tvOperator : TVOperator)
11+
func isDecisionMade() -> Bool
12+
func setDecisionStatus(status : Bool)
13+
}
14+
15+
class TVOperator : Command{
16+
var tvUmpire:TVUmpire
17+
18+
init(_ tvUmpire : TVUmpire){
19+
self.tvUmpire = tvUmpire
20+
}
21+
22+
func displayStatus() {
23+
if tvUmpire.isDecisionMade(){
24+
print("Decision Made and Batsman in OUT")
25+
tvUmpire.setDecisionStatus(status: true)
26+
} else{
27+
print("Decision Pending")
28+
}
29+
}
30+
31+
func getReady(){
32+
print("Ready to Display Decision")
33+
}
34+
}
35+
36+
class TVDisplay : Command{
37+
var tvUmpire:TVUmpire
38+
39+
init(_ tvUmpire : TVUmpire) {
40+
self.tvUmpire = tvUmpire
41+
tvUmpire.setDecisionStatus(status: true)
42+
}
43+
func displayStatus() {
44+
print("Decision made and permission granted to display the decision on TV Display")
45+
tvUmpire.setDecisionStatus(status: true)
46+
}
47+
}
48+
49+
50+
51+
class TVUmpire : RemoteUmpire{
52+
private var tvOperator : TVOperator?
53+
private var tvDisplay : TVDisplay?
54+
private var decisionMade : Bool?
55+
56+
func registerTVDisplay(tvDisplay: TVDisplay) {
57+
self.tvDisplay = tvDisplay
58+
}
59+
60+
func registerTVOperator(tvOperator: TVOperator) {
61+
self.tvOperator = tvOperator
62+
}
63+
64+
func isDecisionMade() -> Bool {
65+
return decisionMade!
66+
}
67+
68+
func setDecisionStatus(status: Bool) {
69+
self.decisionMade = status
70+
}
71+
}
72+
73+
func main(){
74+
let tvUmpire = TVUmpire()
75+
let tvDisplayAtGround = TVDisplay(tvUmpire)
76+
let tvOperatorAtGround = TVOperator(tvUmpire)
77+
tvUmpire.registerTVDisplay(tvDisplay: tvDisplayAtGround)
78+
tvUmpire.registerTVOperator(tvOperator: tvOperatorAtGround)
79+
tvOperatorAtGround.getReady()
80+
tvDisplayAtGround.displayStatus()
81+
tvOperatorAtGround.displayStatus()
82+
83+
84+
}
85+
86+
main()
87+
88+
89+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//Assume you are adding the stats (number of runs scored) of a cricketer year by year in your program and at some point of time you want to trace back to an year in the past and check his stats till that point of time
2+
3+
import UIKit
4+
5+
class Memento {
6+
let numberOfRunsScored : Int
7+
8+
init(_ numberOfRunsScored : Int){
9+
self.numberOfRunsScored = numberOfRunsScored
10+
}
11+
}
12+
13+
//StatsHolder is an imaginary hardware which displays the stats
14+
15+
class StatsHolder : CustomStringConvertible{
16+
17+
private var numberOfRunsScored : Int
18+
19+
init(_ numberOfRunsScored : Int) {
20+
self.numberOfRunsScored = numberOfRunsScored
21+
}
22+
23+
func addStatsToHolder (_ runsToBeAdded : Int) -> Memento{
24+
numberOfRunsScored += runsToBeAdded
25+
return Memento(numberOfRunsScored)
26+
}
27+
28+
func restoreToPastStat(_ memento : Memento){
29+
numberOfRunsScored = memento.numberOfRunsScored
30+
}
31+
32+
var description: String{
33+
return "Total Runs scored = \(numberOfRunsScored)"
34+
}
35+
}
36+
37+
func main(){
38+
let statsHolder = StatsHolder(1200) //1200 is the first stat (number of runs) we add to stats holder
39+
let stat1 = statsHolder.addStatsToHolder(1400)
40+
let stat2 = statsHolder.addStatsToHolder(700)
41+
42+
print(statsHolder)
43+
44+
//restoreToStat1
45+
statsHolder.restoreToPastStat(stat1)
46+
print(statsHolder)
47+
48+
//restoreToStat2
49+
statsHolder.restoreToPastStat(stat2)
50+
print(statsHolder)
51+
52+
//There is no memento/snapshot when the StatsHolder is initialised
53+
}
54+
55+
56+
main()
57+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import Foundation
2+
3+
protocol Log
4+
{
5+
func bowlerStatsFromCurrentMatch(_ stats: String)
6+
func batsmenStatsFromCurrentMatch(_ stats: String)
7+
}
8+
9+
class StatsDisplayLog : Log
10+
{
11+
func bowlerStatsFromCurrentMatch(_ stats: String) {
12+
print(stats)
13+
}
14+
15+
func batsmenStatsFromCurrentMatch(_ stats: String) {
16+
print(stats)
17+
}
18+
}
19+
20+
class NoDisplayStatsLog : Log
21+
{
22+
func bowlerStatsFromCurrentMatch(_ stats: String) {}
23+
func batsmenStatsFromCurrentMatch(_ stats: String) {}
24+
}
25+
26+
27+
class UserInterface
28+
{
29+
var log: Log
30+
var runsScored = 0
31+
var wicketsTaken = 0
32+
33+
init(_ log: Log)
34+
{
35+
self.log = log
36+
}
37+
38+
func wicketTaken (){
39+
wicketsTaken += 1
40+
log.bowlerStatsFromCurrentMatch("Total Wickets : \(wicketsTaken)")
41+
}
42+
43+
func runsScored(numberOFRunsScored : Int){
44+
runsScored += numberOFRunsScored
45+
log.batsmenStatsFromCurrentMatch("Total Runs : \(runsScored)")
46+
}
47+
48+
}
49+
50+
51+
func main()
52+
{
53+
let ipadLog = StatsDisplayLog()
54+
let iPAdUserInterface = UserInterface(ipadLog)
55+
iPAdUserInterface.runsScored(numberOFRunsScored: 4)
56+
iPAdUserInterface.runsScored(numberOFRunsScored: 3)
57+
iPAdUserInterface.wicketTaken()
58+
59+
let iPhoneLog = NoDisplayStatsLog()
60+
let iPhoneUserInterface = UserInterface(iPhoneLog)
61+
iPhoneUserInterface.runsScored(numberOFRunsScored: 6)
62+
iPhoneUserInterface.runsScored(numberOFRunsScored: 2)
63+
64+
}
65+
66+
main()
67+

0 commit comments

Comments
 (0)