|
ContextIn a J2EE application, the server side business components are implemented using session beans, entity beans, data access objects, and so forth. Application clients frequently need to data, which is composed from multiple objects. ProblemApplication clients typically require the model or parts of the model to either present to the user or as an intermediate-processing step before providing some service. The application model is an abstraction of the business data and business logic implemented on the server side as business components. A model may be expressed as a collection of objects put together in a structured manner (tree or graph). In a J2EE application, the model is a distributed collection of objects such as session beans, or entity beans, or data access objects and other objects. For a client to obtain the data for the model, such as to display to the user or to perform some processing, it must access individually each distributed object that defines the model. This approach has several drawbacks:
Forces
SolutionUse a Value Object Assembler to build the required model or sub-model. The Value Object Assembler uses value objects to retrieve data from various business objects and other objects that define the model or part of the model. The ValueObject represents data from different business components. The intention of this ValueObject is to carry the data for the model to the client in a single method call. Since the model data can be complex, it is recommended that this value object be immutable. That is, the client purely obtains such value objects for presentation and processing purposes (that is, read-only). Clients are not allowed to make changes to the value objects. When the client needs the model data, and if the model is represented by a single coarse-grained component (such as an Aggregate Entity), then the process of obtaining the model data is simple. The client simply requests the coarse-grained components for its value object. However, most real world applications have a model comprised of a combination of many coarse-grained and fine-grained components. In this case, the client must interact with numerous such business components to obtain all the data necessary to represent the model. The immediate drawbacks of this approach can be seen in that the clients become tightly coupled with the model implementation (model elements) and that the clients tend to make numerous remote method invocations to obtain the data from each individual component. In some cases, a single coarse-grained component (see Aggregate Entity pattern) provides the model or parts of the model as a single value object (simple or composite). However, when multiple components represent the model, a single value object (simple or composite) may not specify the entire model. To represent the model, it is necessary to obtain value objects from various components and assemble them into a new, composite value object. The server, not the client, should perform such "on the fly" construction of the model. StructureThe following class diagram represents the relationships for the Value Object Assembler pattern.
Participants & ResponsibilitiesThe following sequence diagram shows the interaction between the various participants in this pattern. click to enlarge
ValueObjectAssembler The ValueObjectAssembler is the main class of this pattern. The ValueObjectAssembler constructs a new value object based on the requirements of the application when the client requests a composite value object. The ValueObjectAssembler then locates the required BusinessObject instances to retrieve data to build the composite value object. BusinessObjects are business tier components such as entity beans and session beans, data access objects, and so forth. Client If the ValueObjectAssembler is implemented as an arbitrary Java object, then the client is typically a Session Façade that provides the boundary controller layer to the business tier. If the ValueObjectAssembler is implemented as a session bean, then the client can be a Session Façade or a Business Delegate. BusinessObject The BusinessObject participates in the construction of the new value object by providing the required data to the ValueObjectAssembler. Therefore, the BusinessObject is a role that can be fulfilled by a session bean, an entity bean, a data access object, or a regular Java object. ValueObject The value object constructed by the ValueObjectAssembler and returned to the client . BusinessObject is a role that can be fulfilled by a session bean. It is shown as the BusinessSession in the sequence diagram. BusinessEntity BusinessObject is a role that can be fulfilled by an entity bean. It is shown as the BusinessEntity in the sequence diagram. DataAccessObject A data access object abstracts the database access and provides an interface to load and store data. When the ValueObjectAssembler needs to obtain data directly from the persistent storage to build the value object, it can use a data access object. This is shown as the DataAccessObject object in the diagrams. StrategiesThis section explains different strategies for implementing a Value Object Assembler pattern. ValueObjectAssembler as an arbitrary Java ObjectThe ValueObjectAssembler can be an arbitrary Java object and need not be an enterprise bean. In such implementations, a session bean usually fronts the ValueObjectAssembler. This session bean is typically a Session Façade that performs its other duties related to providing business services. The ValueObjectAssembler runs in the business tier regardless of the implementation strategies. The motivation for this is to prevent the remote invocations from the ValueObjectAssembler to the source objects from crossing the tier.
ValueObjectAssembler as a Session Bean StrategyThis strategy implements the ValueObjectAssembler as a session bean (as shown in the class diagram). If a session bean implementation is preferred to provide the ValueObjectAssembler as a business service, it is typically implemented as a stateless session bean. The business components that make up the application model are constantly involved in transactions with various clients. As a result, when a ValueObjectAssembler constructs a new composite value object from various business components, it produces a snapshot of the model at the time of construction. The model could change immediately thereafter if another client changes one or more business components, effectively changing the business application model. Therefore, implementing ValueObjectAssembler as a stateful session bean provides no benefits over implementing it as a stateless session bean as preserving the state of the composite model data value when the underlying model is changing is futile. If the underlying model changes, it causes the value object held by the ValueObjectAssembler to become stale. The ValueObjectAssembler, when next asked for the value object, either returns a stale state or re-constructs the value object to obtain the most recent snapshot. Therefore, it is recommended that the ValueObjectAssembler be a stateless session bean to leverage the benefits of stateless over stateful session beans. However, if the underlying model rarely changes, then the ValueObjectAssembler may be a stateful session bean and retain the newly constructed value object. But if it is a stateful session then, then the ValueObjectAssembler must include mechanisms to recognize changes to the underlying model and to reconstruct the model for the next client request. Business Object StrategyThe BusinessObject role in this pattern can be supported by different types of objects as explained below.
Consequences
When the client includes logic to manage the interactions with distributed components, it becomes difficult to clearly separate business logic from the client tier. The Value Object Assembler contains the business logic to maintain the object relationships and to construct the composite value object representing the model. The client needs no knowledge of how to construct the model or the different components that provide data to assemble the model. The Value Object Assembler drastically reduces the network overhead of remote method calls and chattiness. The client can request the data for the application model from the Value Object Assembler in a single remote method call. The assembler constructs and returns the composite value object for the model. However, the composite value object may contain a large amount of data. Thus, while use of the Value Object Assembler reduces the number of network calls, there is an increase in the amount of data transported in a single call. This tradeoff should be considered in applying this pattern. The server-side Value Object Assembler constructs the model as a composite value object without using any client resources. The client spends no time assembling the model. Typically, updates are isolated to a very small part of the model and can be performed by fine-grained transactions. These transactions focus on isolated parts of the model instead of locking up the coarse-grained object (model). After the client obtains the model and displays or processes it locally, the user (or the client) may need to update or otherwise modify the model. The client can interact directly with a session façade to accomplish this at a suitable granularity level. The Value Object Assembler is not involved in the transaction to update or modify the model. There is better performance control because transactional work with the model happens at the appropriate level of granularity. The Value Object Assembler constructs value objects on demand. These value objects are snapshots of the current state of the model represented by various business components. Once the client obtains a value object from the Value Object Assembler, that value object is entirely local to the client. Since the value objects are not network aware, other changes made to the business components used to construct the value object are not reflected in the value objects. Therefore, after the value object is obtained, it can quickly become stale if there are transactions on the business components. The Value Object Assembler builds a coarse-grained composite value object from multiple source objects and manages the relationships between these source objects. A source object can be a session bean, entity bean, another Value Object Assembler, or a regular Java object.
The Value Object Assembler needs to locate and use various business objects. The Service Locator pattern can be used in conjunction with the Value Object Assembler pattern whenever a business object or a service needs to be located. (c) 2000-2001 Sun Microsystems, Inc. All Rights Reserved. Version 1.0 Beta | |||
|
| ||||||||||||