@@ -808,7 +808,6 @@ code using existential types incurs pointer indirection and dynamic method dispa
808
808
that cannot be optimized away.
809
809
-->
810
810
811
-
812
811
## Delegation
813
812
814
813
* Delegation* is a design pattern that enables
@@ -893,15 +892,16 @@ is marked by its inheritance from `AnyObject`,
893
892
as discussed in < doc:Protocols#Class-Only-Protocols > .
894
893
895
894
` DiceGame.Delegate ` provides three methods for tracking the progress of a game.
896
- These three methods have been incorporated into the game logic within
897
- the ` play() ` method above, and are called when
895
+ These three methods are incorporated into the game logic
896
+ in the ` play(rounds:) ` method above.
897
+ The ` DiceGame ` class calls its delegate methods when
898
898
a new game starts, a new turn begins, or the game ends.
899
899
900
900
Because the ` delegate ` property is an * optional* ` DiceGame.Delegate ` ,
901
- the ` play() ` method uses optional chaining each time it calls a method on the delegate,
901
+ the ` play(rounds: ) ` method uses optional chaining each time it calls a method on the delegate,
902
902
as discussed in < doc:OptionalChaining > .
903
903
If the ` delegate ` property is nil,
904
- these delegate calls fail gracefully and without error .
904
+ these delegate calls are just skipped .
905
905
If the ` delegate ` property is non-nil,
906
906
the delegate methods are called,
907
907
and are passed the ` DiceGame ` instance as a parameter.
@@ -941,33 +941,14 @@ class DiceGameTracker: DiceGame.Delegate {
941
941
}
942
942
```
943
943
944
- <!-- XXX rewrite paragraph -->
945
- ` DiceGameTracker ` implements all three methods required by ` DiceGame.Delegate ` .
946
- It uses these methods to keep track of the number of turns a game has taken.
947
- It resets a ` numberOfTurns ` property to zero when the game starts,
948
- increments it each time a new turn begins,
949
- and prints out the total number of turns once the game has ended.
950
-
951
- <!-- XXX rewrite paragraph -->
952
- The implementation of ` gameDidStart(_:) ` shown above uses the ` game ` parameter
953
- to print some introductory information about the game that's about to be played.
954
- The ` game ` parameter has a type of ` DiceGame ` , not ` SnakesAndLadders ` ,
955
- and so ` gameDidStart(_:) ` can access and use only methods and properties that
956
- are implemented as part of the ` DiceGame ` protocol.
957
- However, the method is still able to use type casting to
958
- query the type of the underlying instance.
959
- In this example, it checks whether ` game ` is actually
960
- an instance of ` SnakesAndLadders ` behind the scenes,
961
- and prints an appropriate message if so.
962
-
963
- <!-- XXX rewrite paragraph -->
964
- The ` gameDidStart(_:) ` method also accesses the ` dice ` property of the passed ` game ` parameter.
965
- Because ` game ` is known to conform to the ` DiceGame ` protocol,
966
- it's guaranteed to have a ` dice ` property,
967
- and so the ` gameDidStart(_:) ` method is able to access and print the dice's ` sides ` property,
968
- regardless of what kind of game is being played.
969
-
970
- Here's how ` DiceGameTracker ` looks in action:
944
+ The ` DiceGameTracker ` class implements all three methods
945
+ that are required by the ` DiceGame.Delegate ` protocol.
946
+ It uses these methods to zero out both players' scores
947
+ at the start of a new game,
948
+ to update their scores at the end of each round,
949
+ and to announce a winner at the end of the game.
950
+
951
+ Here's how ` DiceGame ` and ` DiceGameTracker ` look in action:
971
952
972
953
``` swift
973
954
let tracker = DiceGameTracker ()
0 commit comments