设计模式 ------- 仿写Android RxJava 中的装饰器模式

2023-12-16 01:08

本文主要是介绍设计模式 ------- 仿写Android RxJava 中的装饰器模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

仿写RxJava中的装饰器模式:


整个例子的背景:用户选择商品、下单、店家发货、快递、拿到商品。

交易类:Delivery

public abstract class Delivery {public abstract void transport(Product product);public void arrive(Product product){transport(product);}// 创建订单与收获地址public static Delivery create(String name,Order order){// 创建收获地址DeliveryCreate deliveryCreate=new DeliveryCreate(order);// 创建快递return new DeliveryProcess(deliveryCreate,name);}// 设置商品传递流程public Delivery sendOrderToLocation(String name){return new DeliveryProcess(this,name);}// 设置商品传递流程,且做些事public Delivery sendOrderToLocation(String name,String thing){return new DeliveryProcess(this,name,thing);}// 转换商品public Delivery map(String name,Exchange<?,?> p){return new DeliveryProcess(this,name,p);}
}

创建订单和收获地址类:

public class DeliveryCreate extends Delivery{// 订单private Order order;public DeliveryCreate(Order order){this.order = order;}// 收获地址@Overridepublic void transport(Product product) {System.out.println("用户拿到了商品");order.apply(product);}
}

订单类:Order

public interface Order {void apply(Product dothing);
}

快递传递类:DeliveryProcess

public class DeliveryProcess extends Delivery{private Exchange exchange;private String name;private String otherThing;private Delivery parent;public DeliveryProcess(Delivery nextNode, String name){System.out.println("LogOut:订单到达-----"+name);this.parent= nextNode;this.name= name;}public DeliveryProcess(Delivery nextNode,String name,Exchange exchange){System.out.println("LogOut:订单到达-----"+name);this.name=name;this.parent= nextNode;this.exchange= exchange;}public DeliveryProcess(Delivery nextNode, String name,String otherThing){System.out.println("LogOut:订单到达-----"+name);this.parent= nextNode;this.name= name;this.otherThing=otherThing;}// 传递快递@Overridepublic void transport(Product product) {if(name!=null){System.out.println("LogOut:商品已发货,目前到达-----"+name);}if(otherThing!=null){System.out.println("LogOut:商品检查:-----"+otherThing);}Product newProduct=product;if (exchange!=null){newProduct=exchange.change(product);}// 将快递给下一个快递站parent.transport(newProduct);}}

商品类:Product

public interface Product<T> {void size(int d);void number(int t);void color(String  e);void content(T info);
}

商品转换类:

public interface Exchange<R,T> {Product<T> change(Product<R> product);
}

main:

Delivery.create("用户下单地点",new Order() {@Overridepublic void apply(Product dothing) {dothing.color("Red");dothing.size(100);dothing.number(10);dothing.content("三只松鼠");}}).sendOrderToLocation("广东","检查下是否过期了").sendOrderToLocation("四川","看看好不好吃???").sendOrderToLocation("三只松鼠旗舰店").arrive(new Product<String>() {@Overridepublic void number(int d) {System.out.println("商品的个数:"+d);}@Overridepublic void size(int big) {System.out.println("商品的大小:"+big);}@Overridepublic void color(String col) {System.out.println("商品的颜色:"+col);}@Overridepublic void content(String str) {System.out.println("商品的类型:"+str);}});}

执行结果:

LogOut:订单到达-----用户下单地点
LogOut:订单到达-----广东
LogOut:订单到达-----四川
LogOut:订单到达-----三只松鼠旗舰店
LogOut:商品已发货,目前到达-----三只松鼠旗舰店
LogOut:商品已发货,目前到达-----四川
LogOut:商品检查:-----看看好不好吃???
LogOut:商品已发货,目前到达-----广东
LogOut:商品检查:-----检查下是否过期了
LogOut:商品已发货,目前到达-----用户下单地点
用户拿到了商品,三只松鼠
商品的颜色:Red
商品的大小:100
商品的个数:10
商品的类型:三只松鼠

添加商品转换:

Delivery.create("用户下单地点",new Order() {@Overridepublic void apply(Product dothing) {dothing.color("Red");dothing.size(100);dothing.number(10);dothing.content(12);}}).sendOrderToLocation("广东","检查下是否过期了").map("重庆", new Exchange<Integer, String>() {@Overridepublic Product<Integer> change(Product food) {return new Product<Integer>() {@Overridepublic void number(int d) {System.out.println("商品被掉包:number");food.number(5);}@Overridepublic void size(int big) {System.out.println("商品被掉包:size");food.size(big);}@Overridepublic void color(String cor) {System.out.println("商品被掉包:color");food.color("绿色");}@Overridepublic void content(Integer str) {System.out.println("商品被掉包:info");food.content("重庆火锅");}};}}).sendOrderToLocation("四川","看看好不好吃???").sendOrderToLocation("三只松鼠旗舰店").arrive(new Product<String>() {@Overridepublic void number(int d) {System.out.println("商品的个数:"+d);}@Overridepublic void size(int big) {System.out.println("商品的大小:"+big);}@Overridepublic void color(String col) {System.out.println("商品的颜色:"+col);}@Overridepublic void content(String str) {System.out.println("商品的类型:"+str);}});}

打印结果:

LogOut:订单到达-----用户下单地点
LogOut:订单到达-----广东
LogOut:订单到达-----重庆
LogOut:订单到达-----四川
LogOut:订单到达-----三只松鼠旗舰店
LogOut:商品已发货,目前到达-----三只松鼠旗舰店
LogOut:商品已发货,目前到达-----四川
LogOut:商品检查:-----看看好不好吃???
LogOut:商品已发货,目前到达-----重庆
LogOut:商品已发货,目前到达-----广东
LogOut:商品检查:-----检查下是否过期了
LogOut:商品已发货,目前到达-----用户下单地点
用户拿到了商品
商品被掉包:color
商品的颜色:绿色
商品被掉包:size
商品的大小:100
商品被掉包:number
商品的个数:5
商品被掉包:info
商品的类型:重庆火锅

上面的仿写,其实就做了两件事:在拿到商品前,不停的装饰自己(即添加额外操作),拿到商品后,不停的卸装(执行额外操作,然后将商品传给下一层)。

这种设计模式是一种垂直方向添加功能,上一层需要持有下一层的对象。

缺点: 多层装饰比较复杂

 

这篇关于设计模式 ------- 仿写Android RxJava 中的装饰器模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

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、网关全局变

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

基于SpringBoot实现分布式锁的三种方法

《基于SpringBoot实现分布式锁的三种方法》这篇文章主要为大家详细介绍了基于SpringBoot实现分布式锁的三种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、基于Redis原生命令实现分布式锁1. 基础版Redis分布式锁2. 可重入锁实现二、使用Redisso

SpringBoot的全局异常拦截实践过程

《SpringBoot的全局异常拦截实践过程》SpringBoot中使用@ControllerAdvice和@ExceptionHandler实现全局异常拦截,@RestControllerAdvic... 目录@RestControllerAdvice@ResponseStatus(...)@Except

Springboot配置文件相关语法及读取方式详解

《Springboot配置文件相关语法及读取方式详解》本文主要介绍了SpringBoot中的两种配置文件形式,即.properties文件和.yml/.yaml文件,详细讲解了这两种文件的语法和读取方... 目录配置文件的形式语法1、key-value形式2、数组形式读取方式1、通过@value注解2、通过

Java 接口定义变量的示例代码

《Java接口定义变量的示例代码》文章介绍了Java接口中的变量和方法,接口中的变量必须是publicstaticfinal的,用于定义常量,而方法默认是publicabstract的,必须由实现类... 在 Java 中,接口是一种抽象类型,用于定义类必须实现的方法。接口可以包含常量和方法,但不能包含实例