Which design patterns can be used when docking multiple third-party API?

problem description

due to the needs of the project, to interface with N third-party API, and then convert the data returned by the request API into the data structure of your own project, the third API may be an encapsulated SDK, a URL, or a restful-style API.

which design patterns would be better for development in order to facilitate future expansion and maintenance?
now I think of the combination of abstract factories and templates,

use abstract factory pattern to extract common parts and implement
use template pattern to implement abstract method

Please discuss what else is better.

May.07,2021

according to your description, I think maybe it has nothing to do with the design pattern.
use hierarchical design instead.
for example, typical examples are: presentation layer, business layer, data layer.

    The
  1. presentation layer is responsible for accepting the request and then providing the assembled data return.
  2. The
  3. business layer deals with transformation assembly of data, business-related computing, and so on. It takes input from the display layer, processes it, passes it to the data layer, then gets it back from the data layer, and then returns to the display layer after processing.
  4. the data layer is the layer that encapsulates all kinds of API. According to different inputs, it can go to different data layer objects, such as SDK,restful, and then the data layer is responsible for interacting with all third-party API. The business layer and presentation layer do not need to know the existence of the third-party API. After the data layer interacts with the third-party API, it obtains the data, converts it into a unified data structure in the project, and then returns it to the business layer.

then design patterns are used in the data layer, for example, factory patterns can be used to create different third-party services.


  1. Policy mode
    first of all, according to your own project data structure and application scenario, define the interface for data acquisition (each scenario may have multiple interfaces for one interface), so as to separate the interface definition from the specific implementation.
  2. template method
    define the main process for different implementation methods (SDK, URL, Restful). For example, the main process of Restful includes:
    a. Input parameter verification
    b. Input parameters to restful parameters conversion (convert API input parameters to restful parameters)
    c. Restful API calls
    d. Restful returns result parsing (success, failure)
    e. Restful returns result conversion (converts the result to its own data structure)
  3. Integration (the interface definition comes from 1, the general implementation part comes from 2, and the difference part comes from 3)
    inherits the template class created by 2 according to the specific implementation, and implements the business interface created by 1, and completes the whole business by completing the template callback method.
  4. other
    if you have other needs, such as unified logging and unified performance monitoring, you can use proxy mode
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b30e2b-2bd44.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b30e2b-2bd44.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?