- Transfer Object Pattern
- Service Locator Pattern
- Intercepting Filter Pattern
- Front Controller Pattern
- Data Access Object Pattern
- Composite Entity Pattern
- Business Delegate Pattern
- Design Patterns - MVC Pattern
- Design Patterns - Visitor Pattern
- Design Patterns - Template Pattern
- Design Patterns - Strategy Pattern
- Design Patterns - Null Object Pattern
- Design Patterns - State Pattern
- Design Patterns - Observer Pattern
- Design Patterns - Memento Pattern
- Design Patterns - Mediator Pattern
- Design Patterns - Iterator Pattern
- Design Patterns - Interpreter Pattern
- Design Patterns - Command Pattern
- Chain of Responsibility Pattern
- Design Patterns - Proxy Pattern
- Design Patterns - Flyweight Pattern
- Design Patterns - Facade Pattern
- Design Patterns - Decorator Pattern
- Design Patterns - Composite Pattern
- Design Patterns - Filter Pattern
- Design Patterns - Bridge Pattern
- Design Patterns - Adapter Pattern
- Design Patterns - Prototype Pattern
- Design Patterns - Builder Pattern
- Design Patterns - Singleton Pattern
- Abstract Factory Pattern
- Design Patterns - Factory Pattern
- Design Patterns - Overview
- Design Patterns - Home
Design Patterns Resources
- Design Patterns - Discussion
- Design Patterns - Useful Resources
- Design Patterns - Quick Guide
- Design Patterns - Questions/Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Design Patterns - Composite Entity Pattern
Composite Entity pattern is used in EJB persistence mechanism. A Composite entity is an EJB entity bean which represents a graph of objects. When a composite entity is updated, internally dependent objects beans get updated automatically as being managed by EJB entity bean. Following are the participants in Composite Entity Bean.
Composite Entity - It is primary entity bean. It can be coarse grained or can contain a coarse grained object to be used for persistence purpose.
Coarse-Grained Object - This object contains dependent objects. It has its own pfe cycle and also manages pfe cycle of dependent objects.
Dependent Object - Dependent object is an object which depends on coarse grained object for its persistence pfecycle.
Strategies - Strategies represents how to implement a Composite Entity.
Implementation
We are going to create CompositeEntity object acting as CompositeEntity. CoarseGrainedObject will be a class which contains dependent objects. CompositeEntityPatternDemo, our demo class will use Cpent class to demonstrate use of Composite Entity pattern.
Step 1
Create Dependent Objects.
DependentObject1.java
pubpc class DependentObject1 { private String data; pubpc void setData(String data){ this.data = data; } pubpc String getData(){ return data; } }
DependentObject2.java
pubpc class DependentObject2 { private String data; pubpc void setData(String data){ this.data = data; } pubpc String getData(){ return data; } }
Step 2
Create Coarse Grained Object.
CoarseGrainedObject.java
pubpc class CoarseGrainedObject { DependentObject1 do1 = new DependentObject1(); DependentObject2 do2 = new DependentObject2(); pubpc void setData(String data1, String data2){ do1.setData(data1); do2.setData(data2); } pubpc String[] getData(){ return new String[] {do1.getData(),do2.getData()}; } }
Step 3
Create Composite Entity.
CompositeEntity.java
pubpc class CompositeEntity { private CoarseGrainedObject cgo = new CoarseGrainedObject(); pubpc void setData(String data1, String data2){ cgo.setData(data1, data2); } pubpc String[] getData(){ return cgo.getData(); } }
Step 4
Create Cpent class to use Composite Entity.
Cpent.java
pubpc class Cpent { private CompositeEntity compositeEntity = new CompositeEntity(); pubpc void printData(){ for (int i = 0; i < compositeEntity.getData().length; i++) { System.out.println("Data: " + compositeEntity.getData()[i]); } } pubpc void setData(String data1, String data2){ compositeEntity.setData(data1, data2); } }
Step 5
Use the Cpent to demonstrate Composite Entity design pattern usage.
CompositeEntityPatternDemo.java
pubpc class CompositeEntityPatternDemo { pubpc static void main(String[] args) { Cpent cpent = new Cpent(); cpent.setData("Test", "Data"); cpent.printData(); cpent.setData("Second Test", "Data1"); cpent.printData(); } }
Step 6
Verify the output.
Data: Test Data: Data Data: Second Test Data: Data1Advertisements