OC cannot do protocol-oriented development, while Swift can, because Swift can do the specific implementation of protocol methods, while OC cannot

Object-oriented development

The traditional object-oriented development way of thinking is to extract the similar methods implemented in the class, and then put them into a Base class, and then inherit from the Base class, each class can find the same method, no longer need to manually implement. For example, if you have a Person class and a Dog class, and they both have the eat method, you can create a new Animal class, extract the eat method and put it in it, and then inherit both Person and Dog from Animal. However, if we have a Robot class, we need to have eat method, and it is not reasonable to inherit it from Animal, so we need to change our thinking, oriented to protocol development ~

Protocol Oriented development

The core of protocol-oriented development is: ** modularization (componentization) ** Let’s review the general use of protocol first, create a Swift file LXFProtocol. Swift

import Foundation

protocol LXFProtocol {
    func eat()
}
Copy the code

Our Person class complies with the protocol LXFProtocol and requires us to implement methods in the protocol, such as:

class Person: NSObject, LXFProtocol {
    func eat() {//}}Copy the code

If we do this for each class, it’s no different than just copying and pasting code.

Swift can achieve the specific implementation of the protocol method

So now, let’s create a new Swift file, Eatable. Swift, to distinguish between LXFProtocol. Swift Eatable.

Import Foundation Protocol Eatable {// declarable variables} extension Eatable {funceat() {// implement specific function}}Copy the code

There are two caveats

  • You can declare variables in protocol for easy use in protocol methods
  • The implementation of the protocol method needs to be implemented in Extension

Make the Dog class Eatable compliant

class Dog: NSObject, Eatable {

}
Copy the code

This makes it easy to call dog’s eat method elsewhere, as does the Person and Robot classes

At this point, we can customize the different functionality of our classes in a protocol-oriented manner, that is, modularity. You can see that protocol oriented programming in Swift is similar to multiple inheritance in c++

The constraint

The current Eatable protocol can be complied with at will. If we have a requirement that the protocol we create is only complied with by UIViewController, what should we do? Of course, the Eatable protocol can only be observed by UIViewController. This is just an example, so don’t worry about it.

Add the constraint keyword [WHERE] after extension, and note that the protocol can only be observed by the class UIViewController (including subclasses), and we can also get the view of the controller that complies with the protocol

//import Foundation
import UIKit

protocol Eatable {
    
}

extension Eatable where Self : UIViewController {
    func eat() {
        view.backgroundColor = UIColor.red
    }
}

Copy the code

Ios-swift Protocol oriented programming (2)