Distinguish simple understanding

  • Object oriented: a person’s name/age/run/eat
  • Protocol oriented: a person who can have many capabilities (protocols)

object-oriented

  • Traditional object-oriented thinking:
    • There’s a dog that has a name and can run
    • There’s a cat that has a name and can run
    • There is a man with his own name who can run
    • Abstract an animal class, and then put the running behavior into the animal class
    • If an act of eating is needed, it can also be abstracted in the animal class

  • Animal acts as a base class to construct the running and eating behavior methods
Class Animal: NSObject {func running() {print(" ")} func eating() {print(" ")}}Copy the code
  • Dog, as a subclass, calls methods of the superclass
    • Dog inherits from its parent class as a child
    • In a subclass, the subclass overrides the method of the superclass, and the superclass pointer points to the subclass. polymorphism
    • The same goes for Cat and Person
  • Three conditions of polymorphism
    • Inheritance: Various subclasses inherit from their parent
    • Overridden: subclass overrides the superclass methods eating(), running()
    • Point to: A pointer from a parent class points to a subclass
Class Dog: Animal{override func eating(){super.eating() print()} override func running(){super.running()} class Dog: Animal{override func eating(){super.eating() print()}Copy the code
  • The downside of object-oriented development
    • If you have a robot (a car), and you have running behavior, how can you abstract it, which is obviously not very abstract
    • Obviously car and robot can’t inherit from Animal because they’re not animals they can only extract a base class or write their own

class Car: NSObject {
    
    func running() {
        
    }
}

class Robot: NSObject {
    func running() {
        
    }
}
Copy the code
  • In real development scenarios
    • The UIViewController/UITableViewController/UICollectionViewController abstract the same way
    • So you may need to do BaseViewController BaseTableViewController/BaseCollectionViewController three controller base class (or very complicated)
    • If you want to copy some of the functionality to another project, some of it should be copied and some of it should not be copied, extracting can be tricky
  • The core of object-oriented development is: encapsulation – inheritance -(polymorphism)

Protocol-oriented Example

  • There are more than 50 complex protocols in the Swift standard library, and almost all of the actual types satisfy several protocols
  • The core of protocol-oriented development is: modularity (componentization)
  • Protocol oriented application development:
    • Particle animation effect is achieved, extracted in a separate protocol, if there is a controller need, to achieve my protocol
    • A lot of UIViews are described by xiBs, and often we need to load UIViews from a XIB, extract individual protocols, and we need to load classes from the XIB, and we just need to follow the protocol

A simple example

  • When the UIViewController/UITableViewController/UICollectionViewController requires a Test method
  • Write a Testable. Swift
  • Add an extension to a custom protocol and default the optional methods in the Extension so that objects that adhere to the protocol do not have to implement the optional methods.
  • Example code:
Import Foundation protocol Testable whereto {// whereto do the Testable Testable work // whereto do the Testable work // whereto do the Testable work} // To implement the protocol you have to do an extension extension equivalent to the EXTINCTION file of OC. Swift is convenient extension {func test() {print(" simple test of the code ")}}Copy the code

class OneViewController: UIViewController, Testable
class TwoViewController: UITableViewController, Testable
class ThreeViewController: UICollectionViewController, Testable
Copy the code
  • Each of the three classes follows the protocol and just writes the method in the right place

Back to eating and running

  • Subclasses follow the protocol implementation method
Import Foundation Protocol Runable {} extension Runable {func running() {print(" running ing")}} import Foundation Protocol Eatable {} extension Eatable {func eating(){print(" ing")}} class Cat: NSObject, Runable { } class Person: NSObject, Runable { }Copy the code

Protocol-oriented conditions Only the current class is A UIViewController to comply with the protocol

protocol Emitterable {
    
}
extension Emitterable where Self : UIViewController
Copy the code

Network requests (developed using a protocol-oriented approach)

protocol Requestable { var method : HttpMethod { get } var host : String { get } var path : String { get } var parameters : [String : Any] { get } associatedtype ResultType : Decodable } extension Requestable { func request(completion : @escaping (ResultType?) -> Void) {// 1. Create URL let URL = URL(string: host + path)! Let request = URLRequest(URL: URL) // 3. Request sent via URLSession let task = URLSession. Shared. DataTask (with: request, completionHandler: { (data, _, error) in completion(ResultType.parse(data!) }} Protocol Decodable {static func parse(_ data: data) -> Self? }Copy the code