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
+15-44
Original file line number
Diff line number
Diff line change
@@ -10,35 +10,27 @@ tag:
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
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.
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.
16
14
17
15
## Explanation
18
16
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.
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.
21
18
22
19
Real world example
23
20
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.
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
22
27
23
In plain words
28
24
29
25
> Abstract Document pattern allows attaching properties to objects without them knowing about it.
30
26
31
27
Wikipedia says
32
28
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.
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.
37
30
38
31
**Programmatic Example**
39
32
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.
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.
42
34
43
35
```java
44
36
publicinterfaceDocument {
@@ -84,8 +76,7 @@ public abstract class AbstractDocument implements Document {
84
76
}
85
77
```
86
78
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
+
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.
89
80
90
81
```java
91
82
publicenumProperty {
@@ -180,38 +171,21 @@ And finally here's how we construct and use the `Car` in a full example.
180
171
181
172
## Applicability
182
173
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:
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:
186
175
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.
176
+
* 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.
190
177
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.
178
+
* 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.
195
179
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.
180
+
*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.
199
181
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.
182
+
*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.
203
183
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.
184
+
*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.
207
185
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.
186
+
*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.
211
187
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.
188
+
*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.
215
189
216
190
*Documents have diverse and evolving attribute structures.
217
191
@@ -221,10 +195,7 @@ scenarios where the Abstract Document design pattern can be applicable:
221
195
222
196
* Maintainability and flexibility are critical for the codebase.
223
197
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
227
-
structures.
198
+
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.
Copy file name to clipboardExpand all lines: abstract-factory/README.md
+11-24
Original file line number
Diff line number
Diff line change
@@ -14,32 +14,25 @@ Kit
14
14
15
15
## Intent
16
16
17
-
The Abstract Factory design pattern provides a way to create families of related objects without specifying their
18
-
concrete classes. This allows for code that is independent of the specific classes of objects it uses, promoting
19
-
flexibility and maintainability.
17
+
The Abstract Factory design pattern provides a way to create families of related objects without specifying their concrete classes. This allows for code that is independent of the specific classes of objects it uses, promoting flexibility and maintainability.
20
18
21
19
## Explanation
22
20
23
21
Real-world example
24
22
25
-
> To create a kingdom we need objects with a common theme. The elven kingdom needs an elven king, elven castle, and
26
-
> elven army whereas the orcish kingdom needs an orcish king, orcish castle, and orcish army. There is a dependency
27
-
> between the objects in the kingdom.
23
+
> To create a kingdom we need objects with a common theme. The elven kingdom needs an elven king, elven castle, and elven army whereas the orcish kingdom needs an orcish king, orcish castle, and orcish army. There is a dependency between the objects in the kingdom.
28
24
29
25
In plain words
30
26
31
-
> A factory of factories; a factory that groups the individual but related/dependent factories together without
32
-
> specifying their concrete classes.
27
+
> A factory of factories; a factory that groups the individual but related/dependent factories together without specifying their concrete classes.
33
28
34
29
Wikipedia says
35
30
36
-
> The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme
37
-
> without specifying their concrete classes
31
+
> The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes
38
32
39
33
**Programmatic Example**
40
34
41
-
Translating the kingdom example above. First of all, we have some interfaces and implementation for the objects in the
42
-
kingdom.
35
+
Translating the kingdom example above. First of all, we have some interfaces and implementation for the objects in the kingdom.
43
36
44
37
```java
45
38
publicinterfaceCastle {
@@ -134,8 +127,7 @@ public class OrcKingdomFactory implements KingdomFactory {
134
127
}
135
128
```
136
129
137
-
Now we have the abstract factory that lets us make a family of related objects i.e. elven kingdom factory creates elven
138
-
castle, king and army, etc.
130
+
Now we have the abstract factory that lets us make a family of related objects i.e. elven kingdom factory creates elven castle, king and army, etc.
139
131
140
132
```java
141
133
var factory=newElfKingdomFactory();
@@ -156,11 +148,7 @@ This is the elven castle!
156
148
This is the elven Army!
157
149
```
158
150
159
-
Now, we can design a factory for our different kingdom factories. In this example, we created `FactoryMaker`,
160
-
responsible for returning an instance of either `ElfKingdomFactory` or `OrcKingdomFactory`. The client can
161
-
use `FactoryMaker` to create the desired concrete factory which, in turn, will produce different concrete objects (
162
-
derived from `Army`, `King`, `Castle`). In this example, we also used an enum to parameterize which type of kingdom
163
-
factory the client will ask for.
151
+
Now, we can design a factory for our different kingdom factories. In this example, we created `FactoryMaker`, responsible for returning an instance of either `ElfKingdomFactory` or `OrcKingdomFactory`. The client can use `FactoryMaker` to create the desired concrete factory which, in turn, will produce different concrete objects (derived from `Army`, `King`, `Castle`). In this example, we also used an enum to parameterize which type of kingdom factory the client will ask for.
164
152
165
153
```java
166
154
publicstaticclassFactoryMaker {
@@ -203,8 +191,7 @@ Use the Abstract Factory pattern when
203
191
* The system should be independent of how its products are created, composed, and represented
204
192
* The system should be configured with one of the multiple families of products
205
193
* The family of related product objects is designed to be used together, and you need to enforce this constraint
206
-
* You want to provide a class library of products, and you want to reveal just their interfaces, not their
207
-
implementations
194
+
* You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations
208
195
* The lifetime of the dependency is conceptually shorter than the lifetime of the consumer.
209
196
* You need a run-time value to construct a particular dependency
210
197
* You want to decide which product to call from a family at runtime.
@@ -214,8 +201,7 @@ Use the Abstract Factory pattern when
214
201
215
202
Example use cases
216
203
217
-
* Selecting to call to the appropriate implementation of FileSystemAcmeService or DatabaseAcmeService or
218
-
NetworkAcmeService at runtime.
204
+
* Selecting to call to the appropriate implementation of FileSystemAcmeService or DatabaseAcmeService or NetworkAcmeService at runtime.
219
205
* Unit test case writing becomes much easier
220
206
* UI tools for different OS
221
207
@@ -258,4 +244,5 @@ Trade-offs
258
244
*[Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
259
245
*[Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
260
246
*[Java Design Patterns: A Hands-On Experience with Real-World Examples](https://amzn.to/3HWNf4U)
261
-
*[Design Patterns in Java](https://amzn.to/3Syw0vC)
247
+
*[Design Patterns in Java](https://amzn.to/3Syw0vC)
Copy file name to clipboardExpand all lines: active-object/README.md
+6-13
Original file line number
Diff line number
Diff line change
@@ -8,22 +8,17 @@ tag:
8
8
9
9
## Intent
10
10
11
-
The Active Object design pattern provides a safe and reliable way to implement asynchronous behavior in concurrent
12
-
systems. It achieves this by encapsulating tasks within objects that have their own thread and message queue. This
13
-
separation keeps the main thread responsive and avoids issues like direct thread manipulation or shared state access.
11
+
The Active Object design pattern provides a safe and reliable way to implement asynchronous behavior in concurrent systems. It achieves this by encapsulating tasks within objects that have their own thread and message queue. This separation keeps the main thread responsive and avoids issues like direct thread manipulation or shared state access.
14
12
15
13
## Explanation
16
14
17
-
The class that implements the active object pattern will contain a self-synchronization mechanism without using '
18
-
synchronized' methods.
15
+
The class that implements the active object pattern will contain a self-synchronization mechanism without using 'synchronized' methods.
19
16
20
17
Real-world example
21
18
22
-
> The Orcs are known for their wildness and untameable soul. It seems like they have their own thread of control based
23
-
> on previous behavior.
19
+
> The Orcs are known for their wildness and untameable soul. It seems like they have their own thread of control based on previous behavior.
24
20
25
-
To implement a creature that has its own thread of control mechanism and expose its API only and not the execution
26
-
itself, we can use the Active Object pattern.
21
+
To implement a creature that has its own thread of control mechanism and expose its API only and not the execution itself, we can use the Active Object pattern.
27
22
28
23
**Programmatic Example**
29
24
@@ -83,8 +78,7 @@ public abstract class ActiveCreature {
83
78
}
84
79
```
85
80
86
-
We can see that any class that will extend the ActiveCreature class will have its own thread of control to invoke and
87
-
execute methods.
81
+
We can see that any class that will extend the ActiveCreature class will have its own thread of control to invoke and execute methods.
88
82
89
83
For example, the Orc class:
90
84
@@ -98,8 +92,7 @@ public class Orc extends ActiveCreature {
98
92
}
99
93
```
100
94
101
-
Now, we can create multiple creatures such as Orcs, tell them to eat and roam, and they will execute it on their own
102
-
thread of control:
95
+
Now, we can create multiple creatures such as Orcs, tell them to eat and roam, and they will execute it on their own thread of control:
0 commit comments