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
In the past, the development of the entire color ...
In the information flow industry, there are too m...
Do you still remember that Sony was unanimously b...
The greatest value of this article is to establis...
Snapdragon 810 is Qualcomm's pain point. Alth...
It’s Double Eleven again this year, and the trans...
When smartphones appeared, few people thought tha...
Today I will share with you a practical guide for...
Behind every search action and every click is a l...
The article revolves around social media to attra...
IDC data center business is flourishing all over ...
A while ago, ChatGPT became popular all over the ...
Every marketing planner hopes that his or her act...
Simple funnel tools can only tell you at which st...
Do you hope that the event will become a hit? Tha...