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

【设计模式】JAVA Design Patterns——Abstract-document(抽象文档模式)

 🔍 目的


使用动态属性,并在保持类型安全的同时实现非类型化语言的灵活性。

 🔍 解释


抽象文档模式使您能够处理其他非静态属性。 此模式使用特征的概念来实现类型安全,并将不同类的属性分离为一组接口

真实世界例子

考虑由多个部分组成的计算机。 但是,我们不知道特定计算机是否真的拥有所有零件,或者仅仅是零件中的一部分。 我们的计算机是动态而且非常灵活的。

通俗的说

抽象文档模式允许在对象不知道的情况下将属性附加到对象。

维基百科

面向对象的结构设计模式,用于组织松散类型的键值存储中的对象并使用类型化的视图公开数据。 该模式的目的是在强类型语言中实现组件之间的高度灵活性,在这种语言中,可以在不丢失类型安全支持的情况下,将新属性动态地添加到对象树中。 该模式利用特征将类的不同属性分成不同的接口。

 🔍 程序示例

让我们首先定义基类DocumentAbstractDocument。 它们基本上使对象拥有属性映射和任意数量的子对象。

public interface Document {Void put(String key, Object value);Object get(String key);<T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor);
}public abstract class AbstractDocument implements Document {private final Map<String, Object> properties;protected AbstractDocument(Map<String, Object> properties) {Objects.requireNonNull(properties, "properties map is required");this.properties = properties;}@Overridepublic Void put(String key, Object value) {properties.put(key, value);return null;}@Overridepublic Object get(String key) {return properties.get(key);}@Overridepublic <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {return Stream.ofNullable(get(key)).filter(Objects::nonNull).map(el -> (List<Map<String, Object>>) el).findAny().stream().flatMap(Collection::stream).map(constructor);}...
}

 接下来,我们定义一个枚举“属性”和一组类型,价格,模型和零件的接口。 这使我们能够为Car类创建静态外观的界面。

public enum Property {PARTS, TYPE, PRICE, MODEL
}public interface HasType extends Document {default Optional<String> getType() {return Optional.ofNullable((String) get(Property.TYPE.toString()));}
}public interface HasPrice extends Document {default Optional<Number> getPrice() {return Optional.ofNullable((Number) get(Property.PRICE.toString()));}
}
public interface HasModel extends Document {default Optional<String> getModel() {return Optional.ofNullable((String) get(Property.MODEL.toString()));}
}public interface HasParts extends Document {default Stream<Part> getParts() {return children(Property.PARTS.toString(), Part::new);}
}

我们准备介绍Car

public class Car extends AbstractDocument implements HasModel, HasPrice, HasParts {public Car(Map<String, Object> properties) {super(properties);}
}

完整示例中的Car构造和使用方式。

    LOGGER.info("Constructing parts and car");var wheelProperties = Map.of(Property.TYPE.toString(), "wheel",Property.MODEL.toString(), "15C",Property.PRICE.toString(), 100L);var doorProperties = Map.of(Property.TYPE.toString(), "door",Property.MODEL.toString(), "Lambo",Property.PRICE.toString(), 300L);var carProperties = Map.of(Property.MODEL.toString(), "300SL",Property.PRICE.toString(), 10000L,Property.PARTS.toString(), List.of(wheelProperties, doorProperties));var car = new Car(carProperties);LOGGER.info("Here is our car:");LOGGER.info("-> model: {}", car.getModel().orElseThrow());LOGGER.info("-> price: {}", car.getPrice().orElseThrow());LOGGER.info("-> parts: ");car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}",p.getType().orElse(null),p.getModel().orElse(null),p.getPrice().orElse(null)));// Constructing parts and car// Here is our car:// model: 300SL// price: 10000// parts: // wheel/15C/100// door/Lambo/300

 🔍 类图

Abstract Document Traits and Domain 

 🔍 适用性


使用抽象文档模式当

  • 需要即时添加新属性
  • 你想要一种灵活的方式来以树状结构组织域
  • 你想要更宽松的耦合系统

http://www.lryc.cn/news/349058.html

相关文章:

  • 5.13网络编程
  • 那些年使用过的UA头
  • IT技术产品:开发者极为重要的思维习惯
  • 软件产品质量模型及其子特性
  • 神经网络中的误差反向传播(Backpropagation)方法理解
  • Day 32 shell变量及运算
  • 八、VUE内置指令
  • 学习笔记:IEEE 1003.13-2003【POSIX PSE53接口列表】
  • springboot logback 日志注入安全问题 统一处理
  • linux进阶高级配置,你需要知道的有哪些(13)-Squid代理服务器
  • SpringBoot自动装配(二)
  • 数据结构 顺序表1
  • C++基础-编程练习题1
  • 四十九坊股权设计,白酒新零售分红制度,新零售策划机构
  • 如何将公众号添加到CSDN个人主页
  • 64K方法数限制原理及解决方案
  • 产品品牌CRUD
  • 惠普发布全新AI战略,重塑办公空间 引领企业智能化新浪潮
  • python中的数据可视化:极坐标散点图
  • 5.12.1 Detecting and classifying lesions in mammograms with Deep Learning
  • Python爬虫——如何使用urllib的HTTP基本库
  • OceanBase v4.3特性解析:新功能“租户克隆”的场景与应用指南
  • RS3236-3.3YUTDN4功能和参数介绍及PDF资料
  • Vue如何引入公用方法
  • Java面试题:ConcurrentHashMap
  • 现在闪侠惠递寄快递有福利了,千万不要因没把握住而后悔呀!
  • vue+ant-design+formBuiler表单构建器——技能提升——form design——亲测有效
  • Git 如何管理标签命令(tag)
  • 零样本身份保持:ID-Animator引领个性化视频生成技术新前沿
  • 在Go语言中,可以这样使用Json