设计模式 ------- 仿写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

相关文章

Spring IOC的三种实现方式详解

《SpringIOC的三种实现方式详解》:本文主要介绍SpringIOC的三种实现方式,在Spring框架中,IOC通过依赖注入来实现,而依赖注入主要有三种实现方式,构造器注入、Setter注入... 目录1. 构造器注入(Cons编程tructor Injection)2. Setter注入(Setter

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

Java function函数式接口的使用方法与实例

《Javafunction函数式接口的使用方法与实例》:本文主要介绍Javafunction函数式接口的使用方法与实例,函数式接口如一支未完成的诗篇,用Lambda表达式作韵脚,将代码的机械美感... 目录引言-当代码遇见诗性一、函数式接口的生物学解构1.1 函数式接口的基因密码1.2 六大核心接口的形态学

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC

Spring Boot统一异常拦截实践指南(最新推荐)

《SpringBoot统一异常拦截实践指南(最新推荐)》本文介绍了SpringBoot中统一异常处理的重要性及实现方案,包括使用`@ControllerAdvice`和`@ExceptionHand... 目录Spring Boot统一异常拦截实践指南一、为什么需要统一异常处理二、核心实现方案1. 基础组件

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的

IDEA运行spring项目时,控制台未出现的解决方案

《IDEA运行spring项目时,控制台未出现的解决方案》文章总结了在使用IDEA运行代码时,控制台未出现的问题和解决方案,问题可能是由于点击图标或重启IDEA后控制台仍未显示,解决方案提供了解决方法... 目录问题分析解决方案总结问题js使用IDEA,点击运行按钮,运行结束,但控制台未出现http://

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

JSON字符串转成java的Map对象详细步骤

《JSON字符串转成java的Map对象详细步骤》:本文主要介绍如何将JSON字符串转换为Java对象的步骤,包括定义Element类、使用Jackson库解析JSON和添加依赖,文中通过代码介绍... 目录步骤 1: 定义 Element 类步骤 2: 使用 Jackson 库解析 jsON步骤 3: 添