Last active
August 3, 2017 09:14
-
-
Save qhhonx/d78991dfcec7a8fbbb94cad0f747507b to your computer and use it in GitHub Desktop.
Use Swift generic technique to implement two inherited path classes that behaves like dynamic synthesis in Objective-C.
This file contains 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
// Write some awesome Swift code, or import libraries like "Foundation", | |
// "Dispatch", or "Glibc" | |
// +------------------------+ +------------------------+ | |
// | | | | | |
// | AnimalViewController | - - - - - -> | AnimalViewModel | | |
// | | | | | |
// +------------------------+ +------------------------+ | |
// ^ ^ | |
// | | | |
// | | | |
// | | | |
// +------------------------+ +------------------------+ | |
// | | | | | |
// | DogViewController | - - - - - -> | DogViewModel | | |
// | | | | | |
// +------------------------+ +------------------------+ | |
// ^ ^ | |
// | | | |
// | | | |
// +------------------------+ +------------------------+ | |
// | | | | | |
// | MastiffViewController | - - - - - -> | MastiffViewModel | | |
// | | | | | |
// +------------------------+ +------------------------+ | |
class AnimalViewModel { | |
let name: String | |
init(name: String) { | |
self.name = name | |
} | |
} | |
class AnimalViewController<ViewModel: AnimalViewModel> { | |
let viewModel: ViewModel | |
init(viewModel: ViewModel) { | |
self.viewModel = viewModel | |
} | |
func showAnimal() { | |
print("this is a", viewModel.name) | |
} | |
} | |
let animalVC = AnimalViewController(viewModel: AnimalViewModel(name: "animal")) | |
animalVC.showAnimal() | |
class DogViewModel: AnimalViewModel { | |
let isWalkedToday: Bool | |
init(name: String, isWalkedToday: Bool = false) { | |
self.isWalkedToday = isWalkedToday | |
super.init(name: name) | |
} | |
} | |
class DogViewController<ViewModel: DogViewModel>: AnimalViewController<ViewModel> { | |
override func showAnimal() { | |
super.showAnimal() | |
print("and it", viewModel.isWalkedToday ? "has" : "hasn't", "walked today") | |
} | |
} | |
let dogVC = DogViewController(viewModel: DogViewModel(name: "dog")) | |
dogVC.showAnimal() | |
final class MastifViewModel: DogViewModel { | |
let owner: String? | |
init(name: String, owner: String?, isWalkedToday: Bool = false) { | |
self.owner = owner | |
super.init(name: name, isWalkedToday: isWalkedToday) | |
} | |
} | |
final class MastiffViewController: DogViewController<MastifViewModel> { | |
override func showAnimal() { | |
super.showAnimal() | |
print(viewModel.owner == nil ? "unfortunatlly nobody likes it" : "luckily it has a owner named \(viewModel.owner!)") | |
} | |
} | |
let mastiffVC = MastiffViewController(viewModel: MastifViewModel(name: "mastiff", owner: "John", isWalkedToday: true)) | |
mastiffVC.showAnimal() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment