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 May this year, a Bichon Frise named "Coff...
With the continuous development of the Internet of...
There is no doubt that the topic of mini programs...
[[127133]] As the mobile phone industry enters a ...
Course Contents: Chapter 0: Introduction 0.1: Pre...
【Lecturer Introduction】 Xia Qijiang, a well-known...
On March 2, the Mongolian Red Cross donated 20,00...
Recently, Microsoft introduced and provided a pre...
Recently, the water level of the river in Venice,...
On August 2, Xpeng Motors officially announced th...
The fastest way to increase followers on Douyin i...
After the content of Huaban.com was rectified A l...
There are two types of information platforms on t...
China's Yunnan and Guizhou regions were once ...
Suppose you participated in three games, and ther...