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
FastQuery fast operation data layer framework Fas...
This article conducts product experience and anal...
Let’s look at an example first. A stock trading s...
Every February and March is the time when bamboo ...
Many people say that we should stand on the wind,...
The TV industry, which is used to being calm, has...
The course lecturers are researchers and fund man...
National Highway 219 is the longest, most beautif...
It has been more than a year since I switched fro...
As physics developed into the 20th century, geome...
There is no doubt that price is Pinduoduo's a...
It is no exaggeration to say that I am a typical ...
Recently, DeepBlue S05, a new model under DeepBlu...
Suzhou's smart transportation construction ha...
Black Hat SEO Training Course Season 1: Lesson 1:...