首页 > 其他分享 >[Design Pattern] Encapsulate a network request lib - 1. DIP: Dependence Inversion Principle

[Design Pattern] Encapsulate a network request lib - 1. DIP: Dependence Inversion Principle

时间:2024-12-01 17:24:09浏览次数:7  
标签:layer axios network lib level implementation request Inversion requestor

Three layers design

  • Low level implementation Layer: using low level implementation to complete basic operation.
    • For the network request, we can use the lib such as axios, which internally using xhr, or we can also use fetchdirectly from node.js
  • request-core: Provide high-level network control features, including request serialization, request parallelism, and request deduplication.
  • request-bus: Bind to the busniess logic

Current design is 

  • request-bus depends on request-core
  • request-core depends on low level implementation layer
  • low level implementation layer might use different built-in method or 3-rd party library

 

Impove the design

For low level implementation layer, as we can see it, the solution can be different

  • xhr
  • fetch
  • axios

Which mean that this layer is not stable. Not stable means that once we might swap the current built-in methods or 3-rd party library for a better one in future. For example, if current you are using xhrin the old project, you might want to upgrade it by using fetchor axios; or you found axiosis not good enough, you want to update to other libraray again in the further future again.

The problem is the low level implementation layer is a base layer. If this layer is not stable, any change happens in this layer might affect upper layer, such as request-corelayer. Which might further affect request-buslayer.

Therefore, we need to find a solution can isolate this unstablness. 

For that we can use a design pattern called DIP (Dpendence Inversion Principle), our aims is to decouple request-coreand low level implementation layer.

 

Dependence Inversion Principle

  • High level implementation should not rely on low level implementation details
  • Both should rely on interface

 

Improved desgin

Now request-coreonly provide interface, doesn't provide implementation.

// exmaple
export interface Requestor {
    get(url: string, options: RequestOptions): Promise<Response>
    //
}

let req: Requestor
export function inject(request: Requestor) {
  req = requestor;
}

export function createParalleRequestor(maxCount = 4) {
    const req = useRequestor()
    // ...futher setting
    return req
}

export function createSequenceRequestor() {
    const req = useRequestor()
    // ...futher setting
    return req
}

 

request-axios-implexample code

import { Requestor } from 'request-core'
import axios from 'axios'

const ins = axios.create();

export requestor: Requestor {
    get(url, options) {
        // use axios
    },
    // other methods
}

 

request-busexample code

import {inject} from 'request-core'
import {requestor} from 'request-axios-impl'
inject(requestor)

In future, if there is any changes in low level implementation layer, we don't need to change request-core, we only need to add a new implementation, for example request-fetch-impl.ts

import {Requestor} form 'request-core'

export requestor: Requestor = {
    get(url, options?) {
        // using fetch
    },
    post(url, data, options?) {
    },
    // other methods
}

Only change in request-bus layer

- import {requestor} from 'request-axios-impl';
+ import {requestor} from 'request-fetch-impl';
inject(requestor)

 

标签:layer,axios,network,lib,level,implementation,request,Inversion,requestor
From: https://www.cnblogs.com/Answer1215/p/18579291

相关文章

  • 基于TCN-Transformer-KAN混合模型实现电力负荷时序预测——Kolmogorov-Arnold Network
    前言系列专栏:【深度学习:算法项目实战】✨︎涉及医疗健康、财经金融、商业零售、食品饮料、运动健身、交通运输、环境科学、社交媒体以及文本和图像处理等诸多领域,讨论了各种复杂的深度神经网络思想,如卷积神经网络、循环神经网络、生成对抗网络、门控循环单元、长短期记忆......
  • RabblitMQ 消息队列组件与 libev事件驱动库
    概述RabbitMQ是一个广泛使用的开源消息队列系统,它基于AMQP(高级消息队列协议)。RabbitMQ用于在分布式系统中传递消息,确保消息可靠传递并提供弹性。libev是一个事件驱动的库,用于高效地处理异步事件,常用于网络编程或需要高并发处理的应用。将RabbitMQ与libev结合使用,可以......
  • 【分块】LibreOJ 6282 数列分块入门6
    题目https://loj.ac/p/6282题解数据范围\(1\leqn\leq10^5\),因此进行分块最多分\(\sqrt{10^5}≈318\)块。且数据是随机生成的,因此插入数据后,每个块的长度期望值为\(\frac{318+(318+100000/318)}{2}≈475\)。因此,可以使用分块思想解决该问题。对于每个块,都用一个......
  • Cross-stitch Networks for Multi-task Learning译文_
                                目录1.简介1.1多任务共享:一项实证研究2.相关工作3十字绣网络3.1.分割架构 3.2.统一拆分架构 3.3.十字绣单元4.十字拼接的设计决策5.消融分析5.1.初始化十字绣单元的参数 ......
  • [C++][MSVC][Error] 检测到 RuntimeLibrary 的不匹配项: 值 MT_StaticRelease 不匹配
    1简介本文将介绍在C++编程中使用MSVC编译器时可能遇到的错误:检测到RuntimeLibrary的不匹配项:值MT_StaticRelease不匹配值MD_DynamicRelease。该错误通常是由于编译器和链接器之间的设置不一致引起的。2VisualStudio环境在MSVC工程上右键->属性,找到配置属性->C/C......
  • python语言实现_通过端口转发实现跨网络(多网络之间)通信_science_network
    本文使用python语言实现了一个端口转发的程序,该程序可以实现多网络之间的信息通信,当然这里有个前提,那就是多个网络都在一台主机上有可以连通的端口。之所以有这个编写代码的需求,是因为最近使用的sciencenetwork工具不大好用了,于是就要博士同学发给我一个好用些的来,固然发现同学......
  • ImportError: /nvidia/cusparse/lib/libcusparse.so.12: undefined symbol: __nvJitLi
      大家好,我是爱编程的喵喵。双985硕士毕业,现担任全栈工程师一职,热衷于将数据思维应用到工作与生活中。从事机器学习以及相关的前后端开发工作。曾在阿里云、科大讯飞、CCF等比赛获得多次Top名次。现为CSDN博客专家、人工智能领域优质创作者。喜欢通过博客创作的方式对所学......
  • Online Anomalous Subtrajectory Detection onRoad Networks with Deep Reinforcement
    该代码采用计算图的方式进行计算编码RSRNetdetermine做奖励的计算reward=explore(model,observations,labels,text,SRN,batched_data)策略优化ASDNet中explore做的动作就是在做预测,并会记录这里的值用作后面的计算是的,你理解得对。ep_as存储的动作实际上就是......
  • 12.SpringCloudAlibabaSentinel实现熔断和限流
    1.Sentinel1.1官网sentinel官网,类似SpringCloudCircuitBreaker。1.2是什么面向分布式、多语言异构化服务架构的流量治理组件。1.3下载地址https://github.com/alibaba/Sentinel/releases1.4能干吗Sentinel以流量为切入点,从流量控制、流量路由、熔断降级、系统自适......
  • MeIoN's XCPC Library - ICPC2024 - Kunming
    MeIoN'sXCPCLibrary-ICPC2024-Kunming目录MeIoN'sXCPCLibrary-ICPC2024-Kunming目录Z_HMeIoN_H.hppMeIoN_IO.hppMeIoN_PRET.hppMeIoN_debug.hppfast_io.hppdsLinearBasis.hppWavelet_Matrix.hppbit_vec.hppchothlly.hppdsu.hppfenw......