swift 使用Swift通用技术实现两个继承的路径类,其行为类似于Objective-C中的动态合成。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 使用Swift通用技术实现两个继承的路径类,其行为类似于Objective-C中的动态合成。相关的知识,希望对你有一定的参考价值。

//  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()

以上是关于swift 使用Swift通用技术实现两个继承的路径类,其行为类似于Objective-C中的动态合成。的主要内容,如果未能解决你的问题,请参考以下文章

Swift如何实现通用类型的弱引用数组(下)

Swift如何实现通用类型的弱引用数组(下)

单例模式的Swift继承?

Swift 5:在使用协议实现 Equatable 的结构上实现通用数组操作

UIView 子类的 Swift 通用方法

Swift - 具有实现通用协议的值的 Typealias 字典