抽象文档模式可以处理额外的非静态属性。该模式使用特征的概念来实现类型安全并将不同类的属性分离到一组接口中。
    Abstract-Document模式 - 图1
    考虑由多个部件组成的汽车。然而,我们不知道具体的汽车是否真的拥有所有零件,或者只是其中的一部分。我们的汽车充满活力且极其灵活。
    让我们首先定义基类Document和AbstractDocument. 它们基本上使对象拥有一个属性映射和任意数量的子对象。

    1. public interface Document {
    2. Void put(String key, Object value);
    3. Object get(String key);
    4. <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor);
    5. }
    6. public abstract class AbstractDocument implements Document {
    7. private final Map<String, Object> properties;
    8. protected AbstractDocument(Map<String, Object> properties) {
    9. Objects.requireNonNull(properties, "properties map is required");
    10. this.properties = properties;
    11. }
    12. @Override
    13. public Void put(String key, Object value) {
    14. properties.put(key, value);
    15. return null;
    16. }
    17. @Override
    18. public Object get(String key) {
    19. return properties.get(key);
    20. }
    21. @Override
    22. public <T> Stream<T> children(String key, Function<Map<String, Object>, T> constructor) {
    23. return Stream.ofNullable(get(key))
    24. .filter(Objects::nonNull)
    25. .map(el -> (List<Map<String, Object>>) el)
    26. .findAny()
    27. .stream()
    28. .flatMap(Collection::stream)
    29. .map(constructor);
    30. }
    31. ...
    32. }

    接下来我们定义一个枚举Property和一组类型、价格、模型和零件的接口。这允许我们为我们的Car类创建静态外观界面。

    1. public enum Property {
    2. PARTS, TYPE, PRICE, MODEL
    3. }
    4. public interface HasType extends Document {
    5. default Optional<String> getType() {
    6. return Optional.ofNullable((String) get(Property.TYPE.toString()));
    7. }
    8. }
    9. public interface HasPrice extends Document {
    10. default Optional<Number> getPrice() {
    11. return Optional.ofNullable((Number) get(Property.PRICE.toString()));
    12. }
    13. }
    14. public interface HasModel extends Document {
    15. default Optional<String> getModel() {
    16. return Optional.ofNullable((String) get(Property.MODEL.toString()));
    17. }
    18. }
    19. public interface HasParts extends Document {
    20. default Stream<Part> getParts() {
    21. return children(Property.PARTS.toString(), Part::new);
    22. }
    23. }

    现在我们准备介绍Car.

    1. public class Car extends AbstractDocument implements HasModel, HasPrice, HasParts {
    2. public Car(Map<String, Object> properties) {
    3. super(properties);
    4. }
    5. }

    最后,这是我们Car在完整示例中构建和使用 的方式。

    1. public static void main(String[] args) {
    2. LOGGER.info("Constructing parts and car");
    3. var wheelProperties = Map.of(
    4. Property.TYPE.toString(), "wheel",
    5. Property.MODEL.toString(), "15C",
    6. Property.PRICE.toString(), 100L);
    7. var doorProperties = Map.of(
    8. Property.TYPE.toString(), "door",
    9. Property.MODEL.toString(), "Lambo",
    10. Property.PRICE.toString(), 300L);
    11. var carProperties = Map.of(
    12. Property.MODEL.toString(), "300SL",
    13. Property.PRICE.toString(), 10000L,
    14. Property.PARTS.toString(), List.of(wheelProperties, doorProperties));
    15. var car = new Car(carProperties);
    16. LOGGER.info("Here is our car:");
    17. LOGGER.info("-> model: {}", car.getModel().orElseThrow());
    18. LOGGER.info("-> price: {}", car.getPrice().orElseThrow());
    19. LOGGER.info("-> parts: ");
    20. car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}",
    21. p.getType().orElse(null),
    22. p.getModel().orElse(null),
    23. p.getPrice().orElse(null))
    24. );
    25. }