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.

Composite Entity
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