It is one if the Java EE software-design patterns. The composite entity pattern performs modeling, managing and representing a set of interrelated persistent objects. It does not represent them as separate fine-grained entity beans. Composite entity beans are able to represent a graph of objects.
The composite entity pattern uses various components for implementation. Each of the components has certain tasks to perform and certain problems to cater.
Let’s see the following UML class diagram.
These components include;
The composite entity is in fact, an entity bean that is coarse grained. It can either be an object of coarse-grained or it can hold the reference to a certain objects that are coarse-grained.
A coarse grain object is responsible for handling it sown life cycle and managing its relationships with other objects on its own. A coarse-grained object is a Java object that a composite entity contains. Either this or the composite entity can be the coarse-grained object itself which is responsible for holding the dependent objects.
also read:
A dependent object is defined as an object which depends on the coarse-grained objects. These objects have their life cycles managed by the coarse-grained objects. A dependent object is capable of containing other dependent objects which may result in a tree form of objects contained inside the composite entity.
There are certain strategies to implement the composite entity design pattern. The ‘composite entity contains coarse-grained object strategy’ implements that the composite entity is responsible for holding the coarse-grained objects while the coarse-grained objects continue their relationships with the respective dependent objects. This is also the main strategy of all strategies.
Let’s see the following implementation of the Composite Entity Pattern.
DependentObject1.java
/** * */ package com.doj.patterns.j2ee.compositeentity; /** * @author Dinesh.Rajput * */ public class DependentObject1 { private String data; public void setData(String data){ this.data = data; } public String getData(){ return data; } }
DependentObject2.java
/** * */ package com.doj.patterns.j2ee.compositeentity; /** * @author Dinesh.Rajput * */ public class DependentObject2 { private String data; public void setData(String data){ this.data = data; } public String getData(){ return data; } }
DependentObject3.java
/** * */ package com.doj.patterns.j2ee.compositeentity; /** * @author Dinesh.Rajput * */ public class DependentObject3 { private String data; public void setData(String data){ this.data = data; } public String getData(){ return data; } }
CoarseGrainedObject.java
/** * */ package com.doj.patterns.j2ee.compositeentity; /** * @author Dinesh.Rajput * */ public class CoarseGrainedObject { DependentObject1 do1 = new DependentObject1(); DependentObject2 do2 = new DependentObject2(); DependentObject3 do3 = new DependentObject3(); public void setData(String data1, String data2, String data3){ do1.setData(data1); do2.setData(data2); do3.setData(data3); } public String[] getData(){ return new String[] {do1.getData(),do2.getData(),do3.getData()}; } }
CompositeEntity.java
/** * */ package com.doj.patterns.j2ee.compositeentity; /** * @author Dinesh.Rajput * */ public class CompositeEntity { private CoarseGrainedObject cgo = new CoarseGrainedObject(); public void setData(String data1, String data2, String data3){ cgo.setData(data1, data2, data3); } public String[] getData(){ return cgo.getData(); } }
Client.java
/** * */ package com.doj.patterns.j2ee.compositeentity; /** * @author Dinesh.Rajput * */ public class Client { private CompositeEntity compositeEntity = new CompositeEntity(); public void printData(){ for (int i = 0; i < compositeEntity.getData().length; i++) { System.out.println("Data: " + compositeEntity.getData()[i]); } } public void setData(String data1, String data2, String data3){ compositeEntity.setData(data1, data2, data3); } }
CompositeEntityPatternDemo.java
/** * */ package com.doj.patterns.j2ee.compositeentity; /** * @author Dinesh.Rajput * */ public class CompositeEntityPatternDemo { /** * @param args */ public static void main(String[] args) { Client client = new Client(); client.setData("Dinesh", "Arnav", "Anamika"); client.printData(); client.setData("Adesh", "Vinesh", "Akrati"); client.printData(); } }
Data: Dinesh Data: Arnav Data: Anamika Data: Adesh Data: Vinesh Data: Akrati
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…