Time for a Transfer Object?

If it’s likely that a business service might be asked to send or receive all or most of its data in a big, coarse-grained message, it’s common for that service to provide that feature in its API. Commonly, the business service creates a serializable Java object that contains lots of instance variables. Sun calls this object a Transfer Object. Outside of Sun there is a pattern called Data Transfer Object. Guess what? They’re the same thing. (Yeah, we feel the same way about that.)

image with no caption

The client’s perspective, inside the Business Delegate:

image with no caption

That’s it. Under the covers, the Transfer Object is serialized, shipped, and deserialized on to the client’s local JVM heap. At that point, it is just like any other local bean.

Note

The data in a Transfer Object grows stale!

Once it’s shipped across the network, the Transfer Object is completely out of touch with its source, and begins to fall out of sync with the state of the data in the underlying database. You’ll have to decide for each use case whether data integrity/synchronization is worth the performance hits.

Service Locator and Business Delegate both simplify model components

Listen in as our two black-belts debate which pattern is better—Service Locator or Business Delegate.

image with no caption

Service Locator is the superior pattern. First of all, unlike the Business Delegate, one Service Locator instance can support an entire application tier.

 
 

That’s true, but Service Locator needs to talk to only one remote entity. Business Delegate must handle many entity objects.

Service Locator is more efficient with network calls. It can cache references to stubs or service stubs once it has located them, reducing network traffic for subsequent calls.

 
 

With much respect, you are forgetting that Service Locator has a much easier task. The Business Delegate must carry the heavy burden of communicating with a dynamic object, whose data might change at any moment.

Heavy burden? Your simple business data does not impress me.

 
 

A Business Delegate gives web application programmers much more benefit than your Service Locator.

Ah, maybe programmers do benefit, but your simple pattern seems to forget that it often exists in a network environment. It will make many calls to business services with no restraint, no consideration for the overhead of remote calls.

 
 

Ah ha! The Business Delegate is not ashamed to form an alliance with the Transfer Object! Working as a team, they help the programmer AND minimize remote calls.

Yes, yes, your weak pattern needs assistance, we all know that. But when you partner with a Transfer Object other demons can haunt you... you haven’t forgotten your little problems with data staleness and concurrency, have you?

 
 

No, I haven’t forgotten. But when these issues come up they can be solved. You cannot expect to achieve great things without a little extra effort... nothing in J2EE is ever black and white.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset