A network library encapsulation method based on Command design pattern

By adopting a network library encapsulation method based on the Command design pattern in the mobile banking project, the problems of cumbersome, inefficient and prone to errors in the existing technology are solved, and flexible customization and automated processing of business requests are realized, the readability and maintainability of the code are improved, and the stability of the engineering project is ensured.

CN119743529APending Publication Date: 2025-04-01BEIYIN FINANCIAL TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411969931.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-30
Publication Date
2025-04-01

Smart Images

  • Figure CN119743529A_ABST
    Figure CN119743529A_ABST
Patent Text Reader

Abstract

The invention discloses a network library encapsulation method based on the Command design pattern, including: an encapsulated basic network request library, which adopts a network request method to realize flexible customization of business requests; centrally configures network request common parameters, has a unified abnormal logic processing, supports request return JSON data verification, supports automatic conversion of request data into business model objects, and replaces the underlying request library implementation class without modifying the upper-level business request logic; the encapsulated basic network request library has a clear and extensible interface, and each request is encapsulated as an object by extracting the network request command interface, and the client is parameterized by the request; in response to changes in business request parameters and data models, independent changes in their respective latitudes do not affect the upper-level business. To realize flexible customization of business requests, the network request common parameters can be centrally configured, and the underlying request library implementation class can be replaced without modifying the upper-level business request logic.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of network encapsulation, and in particular to a network library encapsulation method based on a Command design pattern. Background Art

[0002] The network library encapsulated in the existing mobile banking project once supported the rapidly growing needs of end users. However, with the iteration of new businesses, the disadvantages of the original network library have become increasingly prominent, and the expansion of the new business network interface is cumbersome, inefficient, and error-prone. This is specifically manifested in the following three aspects. First, after the request data of the original network library is returned, the corresponding JSON data is not converted into a business model object, but is assigned to each field of the model object by manual traversal by the customer, which is inefficient. Once some fields in the background are not returned or are empty, and the caller does not add logical judgment, the App will crash abnormally. Secondly, the network request code is tightly coupled with the business model code. Once the business changes, the parsing of the network request data needs to be modified and adapted, and the code maintainability is poor. On the other hand, the session Session is tightly coupled with the basic network library, and the two were originally not related. In addition, the network interface provided to the H5 bridge and the native network interface processing logic of the client are inconsistent. There is no separation processing, and all are coupled together, which makes it complicated to troubleshoot production problems.

[0003] The redesigned basic network encapsulation library changes the original intensive network request calling method to a decentralized network request method, decouples the network request class and the business model class, facilitates module division, and makes the code structure clearer and more readable. By introducing the third-party library JSONModel, the corresponding business model object is directly generated after the network request response data, which is convenient for exception handling and acquisition of various data fields. Once the business data is adjusted, only the model object needs to be modified, and the basic network library and data parsing class do not need to be changed. The network interface provided to H5 is separated from the client's native call network interface to facilitate debugging and troubleshooting.

[0004] Existing technical solutions:

[0005] 2.1 Using OS native API

[0006] In mobile iOS development, simple network requests usually use the URLSession class provided by Apple, which is an API in the Foundation framework for managing and configuring network connections. Then, URL objects are created, URLRequest objects are created, request headers are set, network requests are sent (asynchronous requests and synchronous requests are supported), request results are processed, and data tasks are started.

[0007] 2.2 Using a third-party packaged network library

[0008] Because the system's native API is cumbersome to use, and the network request code and business logic code are tightly coupled, it is not convenient to design the layered architecture of the application. In order to avoid these problems, many third-party encapsulated network library frameworks have been generated. The following two are currently commonly used.

[0009] ASIHTTPRequest, each request initializes a (shared) instance by the constructor, configures parameters through this instance and initiates the request. ASIHTTPRequest initially used the delegate mode callback, and after iOSSDK supports Block, it also provides an instance method for registering Block. ASIHTTPRequest uses CFHTTP request completion, directly calls back the instance method of ASIHTTPRequest, and completes the callback of Delegate mode or Block mode through the information recorded by the stored instance object. In the processing of asynchronous requests, after the ASIHTTPRequest object is initialized, the object is added to the shared operation queue in the startAsynchronous method. After that, all operations, including the creation of CFHTTPMessageRef, which is the main object for processing network requests, are completed in the child thread to which the ASIHTTPRequest object belongs.

[0010] (2) AFNetworking, in the sample code of AFN, a static method is used to create a shared instance of AFHTTPClient using dispatch_once(), which is also the official recommended method. In the initialization method of creating AFHTTPClient, an OperationQueue is created and a series of parameter default values ​​are set. In the getPath:parameters:success:failure method, an NSURLRequest is created, and an NSOperation is created with the NSURLRequest object instance as a parameter, and added to the NSOperationQueue created in the initialization method.

[0011] The above operations are all completed in the main thread. In the start method of NSOperation, create an NSURLConnection with the previously created NSURLRequest object as a parameter and open the connection.

[0012] Disadvantages of using third-party network encapsulation libraries

[0013] (1) In terms of security and stability, financial system applications require strong stability. If problems arise, they can be upgraded and deployed in a timely manner to solve application problems as soon as possible. However, third-party network libraries rely on the maintenance of open source teams. If application developers modify them directly, it will cause compatibility issues between the version and the open source project.

[0014] (2) In terms of intellectual property, some open source projects need to follow certain agreements. To avoid unnecessary disputes, direct development is more controlled, highly customized, and more in line with project reality. Summary of the invention

[0015] In view of the above problems, the present invention is proposed to provide a network library encapsulation method based on the Command design pattern to overcome the above problems or at least partially solve the above problems.

[0016] According to one aspect of the present invention, a network library encapsulation method based on the Command design pattern is provided, and the network library encapsulation method comprises:

[0017] The encapsulated basic network request library uses network request mode to achieve flexible customization of business requests;

[0018] Centrally configure common parameters of network requests, have unified exception logic processing, support request return JSON data verification, support automatic conversion of request data into business model objects, and replace the underlying request library implementation class without modifying the upper-level business request logic;

[0019] The encapsulated basic network request library has a clear and extensible interface. By extracting the network request command interface, each request is encapsulated as an object and the client is parameterized with the request.

[0020] In response to changes in business request parameters and data models, independent changes can be made at their respective latitudes without affecting upper-layer business.

[0021] Optionally, the network request method specifically includes: Command design mode, supporting POST and GET.

[0022] Optionally, the encapsulated basic network request library specifically includes: a network service abstraction layer, a network layer base class and a network request management class.

[0023] Optionally, the network service abstraction layer specifically includes:

[0024] This layer class BOBBusinessBaseRequest inherits the BOBBaseRequest class and is the customer interaction interface layer;

[0025] Provides three interfaces: tracking request status information, including request completion initialization, request start, request success, request failure, and request timeout;

[0026] Start a network request and specify the object type parameters of the network return data model. The network library is responsible for parsing. If no parameters are specified, the customer needs to manually parse the network request data.

[0027] Parse the data returned by the network request, relying on the business model parameters specified by the "Start Network Request" interface, which is a class-private interface.

[0028] Optionally, the network layer base class specifically includes:

[0029] This layer class BOBBaseRequest inherits Apple's NSObject class;

[0030] Implement encapsulation of the underlying request specific class;

[0031] The provided interfaces include:

[0032] Specify the HTTP Header information of the network request;

[0033] Specify the success and failure callback functions of the network request;

[0034] Specify the timeout period for network requests;

[0035] Specify the general parameter information of the network request and support whether to append the configuration information;

[0036] Specify the network request type;

[0037] Support adding network request objects to the request queue;

[0038] Supports removing network objects that have not issued requests from the request queue.

[0039] Optionally, the network request management class specifically includes:

[0040] The network request management class BOBNetworkAgent inherits Apple's NSObject class, which is used to maintain a network request object queue, ensure thread safety, and flexibly add or remove request objects from the queue;

[0041] The asynchronous thread will schedule the tasks in the queue for execution;

[0042] When the network request is responded, the result is asynchronously marshaled back to the main thread;

[0043] The external interface includes: adding network request objects; removing network request objects.

[0044] The present invention provides a network library encapsulation method based on the Command design pattern, the network library encapsulation method includes: an encapsulated basic network request library, which adopts a network request method to realize flexible customization of business requests; centrally configures network request common parameters, has a unified abnormal logic processing, supports request return JSON data verification, supports automatic conversion of request data into business model objects, and replaces the underlying request library implementation class without modifying the upper-level business request logic; the encapsulated basic network request library has a clear and extensible interface, and each request is encapsulated as an object by extracting the network request command interface, and the client is parameterized with the request; in response to changes in business request parameters and data models, independent changes in their respective latitudes do not affect the upper-level business. To realize flexible customization of business requests, the network request common parameters can be centrally configured, there is a unified abnormal logic processing, supports request return JSON data verification, supports automatic conversion of request data into business model objects, and the underlying request library implementation class can be conveniently replaced without modifying the upper-level business request logic.

[0045] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented according to the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are listed below. BRIEF DESCRIPTION OF THE DRAWINGS

[0046] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative work.

[0047] Figure 1 A diagram of the encapsulated network library architecture provided by an embodiment of the present invention;

[0048] Figure 2 A timing diagram of a newly encapsulated network library call provided by an embodiment of the present invention;

[0049] Figure 3 A class diagram of the Command design pattern based on an embodiment of the present invention. DETAILED DESCRIPTION

[0050] The exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided to enable a more thorough understanding of the present disclosure and to fully convey the scope of the present disclosure to those skilled in the art.

[0051] The terms "comprises" and "having" and any variations thereof in the description embodiments, claims and drawings of the present invention are intended to cover non-exclusive inclusions, for example, including a series of steps or units.

[0052] The technical solution of the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments.

[0053] The encapsulated network library architecture is as follows Figure 1 shown.

[0054] The basic network request library encapsulated by this method adopts the Command design pattern, supports common network request methods such as POST and GET, realizes flexible customization of business requests, can centrally configure common parameters of network requests, has unified exception logic processing, supports request return JSON data verification, supports automatic conversion of request data into business model objects, and can easily replace the underlying request library implementation class without modifying the upper-level business request logic.

[0055] The basic network request library encapsulated by this method has a clear and extensible interface. By extracting the network request command interface, each request is encapsulated as an object, so that we can parameterize customers with different requests. This can flexibly respond to changes in business request parameters and data models, avoiding the strong dependence of the business layer on the underlying network library. The two can change independently in their own latitudes without affecting the upper-level business. In addition, it is also convenient to monitor network data and implement abnormal data reporting functions.

[0056] Customers customize network request objects according to actual business scenarios. First, call the network class interface of the business abstraction layer, then call the network layer base class, put the specific network request object into the singleton object BOBNetworkAgent queue for management, and the singleton object is responsible for scheduling the underlying network specific request implementation class (the current project uses TMFShark). After the asynchronous response request, the network data is called back to the customer. Figure 2 This is the calling sequence diagram.

[0057] The newly encapsulated network library adopts the command design pattern as a whole. For ease of understanding, we will briefly introduce this pattern. Definition of command design pattern: The command pattern encapsulates requests into objects so that other objects can be parameterized using different requests, queues, or logs. The command pattern also supports revocable operations. --Excerpt from "Design Patterns: The Foundation of Reusable Object-Oriented Software" The command pattern belongs to the behavioral class pattern, and the class diagram relationship is as follows Figure 3 Show.

[0058] In terms of the design of the new encapsulation network library method, the monitoring interfaces and descriptions of each layer are as follows:

[0059] 1. Network service abstraction layer

[0060] This layer class BOBBusinessBaseRequest inherits the BOBBaseRequest class, which is the customer interaction interface layer. It provides the following three interfaces:

[0061] Tracks request status information, such as request completion initialization, request start, request success, request failure, request timeout, etc.

[0062] When starting a network request, you can specify the object type parameter of the network return data model, which will be parsed by the network library. If this parameter is not specified, the customer needs to manually parse the network request data.

[0063] Parse the network request return data, relying on the business model parameters specified by the "Start Network Request" interface, which is a class private interface.

[0064] 2. Network layer base class

[0065] This layer class BOBBaseRequest inherits Apple's NSObject class. It implements the encapsulation of the underlying request specific class. It provides the following seven interfaces:

[0066] Specify the HTTP Header information of the network request;

[0067] Specify the success and failure callback functions of the network request;

[0068] Specify the timeout period for network requests;

[0069] Specify the general parameter information of the network request and support whether to append the configuration information;

[0070] Specify the network request type (support common request types such as POST and GET);

[0071] Support adding network request objects to the request queue;

[0072] Supports removing network objects that have not issued requests from the request queue (canceling requests).

[0073] 3. Network request management

[0074] The network request management class BOBNetworkAgent inherits Apple's NSObject class. This class is designed as a singleton mode. Its main responsibilities are to maintain a queue of network request objects, ensure thread safety, and flexibly add or remove request objects from the queue. The asynchronous thread will schedule the execution of tasks in the queue. When the network request is responded to, the result will be asynchronously marshaled back to the main thread. There are only two main external interfaces.

[0075] Add a network request object; remove a network request object.

[0076] Beneficial effects:

[0077] A self-developed network library is used to solve the problem of high coupling between business logic and network request modules. The basic network encapsulation library is redesigned, and the original intensive network request calling method is changed to a decentralized network request method. The network request class and the business model class are decoupled to facilitate module division, making the code structure clearer and more readable.

[0078] Business data modeling, by introducing the JSONModel library, directly generates the corresponding business model object after the network request response data, which is convenient for exception handling and acquisition of various data fields. Once the business data is adjusted, only the model object needs to be modified, and the basic network library and data parsing class do not need to be changed

[0079] Debugging and troubleshooting are convenient because network requests are centrally managed, making debugging and viewing log information more convenient.

[0080] Pure source code integration, no additional development learning costs are required. After specifying the business model in the actual application, just call an interface, which is simple and easy to use.

[0081] Portability: When the specific implementation of the underlying network library changes, only the specific implementation class needs to be replaced, which has no impact on the upper-end business interface, ensuring the stability of the project.

[0082] The convenience of querying business response log information is that due to the uniqueness of the network request entry, it is more convenient to check logs and locate problems.

[0083] The above specific implementation methods further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above are only specific implementation methods of the present invention and are not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.

Claims

1. A network library encapsulation method based on the Command design pattern, characterized in that: The network library encapsulation method comprises: The encapsulated basic network request library uses network request mode to achieve flexible customization of business requests; Centrally configure common parameters of network requests, have unified exception logic processing, support request return JSON data verification, support automatic conversion of request data into business model objects, and replace the underlying request library implementation class without modifying the upper-level business request logic; The encapsulated basic network request library has a clear and extensible interface. By extracting the network request command interface, each request is encapsulated as an object and the client is parameterized with the request. In response to changes in business request parameters and data models, independent changes can be made at their respective latitudes without affecting upper-layer business.

2. According to claim 1, a network library encapsulation method based on the Command design pattern is characterized in that: The network request method specifically includes: Command design mode, supporting POST and GET.

3. According to a method for encapsulating a network library based on the Command design pattern as claimed in claim 1, it is characterized in that: The encapsulated basic network request library specifically includes: a network service abstraction layer, a network layer base class and a network request management class.

4. According to the method for encapsulating a network library based on the Command design pattern as claimed in claim 3, it is characterized in that: The network service abstraction layer specifically includes: This layer class BOBBusinessBaseRequest inherits the BOBBaseRequest class and is the customer interaction interface layer; Provides three interfaces: tracking request status information, including request completion initialization, request start, request success, request failure, and request timeout; Start a network request and specify the object type parameters of the network return data model. The network library is responsible for parsing. If no parameters are specified, the customer needs to manually parse the network request data. Parse the data returned by the network request, relying on the business model parameters specified by the "Start Network Request" interface, which is a class-private interface.

5. According to the method for encapsulating a network library based on the Command design pattern in claim 3, it is characterized in that: The network layer base class specifically includes: This layer class BOBBaseRequest inherits Apple's NSObject class; Implement encapsulation of the underlying request specific class; The provided interfaces include: Specify the HTTP Header information of the network request; Specify the success and failure callback functions of the network request; Specify the timeout period for network requests; Specify the general parameter information of the network request and support whether to append the configuration information; Specify the network request type; Support adding network request objects to the request queue; Supports removing network objects that have not issued requests from the request queue.

6. According to the method for encapsulating a network library based on the Command design pattern in claim 3, it is characterized in that: The network request management class specifically includes: The network request management class BOBNetworkAgent inherits Apple's NSObject class, which is used to maintain a network request object queue, ensure thread safety, and flexibly add or remove request objects from the queue; The asynchronous thread will schedule the tasks in the queue for execution; When the network request is responded, the result is asynchronously marshaled back to the main thread; The external interface includes: adding network request objects; removing network request objects.