Created
December 8, 2016 07:59
-
-
Save helloworldsmart/766afa8c99588a99f1c84acab429ddbc to your computer and use it in GitHub Desktop.
strategy pattern ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: __Problem 4__ | |
| //: | |
| //: __4a.__ | |
| //: Write an instance method, bark(), that returns a different string based on the value of the stored property, size. | |
| enum Size: Int { | |
| case small | |
| case medium | |
| case large | |
| } | |
| class ChattyDog { | |
| let name: String | |
| let breed: String | |
| let size: Size | |
| init(name: String, breed: String, size: Size) { | |
| self.name = name | |
| self.breed = breed | |
| self.size = size | |
| } | |
| //Solution: 4a | |
| func bark(_ size: Size) -> String { | |
| switch size { | |
| case .small: | |
| return "yip yip" | |
| case .medium: | |
| return "arf arf" | |
| case .large: | |
| return "woof woof" | |
| } | |
| } | |
| //Solution: 4c | |
| static func speak(_ size: Size) -> String { | |
| switch size { | |
| case .small: | |
| return "yip yip" | |
| case .medium: | |
| return "arf arf" | |
| case .large: | |
| return "woof woof" | |
| } | |
| } | |
| } | |
| //: __4b.__ | |
| //: Create an instance of ChattyDog and use it to call the method, bark(). | |
| //: __4c.__ | |
| //: Rewrite the method, bark(), as a type method and rename it speak(). Call your type method to test it out. | |
| //Solution: 4b & 4c | |
| var barkingDog = ChattyDog(name:"Kupar", breed:"Schnauser", size: .medium) | |
| barkingDog.bark(barkingDog.size) | |
| ChattyDog.speak(.medium) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment