<p>It is one if the <a href="https://dineshonjava.com/core-j2ee-patterns-best-design-practices/"><strong>Java EE software-design patterns</strong></a>. The <strong>composite entity pattern</strong> 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.</p>
<h2>Composite Entity Pattern</h2>
<p>The composite entity pattern uses various components for implementation. Each of the components has certain tasks to perform and certain problems to cater.</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px 16px 16px 16px;">
<h2 dir="ltr">Spring 5 Design Pattern Book</h2>
<div dir="ltr" style="color: #20124d; font-family: 'calibri' , sans-serif; font-size: 13pt; text-align: justify;">You could purchase my <strong>Spring 5 book</strong> that is with title name &#8220;<strong>Spring 5 Design Patterns</strong>&#8220;. This book is available on the <a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?ie=UTF8&;qid=1507925340&;sr=8-1&;keywords=spring+5+design+pattern"><strong>Amazon</strong></a> and <a href="https://www.packtpub.com/application-development/spring-5-design-patterns"><strong>Packt</strong></a> publisher website. Learn various <strong>design patterns</strong> and <strong>best practices</strong> in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- &#8220;<strong>AUTHDIS40</strong>&#8220;.</div>
<div dir="ltr"></div>
<div dir="ltr"><a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?s=books&;ie=UTF8&;qid=1512607966&;sr=1-1&;keywords=dinesh+rajput"><br />
<img class="aligncenter wp-image-3053 size-medium" title="Spring-5-Design-Pattern" src="https://i0.wp.com/dineshonjava.com/wp-content/uploads/2013/03/Spring-5-design-pattern.jpg?resize=243%2C300&;ssl=1" alt="Spring-5-Design-Pattern" width="243" height="300" /></a></div>
</div>
<h2>UML Class Diagram</h2>
<p>Let&#8217;s see the following UML class diagram.</p>
<p><img class="aligncenter size-full wp-image-4063" src="https://dineshonjava.com/wp-content/uploads/2018/01/Composite-Entity.gif" alt="Composite Entity" width="702" height="267" /><br />
These components include;</p>
<ul>
<li>Composite Entity</li>
<li>Coarse-grained Object</li>
<li>A dependent Object DependentObject1</li>
<li>DependentObject 2</li>
<li>DependentObject 3</li>
</ul>
<p><strong>The composite entity</strong> 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.</p>
<p><strong>A coarse grain object</strong> 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.</p>
<div style="text-align: left;">
<blockquote>
<p><strong>also read:</strong></p>
<ul style="text-align: left;">
<li><a title="https://dineshonjava.com/business-delegate/" href="https://dineshonjava.com/business-delegate/">Business Delegate Pattern</a></li>
<li><a title="https://dineshonjava.com/service-locator/" href="https://dineshonjava.com/service-locator/">Service Locator Pattern</a></li>
<li><a title="https://dineshonjava.com/session-facade/" href="https://dineshonjava.com/session-facade/">Session Facade Pattern</a></li>
<li><a title="https://dineshonjava.com/business-object/" href="https://dineshonjava.com/business-object/">Business Object Pattern</a></li>
<li><a title="https://dineshonjava.com/transfer-object/" href="https://dineshonjava.com/transfer-object/">Transfer Object Pattern</a></li>
</ul>
</blockquote>
</div>
<p><strong>A dependent object</strong> 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.</p>
<p>There are certain <strong>strategies</strong> 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.</p>
<h2>Sample Implementation of the Composite Entity Pattern</h2>
<p>Let&#8217;s see the following implementation of the Composite Entity Pattern.</p>
<h3>Step 1: Let&#8217;s create Dependent Objects.</h3>
<p><strong>DependentObject1.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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; 
	} 
} 
 
</pre>
<p><strong>DependentObject2.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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; 
	} 
} 
 
</pre>
<p><strong>DependentObject3.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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; 
	} 
} 
 
</pre>
<h3>Step 2: Let&#8217;s create Coarse Grained Object.</h3>
<p><strong>CoarseGrainedObject.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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()}; 
	 } 
} 
 
</pre>
<h3>Step 3: Let&#8217;s create Composite Entity.</h3>
<p><strong>CompositeEntity.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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(); 
	} 
} 
 
</pre>
<h3>Step 4: Let&#8217;s create Client class to use Composite Entity.</h3>
<p><strong>Client.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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); 
	} 
} 
 
</pre>
<h3>Step 5: Let&#8217;s create a demo class and use the Client to demonstrate Composite Entity design pattern usage.</h3>
<p><strong>CompositeEntityPatternDemo.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
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(); 
	} 
 
} 
 
</pre>
<h3>Step 6: Let&#8217;s run this demo class and verify the output.</h3>
<pre class="highlight">Data: Dinesh 
Data: Arnav 
Data: Anamika 
Data: Adesh 
Data: Vinesh 
Data: Akrati 
 
</pre>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/business-object/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/transfer-object/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '4062'});
});
</script>
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…