You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: abstract-document/README.md
+76-45Lines changed: 76 additions & 45 deletions
Original file line number
Diff line number
Diff line change
@@ -2,35 +2,43 @@
2
2
title: Abstract Document
3
3
category: Structural
4
4
language: en
5
-
tag:
6
-
- Abstraction
7
-
- Extensibility
8
-
- Decoupling
5
+
tag:
6
+
- Abstraction
7
+
- Extensibility
8
+
- Decoupling
9
9
---
10
10
11
11
## Intent
12
12
13
-
The Abstract Document design pattern is a structural design pattern that aims to provide a consistent way to handle hierarchical and tree-like data structures by defining a common interface for various document types. It separates the core document structure from specific data formats, enabling dynamic updates and simplified maintenance.
13
+
The Abstract Document design pattern is a structural design pattern that aims to provide a consistent way to handle
14
+
hierarchical and tree-like data structures by defining a common interface for various document types. It separates the
15
+
core document structure from specific data formats, enabling dynamic updates and simplified maintenance.
14
16
15
17
## Explanation
16
18
17
-
The Abstract Document pattern enables handling additional, non-static properties. This pattern uses concept of traits to enable type safety and separate properties of different classes into set of interfaces.
19
+
The Abstract Document pattern enables handling additional, non-static properties. This pattern uses concept of traits to
20
+
enable type safety and separate properties of different classes into set of interfaces.
18
21
19
22
Real world example
20
23
21
-
> Consider a car that consists of multiple parts. However, we don't know if the specific car really has all the parts, or just some of them. Our cars are dynamic and extremely flexible.
24
+
> Consider a car that consists of multiple parts. However, we don't know if the specific car really has all the parts,
25
+
> or just some of them. Our cars are dynamic and extremely flexible.
22
26
23
27
In plain words
24
28
25
29
> Abstract Document pattern allows attaching properties to objects without them knowing about it.
26
30
27
31
Wikipedia says
28
32
29
-
> 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.
33
+
> An object-oriented structural design pattern for organizing objects in loosely typed key-value stores and exposing the
34
+
> data using typed views. The purpose of the pattern is to achieve a high degree of flexibility between components in a
35
+
> strongly typed language where new properties can be added to the object-tree on the fly, without losing the support of
36
+
> type-safety. The pattern makes use of traits to separate different properties of a class into different interfaces.
30
37
31
38
**Programmatic Example**
32
39
33
-
Let's first define the base classes `Document` and `AbstractDocument`. They basically make the object hold a property map and any amount of child objects.
40
+
Let's first define the base classes `Document` and `AbstractDocument`. They basically make the object hold a property
41
+
map and any amount of child objects.
34
42
35
43
```java
36
44
publicinterfaceDocument {
@@ -75,7 +83,9 @@ public abstract class AbstractDocument implements Document {
75
83
...
76
84
}
77
85
```
78
-
Next we define an enum `Property` and a set of interfaces for type, price, model and parts. This allows us to create static looking interface to our `Car` class.
86
+
87
+
Next we define an enum `Property` and a set of interfaces for type, price, model and parts. This allows us to create
88
+
static looking interface to our `Car` class.
79
89
80
90
```java
81
91
publicenumProperty {
@@ -96,6 +106,7 @@ public interface HasPrice extends Document {
@@ -169,21 +180,38 @@ And finally here's how we construct and use the `Car` in a full example.
169
180
170
181
## Applicability
171
182
172
-
This pattern is particularly useful in scenarios where you have different types of documents that share some common attributes or behaviors, but also have unique attributes or behaviors specific to their individual types. Here are some scenarios where the Abstract Document design pattern can be applicable:
183
+
This pattern is particularly useful in scenarios where you have different types of documents that share some common
184
+
attributes or behaviors, but also have unique attributes or behaviors specific to their individual types. Here are some
185
+
scenarios where the Abstract Document design pattern can be applicable:
173
186
174
-
* Content Management Systems (CMS): In a CMS, you might have various types of content such as articles, images, videos, etc. Each type of content could have shared attributes like creation date, author, and tags, while also having specific attributes like image dimensions for images or video duration for videos.
187
+
* Content Management Systems (CMS): In a CMS, you might have various types of content such as articles, images, videos,
188
+
etc. Each type of content could have shared attributes like creation date, author, and tags, while also having
189
+
specific attributes like image dimensions for images or video duration for videos.
175
190
176
-
* File Systems: If you're designing a file system where different types of files need to be managed, such as documents, images, audio files, and directories, the AbstractDocument pattern can help provide a consistent way to access attributes like file size, creation date, etc., while allowing for specific attributes like image resolution or audio duration.
191
+
* File Systems: If you're designing a file system where different types of files need to be managed, such as documents,
192
+
images, audio files, and directories, the AbstractDocument pattern can help provide a consistent way to access
193
+
attributes like file size, creation date, etc., while allowing for specific attributes like image resolution or audio
194
+
duration.
177
195
178
-
*E-commerce Systems:An e-commerce platform might have different product types such as physical products, digital downloads, and subscriptions. Each type could share common attributes like name, price, and description, while having unique attributes like shipping weight for physical products or download link for digital products.
196
+
*E-commerce Systems:An e-commerce platform might have different product types such as physical products, digital
197
+
downloads, and subscriptions. Each type could share common attributes like name, price, and description, while having
198
+
unique attributes like shipping weight for physical products or download link for digital products.
179
199
180
-
*MedicalRecordsSystems:In healthcare, patient records might include various types of data such as demographics, medical history, test results, and prescriptions. TheAbstractDocument pattern can help manage shared attributes like patient ID and date of birth, while accommodating specialized attributes like test results or prescribed medications.
200
+
*MedicalRecordsSystems:In healthcare, patient records might include various types of data such as demographics,
201
+
medical history, test results, and prescriptions. TheAbstractDocument pattern can help manage shared attributes like
202
+
patient ID and date of birth, while accommodating specialized attributes like test results or prescribed medications.
181
203
182
-
*ConfigurationManagement:When dealing with configuration settings for software applications, there can be different types of configuration elements, each with its own set of attributes. TheAbstractDocument pattern can be used to manage these configuration elements while ensuring a consistent way to access and manipulate their attributes.
204
+
*ConfigurationManagement:When dealing with configuration settings for software applications, there can be different
205
+
types of configuration elements, each with its own set of attributes. TheAbstractDocument pattern can be used to
206
+
manage these configuration elements while ensuring a consistent way to access and manipulate their attributes.
183
207
184
-
*EducationalPlatforms:Educational systems might have various types of learning materials such as text-based content, videos, quizzes, and assignments. Common attributes like title, author, and publication date can be shared, while unique attributes like video duration or assignment due dates can be specific to each type.
208
+
*EducationalPlatforms:Educational systems might have various types of learning materials such as text-based content,
209
+
videos, quizzes, and assignments. Common attributes like title, author, and publication date can be shared, while
210
+
unique attributes like video duration or assignment due dates can be specific to each type.
185
211
186
-
*ProjectManagementTools:In project management applications, you could have different types of tasks like to-do items, milestones, and issues. TheAbstractDocument pattern could be used to handle general attributes like task name and assignee, while allowing for specific attributes like milestone date or issue priority.
212
+
*ProjectManagementTools:In project management applications, you could have different types of tasks like to-do
213
+
items, milestones, and issues. TheAbstractDocument pattern could be used to handle general attributes like task name
214
+
and assignee, while allowing for specific attributes like milestone date or issue priority.
187
215
188
216
*Documents have diverse and evolving attribute structures.
189
217
@@ -193,7 +221,10 @@ This pattern is particularly useful in scenarios where you have different types
193
221
194
222
* Maintainability and flexibility are critical for the codebase.
195
223
196
-
The key idea behind the Abstract Document design pattern is to provide a flexible and extensible way to manage different types of documents or entities with shared and distinct attributes. By defining a common interface and implementing it across various document types, you can achieve a more organized and consistent approach to handling complex data structures.
224
+
The key idea behind the Abstract Document design pattern is to provide a flexible and extensible way to manage different
225
+
types of documents or entities with shared and distinct attributes. By defining a common interface and implementing it
226
+
across various document types, you can achieve a more organized and consistent approach to handling complex data
0 commit comments