Design Pattern

Composite Entity Pattern – Core J2EE Patterns

<p>It is one if the <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-j2ee-patterns-best-design-practices&sol;"><strong>Java EE software-design patterns<&sol;strong><&sol;a>&period; The <strong>composite entity pattern<&sol;strong> performs modeling&comma; managing and representing a set of interrelated persistent objects&period; It does not represent them as separate fine-grained entity beans&period; Composite entity beans are able to represent a graph of objects&period;<&sol;p>&NewLine;<h2>Composite Entity Pattern<&sol;h2>&NewLine;<p>The composite entity pattern uses various components for implementation&period; Each of the components has certain tasks to perform and certain problems to cater&period;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px 16px 16px 16px&semi;">&NewLine;<h2 dir&equals;"ltr">Spring 5 Design Pattern Book<&sol;h2>&NewLine;<div dir&equals;"ltr" style&equals;"color&colon; &num;20124d&semi; font-family&colon; 'calibri' &comma; sans-serif&semi; font-size&colon; 13pt&semi; text-align&colon; justify&semi;">You could purchase my <strong>Spring 5 book<&sol;strong> that is with title name &&num;8220&semi;<strong>Spring 5 Design Patterns<&sol;strong>&&num;8220&semi;&period; This book is available on the <a href&equals;"https&colon;&sol;&sol;www&period;amazon&period;in&sol;Spring-Design-Patterns-Dinesh-Rajput&sol;dp&sol;1788299450&sol;ref&equals;sr&lowbar;1&lowbar;1&quest;ie&equals;UTF8&amp&semi;qid&equals;1507925340&amp&semi;sr&equals;8-1&amp&semi;keywords&equals;spring&plus;5&plus;design&plus;pattern"><strong>Amazon<&sol;strong><&sol;a> and <a href&equals;"https&colon;&sol;&sol;www&period;packtpub&period;com&sol;application-development&sol;spring-5-design-patterns"><strong>Packt<&sol;strong><&sol;a> publisher website&period; Learn various <strong>design patterns<&sol;strong> and <strong>best practices<&sol;strong> in Spring 5 and use them to solve common design problems&period; You could use author discount to purchase this book by using code- &&num;8220&semi;<strong>AUTHDIS40<&sol;strong>&&num;8220&semi;&period;<&sol;div>&NewLine;<div dir&equals;"ltr"><&sol;div>&NewLine;<div dir&equals;"ltr"><a href&equals;"https&colon;&sol;&sol;www&period;amazon&period;in&sol;Spring-Design-Patterns-Dinesh-Rajput&sol;dp&sol;1788299450&sol;ref&equals;sr&lowbar;1&lowbar;1&quest;s&equals;books&amp&semi;ie&equals;UTF8&amp&semi;qid&equals;1512607966&amp&semi;sr&equals;1-1&amp&semi;keywords&equals;dinesh&plus;rajput"><br &sol;>&NewLine;<img class&equals;"aligncenter wp-image-3053 size-medium" title&equals;"Spring-5-Design-Pattern" src&equals;"https&colon;&sol;&sol;i0&period;wp&period;com&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;03&sol;Spring-5-design-pattern&period;jpg&quest;resize&equals;243&percnt;2C300&amp&semi;ssl&equals;1" alt&equals;"Spring-5-Design-Pattern" width&equals;"243" height&equals;"300" &sol;><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;<h2>UML Class Diagram<&sol;h2>&NewLine;<p>Let&&num;8217&semi;s see the following UML class diagram&period;<&sol;p>&NewLine;<p><img class&equals;"aligncenter size-full wp-image-4063" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2018&sol;01&sol;Composite-Entity&period;gif" alt&equals;"Composite Entity" width&equals;"702" height&equals;"267" &sol;><br &sol;>&NewLine;These components include&semi;<&sol;p>&NewLine;<ul>&NewLine;<li>Composite Entity<&sol;li>&NewLine;<li>Coarse-grained Object<&sol;li>&NewLine;<li>A dependent Object DependentObject1<&sol;li>&NewLine;<li>DependentObject 2<&sol;li>&NewLine;<li>DependentObject 3<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong>The composite entity<&sol;strong> is in fact&comma; an entity bean that is coarse grained&period; It can either be an object of coarse-grained or it can hold the reference to a certain objects that are coarse-grained&period;<&sol;p>&NewLine;<p><strong>A coarse grain object<&sol;strong> is responsible for handling it sown life cycle and managing its relationships with other objects on its own&period; A coarse-grained object is a Java object that a composite entity contains&period; Either this or the composite entity can be the coarse-grained object itself which is responsible for holding the dependent objects&period;<&sol;p>&NewLine;<div style&equals;"text-align&colon; left&semi;">&NewLine;<blockquote>&NewLine;<p><strong>also read&colon;<&sol;strong><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;business-delegate&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;business-delegate&sol;">Business Delegate Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;service-locator&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;service-locator&sol;">Service Locator Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;session-facade&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;session-facade&sol;">Session Facade Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;business-object&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;business-object&sol;">Business Object Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;transfer-object&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;transfer-object&sol;">Transfer Object Pattern<&sol;a><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;blockquote>&NewLine;<&sol;div>&NewLine;<p><strong>A dependent object<&sol;strong> is defined as an object which depends on the coarse-grained objects&period; These objects have their life cycles managed by the coarse-grained objects&period; A dependent object is capable of containing other dependent objects which may result in a tree form of objects contained inside the composite entity&period;<&sol;p>&NewLine;<p>There are certain <strong>strategies<&sol;strong> to implement the composite entity design pattern&period; The &OpenCurlyQuote;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&period; This is also the main strategy of all strategies&period;<&sol;p>&NewLine;<h2>Sample Implementation of the Composite Entity Pattern<&sol;h2>&NewLine;<p>Let&&num;8217&semi;s see the following implementation of the Composite Entity Pattern&period;<&sol;p>&NewLine;<h3>Step 1&colon; Let&&num;8217&semi;s create Dependent Objects&period;<&sol;h3>&NewLine;<p><strong>DependentObject1&period;java<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;patterns&period;j2ee&period;compositeentity&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class DependentObject1 &lbrace; &NewLine;&Tab; &NewLine;&Tab;private String data&semi; &NewLine; &NewLine;&Tab;public void setData&lpar;String data&rpar;&lbrace; &NewLine;&Tab;&Tab;this&period;data &equals; data&semi; &NewLine;&Tab;&rcub; &NewLine; &NewLine;&Tab;public String getData&lpar;&rpar;&lbrace; &NewLine;&Tab;&Tab;return data&semi; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<p><strong>DependentObject2&period;java<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;patterns&period;j2ee&period;compositeentity&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class DependentObject2 &lbrace; &NewLine;&Tab; &NewLine;&Tab;private String data&semi; &NewLine; &NewLine;&Tab;public void setData&lpar;String data&rpar;&lbrace; &NewLine;&Tab;&Tab;this&period;data &equals; data&semi; &NewLine;&Tab;&rcub; &NewLine; &NewLine;&Tab;public String getData&lpar;&rpar;&lbrace; &NewLine;&Tab;&Tab;return data&semi; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<p><strong>DependentObject3&period;java<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;patterns&period;j2ee&period;compositeentity&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class DependentObject3 &lbrace; &NewLine;&Tab; &NewLine;&Tab;private String data&semi; &NewLine; &NewLine;&Tab;public void setData&lpar;String data&rpar;&lbrace; &NewLine;&Tab;&Tab;this&period;data &equals; data&semi; &NewLine;&Tab;&rcub; &NewLine; &NewLine;&Tab;public String getData&lpar;&rpar;&lbrace; &NewLine;&Tab;&Tab;return data&semi; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3>Step 2&colon; Let&&num;8217&semi;s create Coarse Grained Object&period;<&sol;h3>&NewLine;<p><strong>CoarseGrainedObject&period;java<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;patterns&period;j2ee&period;compositeentity&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class CoarseGrainedObject &lbrace; &NewLine;&Tab; &NewLine;&Tab; DependentObject1 do1 &equals; new DependentObject1&lpar;&rpar;&semi; &NewLine;&Tab; DependentObject2 do2 &equals; new DependentObject2&lpar;&rpar;&semi; &NewLine;&Tab; DependentObject3 do3 &equals; new DependentObject3&lpar;&rpar;&semi; &NewLine;&Tab; &NewLine;&Tab; public void setData&lpar;String data1&comma; String data2&comma; String data3&rpar;&lbrace; &NewLine;&Tab;&Tab; do1&period;setData&lpar;data1&rpar;&semi; &NewLine;&Tab;&Tab; do2&period;setData&lpar;data2&rpar;&semi; &NewLine;&Tab;&Tab; do3&period;setData&lpar;data3&rpar;&semi; &NewLine;&Tab; &rcub; &NewLine; &NewLine;&Tab; public String&lbrack;&rsqb; getData&lpar;&rpar;&lbrace; &NewLine;&Tab;&Tab; return new String&lbrack;&rsqb; &lbrace;do1&period;getData&lpar;&rpar;&comma;do2&period;getData&lpar;&rpar;&comma;do3&period;getData&lpar;&rpar;&rcub;&semi; &NewLine;&Tab; &rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3>Step 3&colon; Let&&num;8217&semi;s create Composite Entity&period;<&sol;h3>&NewLine;<p><strong>CompositeEntity&period;java<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;patterns&period;j2ee&period;compositeentity&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class CompositeEntity &lbrace; &NewLine;&Tab; &NewLine;&Tab;private CoarseGrainedObject cgo &equals; new CoarseGrainedObject&lpar;&rpar;&semi; &NewLine; &NewLine;&Tab;public void setData&lpar;String data1&comma; String data2&comma; String data3&rpar;&lbrace; &NewLine;&Tab;&Tab;cgo&period;setData&lpar;data1&comma; data2&comma; data3&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine; &NewLine;&Tab;public String&lbrack;&rsqb; getData&lpar;&rpar;&lbrace; &NewLine;&Tab;&Tab;return cgo&period;getData&lpar;&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3>Step 4&colon; Let&&num;8217&semi;s create Client class to use Composite Entity&period;<&sol;h3>&NewLine;<p><strong>Client&period;java<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;patterns&period;j2ee&period;compositeentity&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class Client &lbrace; &NewLine;&Tab; &NewLine;&Tab;private CompositeEntity compositeEntity &equals; new CompositeEntity&lpar;&rpar;&semi; &NewLine; &NewLine;&Tab;public void printData&lpar;&rpar;&lbrace; &NewLine;&Tab; &NewLine;&Tab;&Tab;for &lpar;int i &equals; 0&semi; i &lt&semi; compositeEntity&period;getData&lpar;&rpar;&period;length&semi; i&plus;&plus;&rpar; &lbrace; &NewLine;&Tab;&Tab;&Tab;System&period;out&period;println&lpar;"Data&colon; " &plus; compositeEntity&period;getData&lpar;&rpar;&lbrack;i&rsqb;&rpar;&semi; &NewLine;&Tab;&Tab;&rcub; &NewLine;&Tab;&rcub; &NewLine; &NewLine;&Tab;public void setData&lpar;String data1&comma; String data2&comma; String data3&rpar;&lbrace; &NewLine;&Tab;&Tab;compositeEntity&period;setData&lpar;data1&comma; data2&comma; data3&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3>Step 5&colon; Let&&num;8217&semi;s create a demo class and use the Client to demonstrate Composite Entity design pattern usage&period;<&sol;h3>&NewLine;<p><strong>CompositeEntityPatternDemo&period;java<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;patterns&period;j2ee&period;compositeentity&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class CompositeEntityPatternDemo &lbrace; &NewLine; &NewLine;&Tab;&sol;&ast;&ast; &NewLine;&Tab; &ast; &commat;param args &NewLine;&Tab; &ast;&sol; &NewLine;&Tab;public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine;&Tab;&Tab;Client client &equals; new Client&lpar;&rpar;&semi; &NewLine;&Tab;&Tab;client&period;setData&lpar;"Dinesh"&comma; "Arnav"&comma; "Anamika"&rpar;&semi; &NewLine;&Tab;&Tab;client&period;printData&lpar;&rpar;&semi; &NewLine;&Tab;&Tab;client&period;setData&lpar;"Adesh"&comma; "Vinesh"&comma; "Akrati"&rpar;&semi; &NewLine;&Tab;&Tab;client&period;printData&lpar;&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3>Step 6&colon; Let&&num;8217&semi;s run this demo class and verify the output&period;<&sol;h3>&NewLine;<pre class&equals;"highlight">Data&colon; Dinesh &NewLine;Data&colon; Arnav &NewLine;Data&colon; Anamika &NewLine;Data&colon; Adesh &NewLine;Data&colon; Vinesh &NewLine;Data&colon; Akrati &NewLine; &NewLine;<&sol;pre>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;business-object&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;transfer-object&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '4062'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago