Design Patterns Composite Entity Pattern with Examples


Composite Entity Pattern is used in the EJB persistence mechanism. A composite entity is an EJB entity bean that represents an illustration of an object. When a composite entity is updated, the internal dependent object beans are automatically updated because they are managed by the EJB entity bean. The following are participants of a composite entity bean.

  • Composite Entity - This is the main entity bean. It can be coarse-grained or it can contain a coarse-grained object for a continuous lifecycle.
  • Coarse-Grained Object - This object contains dependent objects. It has its own life cycle and can manage the lifecycle of dependent objects.
  • Dependent Object - A dependent object is an object whose persistence lifecycle depends on a coarse-grained object.
  • Strategies - The strategy indicates how to implement a composite entity.

Implementation

We will create an CompositeEntity object as a composite entity. CoarseGrainedObject is a class that contains dependent objects.

CompositeEntityPatternDemo, our demo class uses the Client class to demonstrate the use of composite entity patterns.

esign Patterns Composite Entity Pattern diagram

Step 1

Create a dependency object.

DependentObject1.java

public class DependentObject1 { private String data; public void setData(String data){ this.data = data; } public String getData(){ return data; } }

DependentObject2.java

public class DependentObject2 { private String data; public void setData(String data){ this.data = data; } public String getData(){ return data; } }

Step 2

Create a coarse-grained object.

CoarseGrainedObject.java

public class CoarseGrainedObject { DependentObject1 do1 = new DependentObject1(); DependentObject2 do2 = new DependentObject2(); public void setData(String data1, String data2){ do1.setData(data1); do2.setData(data2); } public String[] getData(){ return new String[] {do1.getData(),do2.getData()}; } }

Step 3

Create a composite entity.

CompositeEntity.java

public class CompositeEntity { private CoarseGrainedObject cgo = new CoarseGrainedObject(); public void setData(String data1, String data2){ cgo.setData(data1, data2); } public String[] getData(){ return cgo.getData(); } }

Step 4

Create a client class that uses a composite entity.

Client.java

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){ compositeEntity.setData(data1, data2); } }

Step 5

Use Client to demonstrate the use of composite entity design patterns.

CompositeEntityPatternDemo.java

public class CompositeEntityPatternDemo { public static void main(String[] args) { Client client = new Client(); client.setData("Test", "Data"); client.printData(); client.setData("Second Test", "Data1"); client.printData(); } }

Step 6

Executing the program, outputting results:

Data: Test
Data: Data
Data: Second Test
Data: Data1





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition