Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Composite Entity Pattern

Composite Entity Pattern

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


Summary

Intent

Convert tables in the database into composite objects in the application; these objects can represent a single record or a group of records in a table.

Main problem solved

Use Cases

Implementation

Key code

Application Example

Advantages

Disadvantages

Usage recommendation

Notes

Main roles included

- Represents a table in the database and can contain a single record or multiple records. - Represents a single record in a table and does not contain child entities. - Represents a group of records in a table and can contain other leaf or container entities. - Used to access and manipulate the database, encapsulating the data access logic. - Uses the Composite Entity pattern to access and manipulate database records.

The Composite Entity pattern provides an intuitive and flexible way to handle complex data relationships by mapping database tables to objects in the application.


Create a class that implements

We will create the CompositeEntity object.CoarseGrainedObject which is a class containing dependent objects.

CompositeEntityPatternDemo, and our demo class uses the Client class to demonstrate the use of the Composite Entity pattern.

Step 1

Create the dependent 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 the 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 the 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 the client class that uses the 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 the Composite Entity design pattern.

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

Run the program, output result:



Data: Test

Data: Data

Data: Second Test

Data: Data1