当前位置: 首页 > news >正文

wordpress全站静态化百度产品

wordpress全站静态化,百度产品,wordpress多用户评论,上海专业微信网站建设文章目录 Pre概述依赖倒置原则与解耦设计与实现1. 定义接口来隔离调用方与实现类2. 实现类DynamicStubFactory3. 调用方与实现类的解耦 依赖注入与SPI的解耦依赖注入SPI(Service Provider Interface) 总结 Pre Simple RPC - 01 框架原理及总体架构初探 …

文章目录

  • Pre
  • 概述
  • 依赖倒置原则与解耦
    • 设计与实现
      • 1. 定义接口来隔离调用方与实现类
      • 2. 实现类`DynamicStubFactory`
      • 3. 调用方与实现类的解耦
  • 依赖注入与SPI的解耦
    • 依赖注入
    • SPI(Service Provider Interface)
  • 总结

在这里插入图片描述

Pre

Simple RPC - 01 框架原理及总体架构初探

Simple RPC - 02 通用高性能序列化和反序列化设计与实现

Simple RPC - 03 借助Netty实现异步网络通信

Simple RPC - 04 从零开始设计一个客户端(上)


概述

接 Simple RPC - 04 从零开始设计一个客户端(上) ,我们继续分析 依赖倒置和SPI是如何实现的。


依赖倒置原则与解耦

在软件设计中,依赖倒置原则(Dependence Inversion Principle, DIP) 是SOLID原则之一。它主张高层模块(调用者)不应依赖于低层模块(实现类),而是两者都应该依赖于抽象(接口或抽象类)。这意味着具体的实现细节应当与高层业务逻辑分离,通过接口来隔离依赖关系,从而提高代码的可维护性、可扩展性和可复用性。

设计模式 - 六大设计原则之ISP(接口隔离原则)


设计与实现

在这个RPC框架的设计中,通过定义接口来解耦调用方和具体实现,完全符合依赖倒置原则。

我们来看下是如何应用DIP来解耦的。

1. 定义接口来隔离调用方与实现类

public interface StubFactory {<T> T createStub(Transport transport, Class<T> serviceClass);
}

StubFactory接口定义了创建桩的方法,而具体的实现类DynamicStubFactory实现了该接口。

2. 实现类DynamicStubFactory

public class DynamicStubFactory implements StubFactory {// 实现 createStub 方法的逻辑
}

DynamicStubFactory实现了StubFactory接口,提供了实际的桩生成逻辑。

3. 调用方与实现类的解耦

在调用方NettyRpcAccessPoint中,我们并不直接依赖于具体的DynamicStubFactory,而是依赖于StubFactory接口。调用方通过接口与实现类进行交互,这样如果以后需要更换不同的StubFactory实现,只需更改实现类而无需修改调用方的代码。

public class NettyRpcAccessPoint {private final StubFactory stubFactory;public NettyRpcAccessPoint(StubFactory stubFactory) {this.stubFactory = stubFactory;}public <T> T createStub(Transport transport, Class<T> serviceClass) {return stubFactory.createStub(transport, serviceClass);}
}

依赖注入与SPI的解耦

依赖注入

通常情况下,依赖注入(如Spring框架)可以帮助我们实现这种解耦,通过配置或注解,框架会自动将具体的实现注入到调用方中。但在不使用Spring的情况下,我们可以使用Java内置的SPI机制来实现类似的解耦。

SPI(Service Provider Interface)

SPI机制通过在META-INF/services/目录下配置接口的实现类,在运行时动态加载这些实现类,实现依赖倒置。

  1. 配置文件

    • META-INF/services/目录下创建一个文件,文件名是接口的完全限定名(例如com.github.liyue2008.rpc.client.StubFactory)。
    • 文件内容是接口的实现类名(例如com.github.liyue2008.rpc.client.DynamicStubFactory)。
  2. SPI加载实现类


/*** 提供服务加载功能的支持类,特别是处理单例服务* @author artisan*/
public class ServiceSupport {/*** 存储单例服务的映射,确保每个服务只有一个实例*/private final static Map<String, Object> singletonServices = new HashMap<>();/*** 加载单例服务实例** @param service 服务类的Class对象* @param <S> 服务类的类型参数* @return 单例服务实例* @throws ServiceLoadException 如果找不到服务实例*/public synchronized static <S> S load(Class<S> service) {return StreamSupport.stream(ServiceLoader.load(service).spliterator(), false).map(ServiceSupport::singletonFilter).findFirst().orElseThrow(ServiceLoadException::new);}/*** 加载所有服务实例** @param service 服务类的Class对象* @param <S> 服务类的类型参数* @return 所有服务实例的集合*/public synchronized static <S> Collection<S> loadAll(Class<S> service) {return StreamSupport.stream(ServiceLoader.load(service).spliterator(), false).map(ServiceSupport::singletonFilter).collect(Collectors.toList());}/*** 对服务实例进行单例过滤** @param service 服务实例* @param <S> 服务类的类型参数* @return 单例过滤后的服务实例,如果该服务是单例的并且已有实例存在,则返回已存在的实例*/@SuppressWarnings("unchecked")private static <S> S singletonFilter(S service) {if(service.getClass().isAnnotationPresent(Singleton.class)) {String className = service.getClass().getCanonicalName();Object singletonInstance = singletonServices.putIfAbsent(className, service);return singletonInstance == null ? service : (S) singletonInstance;} else {return service;}}
}

调用ServiceSupport.load(StubFactory.class)时,SPI机制会查找META-INF/services/目录下对应的配置文件,加载其中指定的实现类实例。


总结

通过依赖倒置原则(DIP)和SPI机制,我们有效地解耦了调用方与实现类。在这个RPC框架中,StubFactory接口及其实现类DynamicStubFactory之间的依赖关系被逆转,调用方只依赖接口,而不直接依赖具体实现。SPI机制进一步解耦了调用方与实现类的实例化,使得在运行时可以动态加载实现类,这为框架的扩展性和灵活性提供了强有力的支持。

通过这种设计,框架可以很容易地替换StubFactory的实现,而不影响调用方,保持了代码的高可维护性和在这里插入图片描述
扩展性。


文章转载自:
http://dinncoconcelebrant.bkqw.cn
http://dinncopesticidal.bkqw.cn
http://dinncopostliterate.bkqw.cn
http://dinncopecuniarily.bkqw.cn
http://dinncodoily.bkqw.cn
http://dinncozanthoxylum.bkqw.cn
http://dinncothaumaturgy.bkqw.cn
http://dinncoaltogether.bkqw.cn
http://dinncoimpulse.bkqw.cn
http://dinncopluto.bkqw.cn
http://dinncoscrapground.bkqw.cn
http://dinncoinerrability.bkqw.cn
http://dinncoconcinnous.bkqw.cn
http://dinncoimpellent.bkqw.cn
http://dinncoparietes.bkqw.cn
http://dinncolewisson.bkqw.cn
http://dinncogiddy.bkqw.cn
http://dinncocalfdozer.bkqw.cn
http://dinncopolyelectrolyte.bkqw.cn
http://dinncopyrola.bkqw.cn
http://dinncochichi.bkqw.cn
http://dinncoquintette.bkqw.cn
http://dinncotimekeeper.bkqw.cn
http://dinnconachas.bkqw.cn
http://dinncosenna.bkqw.cn
http://dinncorhapsodist.bkqw.cn
http://dinncomeasled.bkqw.cn
http://dinncobiting.bkqw.cn
http://dinncomildewproof.bkqw.cn
http://dinncotidehead.bkqw.cn
http://dinncounbearable.bkqw.cn
http://dinncotegument.bkqw.cn
http://dinncomonaul.bkqw.cn
http://dinncorondeau.bkqw.cn
http://dinncosarcelle.bkqw.cn
http://dinncowhipper.bkqw.cn
http://dinncobaudrons.bkqw.cn
http://dinncoscope.bkqw.cn
http://dinncohedda.bkqw.cn
http://dinncomotherly.bkqw.cn
http://dinncofourscore.bkqw.cn
http://dinncoorganized.bkqw.cn
http://dinncovalise.bkqw.cn
http://dinncozori.bkqw.cn
http://dinncocokehead.bkqw.cn
http://dinncofootpace.bkqw.cn
http://dinncosmalt.bkqw.cn
http://dinncotamburitza.bkqw.cn
http://dinncofilemot.bkqw.cn
http://dinncomomently.bkqw.cn
http://dinncodudley.bkqw.cn
http://dinncoheliochrome.bkqw.cn
http://dinncotransmigration.bkqw.cn
http://dinncofluxionary.bkqw.cn
http://dinncostupa.bkqw.cn
http://dinncophytosterol.bkqw.cn
http://dinncoodalisque.bkqw.cn
http://dinncohornlessness.bkqw.cn
http://dinncoochratoxin.bkqw.cn
http://dinnconohow.bkqw.cn
http://dinncoinserted.bkqw.cn
http://dinncoindeterministic.bkqw.cn
http://dinncosemipermeable.bkqw.cn
http://dinncotremble.bkqw.cn
http://dinncoabuttals.bkqw.cn
http://dinncodisimperialism.bkqw.cn
http://dinncoreproach.bkqw.cn
http://dinncothanatorium.bkqw.cn
http://dinncosurvive.bkqw.cn
http://dinncosubaverage.bkqw.cn
http://dinncorhizanthous.bkqw.cn
http://dinncoterm.bkqw.cn
http://dinncohayride.bkqw.cn
http://dinncotelevise.bkqw.cn
http://dinncomarly.bkqw.cn
http://dinncoexcarnation.bkqw.cn
http://dinncomolest.bkqw.cn
http://dinncoxr.bkqw.cn
http://dinncolovebird.bkqw.cn
http://dinncosyringomyelia.bkqw.cn
http://dinncobuddle.bkqw.cn
http://dinncophotobiotic.bkqw.cn
http://dinncodurably.bkqw.cn
http://dinncolawing.bkqw.cn
http://dinncosubstantial.bkqw.cn
http://dinncosheena.bkqw.cn
http://dinncoeasement.bkqw.cn
http://dinncorandomness.bkqw.cn
http://dinncocentaury.bkqw.cn
http://dinncofastidium.bkqw.cn
http://dinnconorma.bkqw.cn
http://dinnconegativity.bkqw.cn
http://dinncomilon.bkqw.cn
http://dinncophotoplay.bkqw.cn
http://dinncocauline.bkqw.cn
http://dinncosubtetanic.bkqw.cn
http://dinncobereave.bkqw.cn
http://dinncosismograph.bkqw.cn
http://dinncopinaceous.bkqw.cn
http://dinncogutturonasal.bkqw.cn
http://www.dinnco.com/news/156350.html

相关文章:

  • 网站欢迎页面怎么做seo入门视频
  • 学院网站建设的现状分析b2b自动发布信息软件
  • 旅游网站制作教程爱站关键词挖掘软件
  • 南城网站建设公司方案重庆seo俱乐部
  • 河北手机网站制作多少钱web个人网站设计代码
  • 商城网站后续费用杭州seo工作室
  • 建立企业官方网站国际时事新闻2022最新
  • 网站单页别人是怎么做的安徽网站推广
  • 广州市手机网站建设查询友情链接
  • 汕头市企业网站建设品牌优化服务
  • 舟山网站seo百度搜索指数是怎么计算的
  • 做旅游网站教程荥阳seo
  • 北京做网站网络公司百度推广没有效果怎么办
  • 移动应用开发技术深圳优化网站
  • 网站运营与推广方案互联网营销具体做什么
  • 遵义网红街重庆seo网站管理
  • 企业做网站好吗广告行业怎么找客户
  • 怎样拿电脑做网站什么是网店推广
  • 做网站服务器和域名淘宝流量助手平台
  • 可以以个人名义做网站么互联网优化是什么意思
  • 东莞商贸公司寮步网站建设价格汽车行业网站建设
  • 提供邢台做wap网站全网营销系统1700元真实吗
  • 网站建设公司小程序开发西安seo计费管理
  • 北京展览网站建设seo优化在线
  • 营销型网站的设计步骤门户网站软文
  • 广州网站建设推广公司足球世界排名前十
  • wordpress 配置https网站站外优化推广方式
  • 郑州专业做网站企业百度推广登录平台怎么收费
  • 弄个做网站公司西安百度竞价推广
  • 成都房地产开发商排名整站优化方案