Skip to content

Commit 1d02470

Browse files
committed
2 parents 773832a + aa2b190 commit 1d02470

File tree

901 files changed

+19477
-20013
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

901 files changed

+19477
-20013
lines changed

.all-contributorsrc

+40-3
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,8 @@
15481548
"avatar_url": "https://avatars.githubusercontent.com/u/17254162?v=4",
15491549
"profile": "https://www.linkedin.com/in/souzasamuel/",
15501550
"contributions": [
1551-
"code"
1551+
"code",
1552+
"doc"
15521553
]
15531554
},
15541555
{
@@ -2546,7 +2547,7 @@
25462547
"login": "tiennm99",
25472548
"name": "Tien Nguyen Minh",
25482549
"avatar_url": "https://avatars.githubusercontent.com/u/39063457?v=4",
2549-
"profile": "http://miti99.dev",
2550+
"profile": "https://github.com/tiennm99",
25502551
"contributions": [
25512552
"code",
25522553
"translation"
@@ -3104,9 +3105,45 @@
31043105
"contributions": [
31053106
"code"
31063107
]
3108+
},
3109+
{
3110+
"login": "Adelechka",
3111+
"name": "Adelya",
3112+
"avatar_url": "https://avatars.githubusercontent.com/u/65678470?v=4",
3113+
"profile": "https://github.com/Adelechka",
3114+
"contributions": [
3115+
"code"
3116+
]
3117+
},
3118+
{
3119+
"login": "gatlanagaprasanna",
3120+
"name": "gatlanagaprasanna",
3121+
"avatar_url": "https://avatars.githubusercontent.com/u/154739216?v=4",
3122+
"profile": "https://github.com/gatlanagaprasanna",
3123+
"contributions": [
3124+
"doc"
3125+
]
3126+
},
3127+
{
3128+
"login": "Avinash2110",
3129+
"name": "Avinash Shukla",
3130+
"avatar_url": "https://avatars.githubusercontent.com/u/37360069?v=4",
3131+
"profile": "https://github.com/Avinash2110",
3132+
"contributions": [
3133+
"code"
3134+
]
3135+
},
3136+
{
3137+
"login": "Mayankchoudhary294",
3138+
"name": "Mayank Choudhary",
3139+
"avatar_url": "https://avatars.githubusercontent.com/u/97609699?v=4",
3140+
"profile": "https://github.com/Mayankchoudhary294",
3141+
"contributions": [
3142+
"code"
3143+
]
31073144
}
31083145
],
3109-
"contributorsPerLine": 7,
3146+
"contributorsPerLine": 6,
31103147
"projectName": "java-design-patterns",
31113148
"projectOwner": "iluwatar",
31123149
"repoType": "github",

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,5 @@ build/
5757
#################### Java Design Patterns #######
5858
etc/Java Design Patterns.urm.puml
5959
serialized-entity/output.txt
60+
fish1.out
61+
fish2.out

README.md

+478-471
Large diffs are not rendered by default.

abstract-document/README.md

+73-56
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
---
2-
title: Abstract Document
2+
title: "Abstract Document Pattern in Java: Simplifying Data Handling with Flexibility"
3+
shortTitle: Abstract Document
4+
description: "Explore the Abstract Document design pattern in Java. Learn its intent, explanation, applicability, benefits, and see real-world examples to implement flexible and dynamic data structures."
35
category: Structural
46
language: en
57
tag:
6-
- Abstraction
7-
- Extensibility
8-
- Decoupling
8+
- Abstraction
9+
- Decoupling
10+
- Dynamic typing
11+
- Encapsulation
12+
- Extensibility
13+
- Polymorphism
914
---
1015

11-
## Intent
16+
## Intent of Abstract Document Design Pattern
1217

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.
18+
The Abstract Document design pattern in Java is a crucial structural design pattern that provides 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.
1419

15-
## Explanation
20+
## Detailed Explanation of Abstract Document Pattern with Real-World Examples
1621

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.
22+
The Abstract Document design pattern in Java allows dynamic handling of non-static properties. This pattern uses concept of traits to enable type safety and separate properties of different classes into set of interfaces.
1823

19-
Real world example
24+
Real-world example
2025

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.
26+
> Consider a library system implementing the Abstract Document design pattern in Java, where books can have diverse formats and attributes: physical books, eBooks, and audiobooks. Each format has unique properties, such as page count for physical books, file size for eBooks, and duration for audiobooks. The Abstract Document design pattern allows the library system to manage these diverse formats flexibly. By using this pattern, the system can store and retrieve properties dynamically, without needing a rigid structure for each book type, making it easier to add new formats or attributes in the future without significant changes to the codebase.
2227
2328
In plain words
2429

@@ -28,7 +33,9 @@ Wikipedia says
2833

2934
> 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.
3035
31-
**Programmatic Example**
36+
## Programmatic Example of Abstract Document Pattern in Java
37+
38+
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.
3239

3340
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.
3441

@@ -72,7 +79,8 @@ public abstract class AbstractDocument implements Document {
7279
.flatMap(Collection::stream)
7380
.map(constructor);
7481
}
75-
...
82+
83+
// Other properties and methods...
7684
}
7785
```
7886

@@ -127,51 +135,57 @@ public class Car extends AbstractDocument implements HasModel, HasPrice, HasPart
127135
And finally here's how we construct and use the `Car` in a full example.
128136
129137
```java
138+
public static void main(String[] args) {
130139
LOGGER.info("Constructing parts and car");
131140
132-
var wheelProperties=Map.of(
133-
Property.TYPE.toString(),"wheel",
134-
Property.MODEL.toString(),"15C",
135-
Property.PRICE.toString(),100L);
136-
137-
var doorProperties=Map.of(
138-
Property.TYPE.toString(),"door",
139-
Property.MODEL.toString(),"Lambo",
140-
Property.PRICE.toString(),300L);
141-
142-
var carProperties=Map.of(
143-
Property.MODEL.toString(),"300SL",
144-
Property.PRICE.toString(),10000L,
145-
Property.PARTS.toString(),List.of(wheelProperties,doorProperties));
146-
147-
var car=new Car(carProperties);
148-
149-
LOGGER.info("Here is our car:");
150-
LOGGER.info("-> model: {}",car.getModel().orElseThrow());
151-
LOGGER.info("-> price: {}",car.getPrice().orElseThrow());
152-
LOGGER.info("-> parts: ");
153-
car.getParts().forEach(p->LOGGER.info("\t{}/{}/{}",
154-
p.getType().orElse(null),
155-
p.getModel().orElse(null),
156-
p.getPrice().orElse(null))
157-
);
158-
159-
// Constructing parts and car
160-
// Here is our car:
161-
// model: 300SL
162-
// price: 10000
163-
// parts:
164-
// wheel/15C/100
165-
// door/Lambo/300
141+
var wheelProperties = Map.of(
142+
Property.TYPE.toString(), "wheel",
143+
Property.MODEL.toString(), "15C",
144+
Property.PRICE.toString(), 100L);
145+
146+
var doorProperties = Map.of(
147+
Property.TYPE.toString(), "door",
148+
Property.MODEL.toString(), "Lambo",
149+
Property.PRICE.toString(), 300L);
150+
151+
var carProperties = Map.of(
152+
Property.MODEL.toString(), "300SL",
153+
Property.PRICE.toString(), 10000L,
154+
Property.PARTS.toString(), List.of(wheelProperties, doorProperties));
155+
156+
var car = new Car(carProperties);
157+
158+
LOGGER.info("Here is our car:");
159+
LOGGER.info("-> model: {}", car.getModel().orElseThrow());
160+
LOGGER.info("-> price: {}", car.getPrice().orElseThrow());
161+
LOGGER.info("-> parts: ");
162+
car.getParts().forEach(p -> LOGGER.info("\t{}/{}/{}",
163+
p.getType().orElse(null),
164+
p.getModel().orElse(null),
165+
p.getPrice().orElse(null))
166+
);
167+
}
168+
```
169+
170+
The program output:
171+
172+
```
173+
07:21:57.391 [main] INFO com.iluwatar.abstractdocument.App -- Constructing parts and car
174+
07:21:57.393 [main] INFO com.iluwatar.abstractdocument.App -- Here is our car:
175+
07:21:57.393 [main] INFO com.iluwatar.abstractdocument.App -- -> model: 300SL
176+
07:21:57.394 [main] INFO com.iluwatar.abstractdocument.App -- -> price: 10000
177+
07:21:57.394 [main] INFO com.iluwatar.abstractdocument.App -- -> parts:
178+
07:21:57.395 [main] INFO com.iluwatar.abstractdocument.App -- wheel/15C/100
179+
07:21:57.395 [main] INFO com.iluwatar.abstractdocument.App -- door/Lambo/300
166180
```
167181
168-
## Class diagram
182+
## Abstract Document Pattern Class Diagram
169183
170-
![alt text](./etc/abstract-document.png "Abstract Document Traits and Domain")
184+
![Abstract Document](./etc/abstract-document.png "Abstract Document Traits and Domain")
171185
172-
## Applicability
186+
## When to Use the Abstract Document Pattern in Java
173187
174-
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:
188+
The Abstract Document design pattern is especially beneficial in scenarios requiring management of different document types in Java 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:
175189
176190
* 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.
177191
@@ -197,9 +211,9 @@ This pattern is particularly useful in scenarios where you have different types
197211

198212
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.
199213

200-
## Consequences
214+
## Benefits and Trade-offs of Abstract Document Pattern
201215

202-
Benefits
216+
Benefits:
203217

204218
* Flexibility: Accommodates varied document structures and properties.
205219

@@ -209,14 +223,17 @@ Benefits
209223

210224
* Reusability: Typed views enable code reuse for accessing specific attribute types.
211225

212-
Trade-offs
226+
Trade-offs:
213227

214228
* Complexity: Requires defining interfaces and views, adding implementation overhead.
215229

216230
* Performance: Might introduce slight performance overhead compared to direct data access.
217231

218-
## Credits
232+
## References and Credits
219233

220-
* [Wikipedia: Abstract Document Pattern](https://en.wikipedia.org/wiki/Abstract_Document_Pattern)
221-
* [Martin Fowler: Dealing with properties](http://martinfowler.com/apsupp/properties.pdf)
234+
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
235+
* [Java Design Patterns: A Hands-On Experience with Real-World Examples](https://amzn.to/3yhh525)
222236
* [Pattern-Oriented Software Architecture Volume 4: A Pattern Language for Distributed Computing (v. 4)](https://amzn.to/49zRP4R)
237+
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
238+
* [Abstract Document Pattern (Wikipedia)](https://en.wikipedia.org/wiki/Abstract_Document_Pattern)
239+
* [Dealing with Properties (Martin Fowler)](http://martinfowler.com/apsupp/properties.pdf)

0 commit comments

Comments
 (0)