Now, it is time to code the protocols in Swift. We will code the following five protocols:
ComicCharacter
GameCharacter
Alien
Wizard
Knight
The following UML diagram shows the five protocols that we will code in Swift, with their required properties and methods included in the diagram. In this case, the diagram shows only protocols and we don't use any mark above the protocol name. However, in other diagrams in which we will mix protocols with classes, we will add procotol after the protocol name. UML diagrams have specifications for interfaces, but we will use our own mechanism to identify protocols:
The following lines show the code for the ComicCharacter
protocol. The public
modifier followed by the protocol
keyword and the protocol name, ComicCharacter
, makes up the protocol declaration. As happens with class declarations, the protocol body is enclosed in curly brackets ({}
). The code file for the sample is included in the swift_3_oop_chapter_05_01
folder:
public protocol ComicCharacter { var nickName: String { get set } func drawSpeechBalloon(message: String) func drawSpeechBalloon(destination: ComicCharacter, message: String) func drawThoughtBalloon(message: String) }
Protocols declare a nickName
read/write String
stored property requirement, a drawSpeechBaloon
method requirement, overloaded twice, and a drawThoughtBalloon
method requirement. The protocol includes only the method declaration because the classes that implement the ComicCharacter
protocol will be responsible for providing the implementation of the two overloads of the drawSpeechBalloon
and drawThoughtBalloon
methods. Note that there is no method body.
The following lines show the code for the GameCharacter
protocol. The code file for the sample is included in the swift_3_oop_chapter_05_01
folder:
public protocol GameCharacter { var fullName: String { get set } var score: UInt { get set } var x: UInt { get set } var y: UInt { get set } func drawAt(x: UInt, y: UInt) func moveTo(x: UInt, y: UInt) func intersects(character: GameCharacter) -> Bool }
In this case, the protocol declaration includes four read/write stored property requirements: fullName
, score
, x
, and y
. In addition, the declaration includes three method requirements: drawAt
, moveTo
, and intersects
. Note that we don't include access modifiers in either the properties or the methods.
The following lines show the code for the Alien
protocol. The code file for the sample is included in the swift_3_oop_chapter_05_01
folder:
public protocol Alien { var numberOfEyes: Int { get set } func appear() func disappear() }
In this case, the protocol declaration includes a property requirement, numberOfEyes
, and two method requirements: appear
and disappear
. Note that we don't include the code for either the getter or the setter method of the numberOfEyes
property. As happens with methods, the classes that implement the Alien
protocol are responsible for providing the implementation of the getter and setter methods for the numberOfEyes
property. We will create classes that implement the Alien
protocol later in this chapter.
The following lines show the code for the Wizard
protocol. The code file for the sample is included in the swift_3_oop_chapter_05_01
folder:
public protocol Wizard { var spellPower: Int { get set } func disappear(alien: Alien) }
In this case, the protocol declaration includes a property requirement, spellPower
, and a method requirement, disappear
. As with the other method requirement declarations included in the previously declared protocols, we use the protocol name as a type of an argument within a method requirement declaration. In this case, the alien
argument for the disappear
method requirement declaration is Alien
. Thus, we will be able to call the method with any class that conforms to the Alien
protocol.
The following lines show the code for the Knight
protocol. The code file for the sample is included in the swift_3_oop_chapter_05_01
folder:
public protocol Knight { var swordPower: Int { get set } var swordWeight: Int { get set } func unsheathSword() func unsheathSword(target: Alien) }
In this case, the protocol declaration includes two property requirements, swordPower
and swordWeight
, and an unsheathSword
method requirement, overloaded twice.
3.147.242.204