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
From being prosperous to dying down, Tencent Weibo...
1. Overview A specification is the official stand...
At present, young consumer groups are rising, and...
The list of "scientific" rumors for Jan...
Paris Buttfield-Addison is a co-founder, mobile a...
After Samsung, LG and others successively set up ...
Ants are a common insect, but how many ants are t...
The Year of the Tiger is here, and all kinds of p...
Usually when it comes to operational activities, ...
How much does it cost to be an agent for a paper ...
Socializing has a psychological and emotional imp...
Review expert: Zhu Guangsi, member of Beijing Sci...
In 1995, less than 1% of the world's populati...
Microsoft, Apple, Google, and Tesla have always b...