It is proposed that this article be deleted because of the following concern:
If you can address this concern by improving, copyediting, sourcing, renaming, or merging the page, please edit this page and do so. You may remove this message if you improve the article or otherwise object to deletion for any reason. Although not required, you are encouraged to explain why you object to the deletion, either in your edit summary or on the talk page. If this template is removed, do not replace it . The article may be deleted if this message remains in place for seven days, i.e., after 21:12, 2 August 2025 (UTC). Find sources: "Abstract Document Pattern" – news · newspapers · books · scholar · JSTOR |
Another editor has reviewed this page's proposed deletion , endorses the proposal to delete, and adds: If you remove the {{proposed deletion/dated}} tag above, please also remove this {{Proposed deletion endorsed}} tag. |
This article relies largely or entirely on a single source .(January 2016) |
An object-oriented structural design pattern for organizing objects in loosely typed key-value stores and exposing the data using typed views. The purpose of the pattern is to achieve a high degree of flexibility between components in a strongly typed language where new properties can be added to the object-tree on the fly, without losing the support of type-safety. The pattern makes use of traits to separate different properties of a class into different interfaces. [1] The term "document" is inspired from document-oriented databases.
A document is an object that contains a number of properties. A property can for an example be a value like a number or a string, or it can be a list of other documents. Every property is referenced using a key. [2] When traversing the document tree, the user specifies a constructor to be used for creating the implementation class of the next level. The implementations are often a union of various traits that extend the Document interface, making it possible for them to handle setting and getting properties on their own.
The interface "Document" states that properties can be edited using the "put" method, read using the "get" method and sub-documents can be traversed using the "children" method. The "children" method requires a functional reference to a method that can produce a typed view of a child given a map of the data the child should have. The map should be a pointer to the original map so that changes in the view also affect the original document.
Implementations can inherit from multiple trait interfaces that describe different properties. Multiple implementations can even share the same map, the only restriction the pattern puts on the design of the implementation is that it must be stateless except for the properties inherited from "BaseDocument".
interface Document put(key : String, value : Object) : Object get(key : String) : Object children(key : String, constructor : Map<String, Object> -> T) : T[] abstractclass BaseDocument : Document properties : Map<String, Object> constructor(properties : Map<String, Object>) this->properties := properties implement put(key : String, value : Object) : Object return this->properties->put(key, value) implement get(key : String) : Object return this->properties->get(key) implement children(key : String, constructor : Map<String, Object> -> T) : T[] var result := new T[] var children := this->properties->get(key) castTo Map<String, Object>[] foreach ( child in children ) result[] := constructor->apply(child) return result
The Abstract Document Pattern allows the developer to store variables like configuration settings in an untyped tree structure and operate on the documents using typed views. New views or alternative implementations of views can be created without affecting the internal document structure. The advantage of this is a more loosely coupled system, but it also increases the risk of casting errors as the inherit type of a property is not always certain.
The full implementation of the Abstract Document pattern is available at https://java-design-patterns.com/patterns/abstract-document/. Here are the key classes shortly.
Document.java
publicinterfaceDocument{Objectput(Stringkey,Objectvalue);Objectget(Stringkey);<T>Stream<T>children(Stringkey,Function<Map<String,Object>,T>constructor););}BaseDocument.java
publicabstractclassAbstractDocumentimplementsDocument{privatefinalMap<String,Object>properties;protectedAbstractDocument(Map<String,Object>properties){Objects.requireNonNull(properties,"properties map is required");this.properties=properties;}@OverridepublicVoidput(Stringkey,Objectvalue){properties.put(key,value);returnnull;}@OverridepublicObjectget(Stringkey){returnproperties.get(key);}@Overridepublic<T>Stream<T>children(Stringkey,Function<Map<String,Object>,T>constructor){returnStream.ofNullable(get(key)).filter(Objects::nonNull).map(el->(List<Map<String,Object>>)el).findAny().stream().flatMap(Collection::stream).map(constructor);}}Usage.java
varcarProperties=...;varcar=newCar(carProperties);Stringmodel=car.getModel().orElseThrow());intprice=car.getPrice().orElseThrow());varparts=car.getParts();