Composition entity mode (Composite Entity Pattern)

Composition entity mode (Composite Entity Pattern) used in the EJB persistence mechanism. A combination of an EJB entity is an entity bean, representing the object illustration. When a combination of updating entity dependent objects inside beans automatically updated as they are managed by the EJB entity bean. The following is a participant in combination entity bean. Combined entity (Composite Entity) - which is the main entity bean. It may be coarse, or may comprise a coarse-grained object used ..


Composition entity mode (Composite Entity Pattern) used in the EJB persistence mechanism. A combination of an EJB entity is an entity bean, representing the object illustration. When a combination of updating entity dependent objects inside beans automatically updated as they are managed by the EJB entity bean. The following is a participant in combination entity bean.

  • Combined entity (the Entity Composite)  - which is the main entity bean. It may be coarse, or may comprise a coarse-grained objects, for sustained life cycle.

  • Coarse-grained objects (Coarse-Grained Object)  - The object contains its object. It has its own life cycle, but also to manage the life cycle of dependent objects.

  • Dependent objects (Dependent Object)  - dependent objects is a continuous life cycle depends on the coarse-grained object object.

  • Strategy (Strategies,)  - Strategy shows how to achieve a combination of entities.

achieve

As a combined entity, we will create  CompositeEntity  object. CoarseGrainedObject  is a dependency object class contains.

CompositeEntityPatternDemo , our demo class uses  Client  class to demonstrate the use of a combination of physical models.

compositeentity_pattern_uml_diagram.jpg

step 1

Create dependent objects.

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 combination of entities.

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

Created using a combination of solid client class.

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 a combination of physical 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

Verify the output.

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

Guess you like

Origin www.cnblogs.com/benbenhan/p/12396507.html