230 likes | 535 Views
8/21/2012. Mintao Si. 2. Content. ProblemForcesSolutionStructureStrategyConsequences. 8/21/2012. Mintao Si. 3. Problem. Context: Application clients need to exchange data with enterprise beans. J2EE enterprise beans include session beans and entity beans. Session beans may provide coarse-gr
E N D
1. Value Object Pattern by
Mintao Si (mtsi06@yahoo.com)
2. 8/21/2012 Mintao Si 2 Content Problem
Forces
Solution
Structure
Strategy
Consequences
3. 8/21/2012 Mintao Si 3 Problem Context: Application clients need to exchange data with enterprise beans.
J2EE enterprise beans include session beans and entity beans.
Session beans may provide coarse-grained service methods which may return data to the client. In such cases, the client must invoke the session bean's get methods multiple times until the client obtains values for all the attribute values.
An entity bean exposes the values of its attributes by providing an getter for each attribute. When a client needs the data values from an entity bean, it may invoke the entity bean's get methods multiple times until the client obtains data for every attribute that it needs.
Every such method call made to the enterprise bean is potentially remote and multiple remote method invocation may significantly degrade the application performance.
4. 8/21/2012 Mintao Si 4 Problem Clients make numerous remote procedure calls to obtain required data
5. 8/21/2012 Mintao Si 5 Forces J2EE applications implement business components as enterprise bean components. All access to an enterprise bean is performed via remote interfaces to the bean. Every call to an enterprise bean is potentially a remote method call with network overhead.
Typically, applications have a greater frequency of read transactions than update transactions. The client requires the data from the business tier for presentation, display, and other read-only types of processing.
The client usually requires more than one attribute value of an enterprise bean. Thus, the client may invoke multiple remote calls to obtain the required data.
The number of calls made by the client to the enterprise bean impacts network performance. Chattier applications-those with increased traffic between client and server tiers-often degrade network performance.
6. 8/21/2012 Mintao Si 6 Solution Use a Value Object to encapsulate the business data. When a client requests an enterprise bean for business data, the enterprise bean can construct a Value Object, populate it with its attribute values, and pass it by value to the client.
Using value objects can reduce remote procedure call and therefore reduce network overhead.
When an enterprise bean uses a value object, the client makes a single remote method invocation to the enterprise bean to request the value object instead of many remote method calls to get individual attribute values one at a time.
The client receives a value object and access the attribute values locally.
7. 8/21/2012 Mintao Si 7 Structure
8. 8/21/2012 Mintao Si 8 Strcuture Client
This represents the client of the enterprise bean.
BusinessObject
The BusinessObject represents a role in this pattern that can be fulfilled by a session bean or an entity bean. The BusinessObject is responsible for creating the value object and returning the ValueObject to the client. The BusinessObject receives the data request from the client and creates a new ValueObject.
ValueObject
The ValueObject is an arbitrary Java object. The ValueObject may provide a constructor that accepts all the required attributes to create the value object. The constructor may accept all entity bean attribute values that the value object is designed to hold. It is a design choice as to whether the ValueObject attributes are private and accessed via getters, or all the attributes are made public. Normally, there is no setter in a ValueObject and it is not modifiable after its creation.
9. 8/21/2012 Mintao Si 9 Strcuture public class ContactVO implements java.io.Serializable {
// public memberspublic String firstName; public String lastName; public String address;
// constructor accepting all valuespublic ContactVO(String firstName, String lastName, String address){init(firstName, lastName, address);}
// method to set all the valuespublic void init(String firstName, String lastName, String address) { this.firstName = firstName; this.lastName = lastName; this.address = address;}
}
10. 8/21/2012 Mintao Si 10 Interaction
11. 8/21/2012 Mintao Si 11 Strategies Updateable Value Object
can carry the changes required by the client back to the entity.
Multiple Value Objects
can produce different value objects depending on the client request.
Entity Inherits Value Object (for entity beans only)
use inheritance, since the entity bean attributes and the value object attributes are identical.
Value Objects Factory (for entity beans only)
create value objects on demand using reflection
12. 8/21/2012 Mintao Si 12 Updatable Value Object
13. 8/21/2012 Mintao Si 13 Updatable Value Object
14. 8/21/2012 Mintao Si 14 Multiple Value Objects
15. 8/21/2012 Mintao Si 15 Multiple Value Objects
16. 8/21/2012 Mintao Si 16 Entity Inherits Value Object
17. 8/21/2012 Mintao Si 17 Entity Inherits Value Object
18. 8/21/2012 Mintao Si 18 Entity Inherits Value Object public class ContactVO implements java.io.Serializable {
// public memberspublic String firstName; public String lastName; public String address;
// create a new value object public ContactVO getData() { return new ContactVO(this);}
}
public class ContactEntity extends ContactVO implements javax.ejb.EntityBean {
...
// the client calls the getData method// getData() is inherited from the Value Object...
}
19. 8/21/2012 Mintao Si 19 Value Objects Factory
20. 8/21/2012 Mintao Si 20 Value Objects Factory
21. 8/21/2012 Mintao Si 21 Consequences Simplifies Entity Bean and Remote Interface
Reduces Network Traffic
Update Propagation and Stale Value Objects
Synchronization and Version Control
Reduced Code duplication
Number of calls vs. amount of data shipped
Concurrent Access and Transactions
22. 8/21/2012 Mintao Si 22 Related patterns Session Façade
Value Object Assembler
Value List Handler
Aggregate Entity
23. 8/21/2012 Mintao Si 23 That’s all folks!
?????