Hi 👋
- 📦 Archive of technical articles
- 🐙 making
- Wechat: RyukieW
My apps
– | Minesweeper Elic Endless Ladder | Dream of books |
---|---|---|
type | The game | financial |
AppStore | Elic | Umemi |
1. Scene 1
Let’s define one that looks like thisProtocol
protocol DescriptionProtocol {
var name: String { get}}Copy the code
Add extension methodprintDescription
extension DescriptionProtocol {
func printDescription(a) {
print("I'm a:\(name)")}}Copy the code
The new classApple
Follow the agreementDescriptionProtocol
class Apple: DescriptionProtocol {
var name: String {
return The word "apple"
}
func printDescription(a) {
print("\(name)Yummy!")}}Copy the code
The new classPeople
Follow the agreementDescriptionProtocol
class People: DescriptionProtocol {
var name: String {
return "Human"
}
func printDescription(a) {
print("\(name)How interesting!")}}Copy the code
Create instance objects separately
let aApple = Apple(a)let aPeople = People()
aApple.printDescription()
aPeople.printDescription()
Copy the code
1.1 thinking
🤔 think about what the output will be?// Call the method implemented in Class, not the extended implementation of protocol
aApple.printDescription() // The apples are delicious!
aPeople.printDescription() // Humans are interesting!
Copy the code
1.2 Call in another way
func showDescription(of: DescriptionProtocol) {
of.printDescription()
}
showDescription(of: aApple)
showDescription(of: aPeople)
Copy the code
🤔 think about what the output will be? showDescription(of: aApple)// I am: apple
showDescription(of: aPeople) // I am: human
Copy the code
1.3 Does it feel strange?
aApple.printDescription()
- This approach calls the instance methods defined by the class directly, rather than the methods implemented in the protocol, and is distributed dynamically
showDescription
- Statically dispatches the function implemented in Protocol
printDescription
Functions with the same name in the protocol and class are unrelated functions with different storage areas
2. Scene 2
What happens if we declare the function in the protocol?
protocol DescriptionProtocol {
var name: String { get }
func printDescription(a)
}
Copy the code
Results:
The apples are delicious! Humans are funny! The apples are delicious! Humans are funny!Copy the code
aApple.printDescription()
- It is the same as scenario 1, which is dynamically distributed
showDescription
- The function declared in the protocol is called and distributed dynamically
Third, summary
The inherited polymorphic representation essentially puts all method implementations in a table, and each call must be queried in the table first, which can be called dynamic dispatch. Hence the concept of overwriting: a method is overridden, meaning that when a subclass calls the method, its implementation is used instead of its superclass’s.
Protocols are different from classes. Methods defined by protocols, like classes, are distributed dynamically; But methods that do not appear in the definition but only in Extension are statically distributed, meaning that the method belongs to the protocol and not to the protocol’s adherents.
Static performance is better than dynamic performance. Like some of the functions that are called a lot we can do statically to make it more efficient. Although dynamic distribution has more steps, such as message forwarding mechanism of OC, it is full of flexibility and often used in business development.
This is only explored on the surface and has not been analyzed from the source point of view. I’ll do it from a source perspective when I have time
reference
static-vs-dynamic-dispatch-in-swift