Skip to content

Commit fd10995

Browse files
committed
docs: composite entity
1 parent e00e67c commit fd10995

File tree

3 files changed

+95
-54
lines changed

3 files changed

+95
-54
lines changed

composite-entity/README.md

Lines changed: 89 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,62 @@ title: Composite Entity
33
category: Structural
44
language: en
55
tag:
6-
- Enterprise Integration Pattern
6+
- Client-server
7+
- Data access
8+
- Enterprise patterns
79
---
810

11+
## Also known as
12+
13+
* Coarse-Grained Entity
14+
915
## Intent
1016

11-
It is used to model, represent, and manage a set of persistent objects that are interrelated, rather than representing them as individual fine-grained entities.
17+
The Composite Entity design pattern is aimed at managing a set of interrelated persistent objects as if they were a
18+
single entity. It is commonly used in the context of Enterprise JavaBeans (EJB) and similar enterprise frameworks to
19+
represent graph-based data structures within business models, enabling clients to treat them as a single unit.
1220

1321
## Explanation
1422

1523
Real world example
1624

17-
> For a console, there may be many interfaces that need to be managed and controlled. Using the composite entity pattern, dependent objects such as messages and signals can be combined together and controlled using a single object.
25+
> For a console, there may be many interfaces that need to be managed and controlled. Using the composite entity
26+
> pattern, dependent objects such as messages and signals can be combined and controlled using a single object.
1827
1928
In plain words
2029

2130
> Composite entity pattern allows a set of related objects to be represented and managed by a unified object.
2231
2332
**Programmatic Example**
2433

25-
We need a generic solution for the problem. To achieve this, let's introduce a generic
34+
We need a generic solution for the problem. To achieve this, let's introduce a generic
2635
Composite Entity Pattern.
2736

2837
```java
2938
public abstract class DependentObject<T> {
3039

31-
T data;
40+
T data;
3241

33-
public void setData(T message) {
34-
this.data = message;
35-
}
42+
public void setData(T message) {
43+
this.data = message;
44+
}
3645

37-
public T getData() {
38-
return data;
39-
}
46+
public T getData() {
47+
return data;
48+
}
4049
}
4150

4251
public abstract class CoarseGrainedObject<T> {
4352

44-
DependentObject<T>[] dependentObjects;
53+
DependentObject<T>[] dependentObjects;
4554

46-
public void setData(T... data) {
47-
IntStream.range(0, data.length).forEach(i -> dependentObjects[i].setData(data[i]));
48-
}
55+
public void setData(T... data) {
56+
IntStream.range(0, data.length).forEach(i -> dependentObjects[i].setData(data[i]));
57+
}
4958

50-
public T[] getData() {
51-
return (T[]) Arrays.stream(dependentObjects).map(DependentObject::getData).toArray();
52-
}
59+
public T[] getData() {
60+
return (T[]) Arrays.stream(dependentObjects).map(DependentObject::getData).toArray();
61+
}
5362
}
5463

5564
```
@@ -67,43 +76,43 @@ public class SignalDependentObject extends DependentObject<String> {
6776

6877
public class ConsoleCoarseGrainedObject extends CoarseGrainedObject<String> {
6978

70-
@Override
71-
public String[] getData() {
72-
super.getData();
73-
return new String[]{
74-
dependentObjects[0].getData(), dependentObjects[1].getData()
75-
};
76-
}
77-
78-
public void init() {
79-
dependentObjects = new DependentObject[]{
80-
new MessageDependentObject(), new SignalDependentObject()};
81-
}
79+
@Override
80+
public String[] getData() {
81+
super.getData();
82+
return new String[] {
83+
dependentObjects[0].getData(), dependentObjects[1].getData()
84+
};
85+
}
86+
87+
public void init() {
88+
dependentObjects = new DependentObject[] {
89+
new MessageDependentObject(), new SignalDependentObject()};
90+
}
8291
}
8392

8493
public class CompositeEntity {
8594

86-
private final ConsoleCoarseGrainedObject console = new ConsoleCoarseGrainedObject();
95+
private final ConsoleCoarseGrainedObject console = new ConsoleCoarseGrainedObject();
8796

88-
public void setData(String message, String signal) {
89-
console.setData(message, signal);
90-
}
97+
public void setData(String message, String signal) {
98+
console.setData(message, signal);
99+
}
91100

92-
public String[] getData() {
93-
return console.getData();
94-
}
101+
public String[] getData() {
102+
return console.getData();
103+
}
95104
}
96105
```
97106

98107
Now managing the assignment of message and signal objects with the composite entity `console`.
99108

100109
```java
101-
var console = new CompositeEntity();
102-
console.init();
103-
console.setData("No Danger", "Green Light");
104-
Arrays.stream(console.getData()).forEach(LOGGER::info);
105-
console.setData("Danger", "Red Light");
106-
Arrays.stream(console.getData()).forEach(LOGGER::info);
110+
var console=new CompositeEntity();
111+
console.init();
112+
console.setData("No Danger","Green Light");
113+
Arrays.stream(console.getData()).forEach(LOGGER::info);
114+
console.setData("Danger","Red Light");
115+
Arrays.stream(console.getData()).forEach(LOGGER::info);
107116
```
108117

109118
## Class diagram
@@ -112,9 +121,44 @@ Arrays.stream(console.getData()).forEach(LOGGER::info);
112121

113122
## Applicability
114123

115-
Use the Composite Entity Pattern in the following situation:
124+
* Useful in enterprise applications where business objects are complex and involve various interdependent objects.
125+
* Ideal for scenarios where clients need to work with a unified interface to a set of objects rather than individual
126+
entities.
127+
* Applicable in systems that require a simplified view of a complex data model for external clients or services.
128+
129+
## Known Uses
130+
131+
* Enterprise applications with complex business models, particularly those using EJB or similar enterprise frameworks.
132+
* Systems requiring abstraction over complex database schemas to simplify client interactions.
133+
* Applications that need to enforce consistency or transactions across multiple objects in a business entity.
134+
135+
## Consequences
136+
137+
Benefits:
138+
139+
* Simplifies client interactions with complex entity models by providing a unified interface.
140+
* Enhances reusability and maintainability of the business layer by decoupling client code from the complex internals of
141+
business entities.
142+
* Facilitates easier transaction management and consistency enforcement across a set of related objects.
143+
144+
Trade-offs:
145+
146+
* May introduce a level of indirection that could impact performance.
147+
* Can lead to overly coarse-grained interfaces that might not be as flexible for all client needs.
148+
* Requires careful design to avoid bloated composite entities that are difficult to manage.
149+
150+
## Related Patterns
151+
152+
* [Decorator](https://java-design-patterns.com/patterns/decorator/): For dynamically adding behavior to individual
153+
objects within the composite entity without affecting the structure.
154+
* [Facade](https://java-design-patterns.com/patterns/facade/): Provides a simplified interface to a complex subsystem,
155+
similar to how a composite entity simplifies access to a set of objects.
156+
* [Flyweight](https://java-design-patterns.com/patterns/flyweight/): Useful for managing shared objects within a
157+
composite entity to reduce memory footprint.
116158

117-
* You want to manage multiple dependency objects through one object to adjust the degree of granularity between objects. At the same time, the lifetime of dependency objects depends on a coarse-grained object.
118159
## Credits
119160

120161
* [Composite Entity Pattern in wikipedia](https://en.wikipedia.org/wiki/Composite_entity_pattern)
162+
* [Core J2EE Patterns: Best Practices and Design Strategies](https://amzn.to/4cAbDap)
163+
* [Enterprise Patterns and MDA: Building Better Software with Archetype Patterns and UML](https://amzn.to/49mslqS)
164+
* [Patterns of Enterprise Application Architecture](https://amzn.to/3xjKdpe)

composite-entity/src/main/java/com/iluwatar/compositeentity/DependentObject.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,18 @@
2424
*/
2525
package com.iluwatar.compositeentity;
2626

27+
import lombok.Getter;
28+
import lombok.Setter;
29+
2730
/**
2831
* It is an object, which can contain other dependent objects (there may be a tree of objects within
2932
* the composite entity), that depends on the coarse-grained object and has its life cycle managed
3033
* by the coarse-grained object.
3134
*/
32-
35+
@Setter
36+
@Getter
3337
public abstract class DependentObject<T> {
3438

3539
T data;
3640

37-
public void setData(T message) {
38-
this.data = message;
39-
}
40-
41-
public T getData() {
42-
return data;
43-
}
4441
}

composite-view/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Composite View
33
category: Structural
44
language: en
55
tag:
6-
- Enterprise Integration Pattern
6+
- Enterprise patterns
77
- Presentation
88
---
99

0 commit comments

Comments
 (0)