java 设计模式:外观设计模式

2023-10-23 22:48

本文主要是介绍java 设计模式:外观设计模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、概念

外观设计模式的主要目的在于让外部减少与子系统内部多个模块的交互,从而让外部能够更简单的使用子系统。他负责把客户端的请求转发给子系统内部的各个模块进行处理。

2、使用场景

  1. 当你要为一个复杂子系统提供一个简单接口时
  2. 客户程序与抽象类的实现部分之间存在很大的依赖性。引入外观类可以将子系统与客户端解耦,从而提高子系统的独立性和可移植性。
  3. 当你需要构建一个层次结构的子系统时; 在层次化结构中,可以使用外观模式定义系统中每一层的入口,层与层之间不直接产生联系,而通过外观类建立联系,降低层之间的耦合度。

3、UML结构图分析

4、实际代码分析


/*** 模块A*/
public class SubSystemA {public void testFunA(){System.out.println("testFunA");}}
/*** 模块B*/
public class SubSystemB {public void testFunB(){System.out.println("testFunB");}
}
/*** 模块C*/
public class SubSystemC {public void testFunC(){System.out.println("testFunC");}
}
/*** Facade */
public class Facade {private SubSystemA subSystemA;private SubSystemB subSystemB;private SubSystemC subSystemC;private Facade(){subSystemA = new SubSystemA();subSystemB = new SubSystemB();subSystemC = new SubSystemC();}private static Facade instance;public static Facade getInstance(){if(instance==null){instance = new Facade();}return instance;}public void tastOperation(){subSystemA.testFunA();subSystemB.testFunB();subSystemC.testFunC();}
}//运行Facade.getInstance().tastOperation();

由于外观类维持了对多个子系统类的引用,外观对象在系统运行时将占用较多的系统资源,因此需要对外观对象的数量进行限制,避免系统资源的浪费。可以结合单例模式对外观类进行改进,将外观类设计为一个单例类。通过对外观模式单例化,可以确保系统中只有唯一一个访问子系统的入口,降低系统资源的消耗。

我在项目中的实践:

在项目中经常会出现,网络请求,缓存本地,本地有缓存用本地缓存,而且网络请求经常会在多个地方调用,如果不采用外观模式设计,则会出现客户端的代码异常复杂,而且不利于维护。于是我就进行了如下改变,建立中间仓库类来进行数据切换,客户端只需要进行对仓库数据进行调用,不用关心仓库里数据怎样生成的。

/*** 建立仓库接口类* TestApiDataSource*/
public interface TestApiDataSource {/*** 登陆接口* @param params* @return*/Observable<GetLoginResponse> getLogin(GetLoginParams params);
}
/*** 建立本地数据源(主要是为了方便客户端调用)* TestApiLocalDataSource*/
public class TestApiLocalDataSource extends BaseLocalDataSource implements TestApiDataSource {@Overridepublic Observable<GetLoginResponse> getLogin(GetLoginParams params) {Observable<GetLoginResponse> observable = Observable.create(new ObservableOnSubscribe<GetLoginResponse>() {@Overridepublic void subscribe(ObservableEmitter<GetLoginResponse> subscriber)  throws Exception {subscriber.onComplete();}});return observable;}}/*** 建立网络数据源* TestApiRemoteDataSource*/
public class TestApiRemoteDataSource extends BaseRemoteDataSource implements TestApiDataSource {/**** 请求网络* @param params* @return*/@Overridepublic Observable<GetLoginResponse> getLogin(GetLoginParams params) {return ApiSource.getApiService(AppHuanJingFactory.getAppModel().getApi()).getApi2Service().getLogin(params);}}/*** 建立单例仓库类* TestApiRepository*/
public class TestApiRepository extends BaseRepository<TestApiLocalDataSource,TestApiRemoteDataSource> implements TestApiDataSource {public static volatile TestApiRepository instance;public static TestApiRepository getInstance(){if(instance==null){synchronized (TestApiRepository.class){if(instance==null){instance = new TestApiRepository(new TestApiLocalDataSource(),new TestApiRemoteDataSource());}}}return instance;}protected TestApiRepository(TestApiLocalDataSource localDataSource, TestApiRemoteDataSource remoteDataSource) {super(localDataSource, remoteDataSource);}/*** 数据源切换* #getLogin#* @param params* @return*/@Overridepublic Observable<GetLoginResponse> getLogin(GetLoginParams params) {Observable<GetLoginResponse> observable = Observable.concat(localDataSource.getLogin(params),remoteDataSource.getLogin(params).doOnNext(new Consumer<GetLoginResponse>() {@Overridepublic void accept(GetLoginResponse response) throws Exception {/*** cache*/}})).compose(RxTransformerHelper.<GetLoginResponse>ioToUI()).firstOrError().toObservable();return observable;}}
//客户端执行,不需要考虑具体实现
TestApiRepository.getInstance().getLogin(new GetLoginParams()).subscribe(new BaseRxNetworkResponseObserver<GetLoginResponse>() {@Overridepublic void onResponse(GetLoginResponse getLoginResponse) {}@Overridepublic void onResponseFail(Exception e) {}@Overrideprotected void onBeforeResponseOperation() {}@Overridepublic void onSubscribe(Disposable d) {add(d);}});

 

优点:

  1. 由于Facade类封装了各个模块交互过程,如果今后内部模块调用关系发生了变化,只需要修改facade实现就可以了
  2. facade实现是可以被多个客户端调用的
  3. 使得客户端和子系统之间解耦,让子系统内部的模块功能更容易扩展和维护;客户端根本不需要知道子系统内部的实现,或者根本不需要知道子系统内部的构成,它只需要跟Facade类交互即可。

这篇关于java 设计模式:外观设计模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/270940

相关文章

springboot控制bean的创建顺序

《springboot控制bean的创建顺序》本文主要介绍了spring-boot控制bean的创建顺序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1、order注解(不一定有效)2、dependsOn注解(有效)3、提前将bean注册为Bea

Java中的ConcurrentBitSet使用小结

《Java中的ConcurrentBitSet使用小结》本文主要介绍了Java中的ConcurrentBitSet使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、核心澄清:Java标准库无内置ConcurrentBitSet二、推荐方案:Eclipse

java中的Supplier接口解析

《java中的Supplier接口解析》Java8引入的Supplier接口是一个无参数函数式接口,通过get()方法延迟计算结果,它适用于按需生成场景,下面就来介绍一下如何使用,感兴趣的可以了解一下... 目录1. 接口定义与核心方法2. 典型使用场景场景1:延迟初始化(Lazy Initializati

Java中ScopeValue的使用小结

《Java中ScopeValue的使用小结》Java21引入的ScopedValue是一种作用域内共享不可变数据的预览API,本文就来详细介绍一下Java中ScopeValue的使用小结,感兴趣的可以... 目录一、Java ScopedValue(作用域值)详解1. 定义与背景2. 核心特性3. 使用方法

spring中Interceptor的使用小结

《spring中Interceptor的使用小结》SpringInterceptor是SpringMVC提供的一种机制,用于在请求处理的不同阶段插入自定义逻辑,通过实现HandlerIntercept... 目录一、Interceptor 的核心概念二、Interceptor 的创建与配置三、拦截器的执行顺

Java中Map的五种遍历方式实现与对比

《Java中Map的五种遍历方式实现与对比》其实Map遍历藏着多种玩法,有的优雅简洁,有的性能拉满,今天咱们盘一盘这些进阶偏基础的遍历方式,告别重复又臃肿的代码,感兴趣的小伙伴可以了解下... 目录一、先搞懂:Map遍历的核心目标二、几种遍历方式的对比1. 传统EntrySet遍历(最通用)2. Lambd

Spring Boot 中 RestTemplate 的核心用法指南

《SpringBoot中RestTemplate的核心用法指南》本文详细介绍了RestTemplate的使用,包括基础用法、进阶配置技巧、实战案例以及最佳实践建议,通过一个腾讯地图路线规划的案... 目录一、环境准备二、基础用法全解析1. GET 请求的三种姿势2. POST 请求深度实践三、进阶配置技巧1

springboot+redis实现订单过期(超时取消)功能的方法详解

《springboot+redis实现订单过期(超时取消)功能的方法详解》在SpringBoot中使用Redis实现订单过期(超时取消)功能,有多种成熟方案,本文为大家整理了几个详细方法,文中的示例代... 目录一、Redis键过期回调方案(推荐)1. 配置Redis监听器2. 监听键过期事件3. Redi

Spring Boot 处理带文件表单的方式汇总

《SpringBoot处理带文件表单的方式汇总》本文详细介绍了六种处理文件上传的方式,包括@RequestParam、@RequestPart、@ModelAttribute、@ModelAttr... 目录方式 1:@RequestParam接收文件后端代码前端代码特点方式 2:@RequestPart接

SpringBoot整合Zuul全过程

《SpringBoot整合Zuul全过程》Zuul网关是微服务架构中的重要组件,具备统一入口、鉴权校验、动态路由等功能,它通过配置文件进行灵活的路由和过滤器设置,支持Hystrix进行容错处理,还提供... 目录Zuul网关的作用Zuul网关的应用1、网关访问方式2、网关依赖注入3、网关启动器4、网关全局变