Swift protocol-oriented programming
The so-called protocol-oriented programming is to use the protocol
declaration method, and then use to extension
provide the default implementation. As long as the class that needs to use the method follows this protocol
, you can directly use the extension
implementation.
protocol animal {
var food: String {get}
func eat()
}
extension animal {
func eat() {
print("food name is \(food)")
}
}
struct Cat: animal {
var food: String = "mouse"
}
struct Dog:animal {
var food: String = "cat"
}
let cat = Cat()
let dog = Dog()
cat.eat()
dog.eat()
log:
food name is mouse
food name is cat
Code reuse
- Inheritance : It will bring coupling.
- The cost of inheritance: This is not a novel topic. It has been controversial since the birth of object-oriented programming. We often have to endure more and more complicated and huge inheritance systems to obtain code reusability, and with inheritance levels With the increase, the complexity of the code will accelerate, and the subsequent bugs will become more and more difficult to find. At this time, we may need to rely on design patterns to find our way back. However, most design patterns can only help you straighten out your code structure, but at the same time further deepen the complexity of your code.
category
/extension
: Will pollute all classes- Protocol-oriented programming :
protocol
+extension
Minimize coupling
Benefits of protocol-oriented programming
The advantage of protocol-oriented programming is that by implementing a function through protocol + extension, you can define the necessary and sufficient conditions, no more, no less. This minimizes coupling . Users can freely combine these protocols like building blocks and write one class
or struct
to complete complex functions. In fact, Swift's standard library is almost everything is starting out as a protocol
.
Why is Swift a protocol-oriented programming language?
Because it Swift
is more recommended to use value type variables ( struct
) rather than reference type ( class
) variables, Swift
many common data types, strings, collection types, and structures and enumerations are value types rather than reference types. Value types are Variables will automatically perform a low-consumption value copy when assigning values. Compared with objects, it copy
is more efficient and does not have thread safety issues.
Why do we need struct
struct
And class
the main differences:
struct
Is a reference value, andclass
is the type of referencestruct
There is no inherited function,class
there is inherited function
The difference between the two basic levels of struct and class reflects the difference from the Objective-C language. The swift language has brought about a whole new and earth-shaking change.
Let me talk about the first difference. From the perspective swift
of updates and struct
continuous improvement, Apple recommends using struct
it instead class
, because the difference between struct
value reference and class
type reference ensures that the use of struct
coding can write more secure and reliable code. Why do you say this? The class
type reference points the variable to the same memory address when assigning a value. This will bring some unexpected problems over a long span. Imagine a simple example, viewControllerA
holding an NSMutableArray
array mutalbeArray
, which contains 100 user
pieces of information will be mutableArray
assigned at this time viewControllerB
. For this viewControllerB
, it only needs the first 10 user
pieces of information, so it mutableArray
deletes the redundant information. Such a brain-dead operation leads to viewControllerA
module display errors and potential logic errors. The use of struct
value references does not cause such problems.
The second difference is that struct
there is no inherited function. This is because swift
in essence it is a protocol-oriented Protocol Oriented
language. struct
There is no and no inherited function. In order to realize a certain function, struct
it is enough to obey and implement a certain protocol. From a higher level, they struct+protocol
are swift
the two cornerstones of a protocol-oriented language.
to sum up
Swift is a language that supports multiple programming paradigms. It supports object-oriented programming, protocol-oriented programming, and functional programming. In the project development process, the controller and view parts should adopt more object-oriented programming methods due to the use of system frameworks; while the custom type parts such as models or business logic should be given priority to protocol-oriented programming.
Reference article
Swift protocol-oriented programming (with code)
Talk about Swift protocol-oriented programming
Let's start with protocol-oriented programming in Swift
A Preliminary Study of Protocol-Oriented Programming in Swift
Author: Yu Yi Suoyan
link: https: //www.jianshu.com/p/d0a5b92a1dd5