Open source project: Pitaya, an HTTP request library suitable for uploading large files: https://github.com/johnlui/Pitaya In this article, we will work together to reduce the coupling of the previous code and use the adapter pattern to implement a network API independent of the underlying structure to create a real network request "library". Reduce coupling How to reduce coupling The current clear soup noodle style code is easy to understand, but the function is single and the code is messy. Let's analyze the use process of NSURLSession: Constructing NSURLRequest Determine the URL Determine the HTTP method (GET, POST, etc.) Adding specific HTTP headers Filling HTTP Body Drive the session.dataTaskWithRequest method to start the request Specific implementation Create another NetworkManager class under Network, set URL, params, files, etc. as member variables, and initialize them in the constructor:
Afterwards, the above analysis 1. Determine the URL 2. Determine the HTTP method (GET, POST, etc.) 3. Add specific HTTP headers 4. Fill in the HTTP Body Encapsulate the first three steps into one function, encapsulate the last step into one function, and then encapsulate the code that drives session.dataTaskWithRequest into one function:
Then use a unified method to drive the above three functions to complete the request:
At the same time, don't forget the three parse params functions stolen from Alamofire, and put them in this class. So far, the work of reducing coupling is basically completed, and then we start to encapsulate the "network API". Encapsulating "network API" using the adapter pattern Understanding the Adapter Pattern The adapter pattern is a design pattern that is easy to understand: my app needs a function to get the string returned by a URL. I currently choose Alamofire, but the developing Pitaya looks good. I want to replace it with Pitaya in the future, so I encapsulate a layer of my own network interface to shield the underlying details. When the time comes, I only need to modify this class, and there is no need to go deeper into the project to change so many interface calls. The adapter pattern sounds high-sounding, but it is actually a design pattern that we use very commonly in daily coding. Do it! Modify the code of the Network class to:
Done! Encapsulating multi-level interfaces Interface without params:
Two get interfaces (with and without params):
Two post interfaces (with and without params):
Test interface Modify the calling code in ViewController to test the multi-level API:
Run the project and click the button to see the effect: Multi-level API encapsulation is successful! |
<<: Write an iOS network request library by yourself - encapsulation interface
>>: Software Engineer Entrepreneurship Trap - Taking Private Jobs
Recently, China Petroleum put into operation its ...
In the previous mini program Q&A, we introduc...
On October 26, 2019, the second NCTS China Cloud ...
A data center, also known as a computer room, is ...
Operators will come into contact with paid promot...
The configuration of the server is closely relate...
I heard that a "novice" can become a &q...
In real life, many friends must have started yawn...
In the era of comprehensive TV intelligence, the ...
Based on the feedback from previous students on Q...
Glaciers, snow-capped peaks, streams, and river v...
"Average Pages Visited" indicates the a...
How to find ancient ships Direction of travel? Wi...
Double 11 is a shopping festival that no business...