|
| 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 | + |
0 commit comments