Design Pattern

Composite Entity Pattern – Core J2EE Patterns

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.

Composite Entity Pattern

The composite entity pattern uses various components for implementation. Each of the components has certain tasks to perform and certain problems to cater.

Spring 5 Design Pattern Book

You could purchase my Spring 5 book that is with title name “Spring 5 Design Patterns“. This book is available on the Amazon and Packt publisher website. Learn various design patterns and best practices in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- “AUTHDIS40“.

UML Class Diagram

Let’s see the following UML class diagram.


These components include;

  • Composite Entity
  • Coarse-grained Object
  • A dependent Object DependentObject1
  • DependentObject 2
  • DependentObject 3

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.

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.

Sample Implementation of the Composite Entity Pattern

Let’s see the following implementation of the Composite Entity Pattern.

Step 1: Let’s create Dependent Objects.

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;
	}
}

Step 2: Let’s create Coarse Grained Object.

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()};
	 }
}

Step 3: Let’s create Composite Entity.

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();
	}
}

Step 4: Let’s create Client class to use Composite Entity.

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);
	}
}

Step 5: Let’s create a demo class and use the Client to demonstrate Composite Entity design pattern usage.

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();
	}

}

Step 6: Let’s run this demo class and verify the output.

Data: Dinesh
Data: Arnav
Data: Anamika
Data: Adesh
Data: Vinesh
Data: Akrati

Previous
Next
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…

2 years ago

Decorator Pattern using Lambda

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

2 years ago

Delegating pattern using lambda

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

2 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;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

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

2 years ago

Scheduler @Scheduled Annotation Spring Boot

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

3 years ago