Seeing Collections in Practice


Collections are a common construct in computer science, so much so that the Java platform contains several different types of collections. Most of the time, collections differentiate themselves based on how they sort (or do not sort ) data stored within them. Some collections are simple queues with first-in first-out orders or last-in first-out orders. Other collections maintain objects in an order based on arbitrary data stored in the object. Collections common in enterprise applications often focus on persistent data and how to efficiently access the collections of persistent data.

To determine the common capabilities that collections offer, you can consider a simple business case of a collection of customers. In the context of customers, you need the collection to facilitate the following:

  • Creating new customers

  • Reading existing customer information

  • Updating customer information

  • Deleting customer information

These operations are the CRUD operations: create, read, update, delete. You will find these same operations on databases and any coherent collection implementation. The Java collections offer these operations in the form of the add operation, an Iterator that supports moving through the instances of objects in a collection and reading them, and the remove operation, which exists on the collection or iterator to delete elements from the collection.

You should note that there are no update operations on the base Collection interface in Java. Specific concrete collection types, such as the LinkedList , add set operations to update specific items in the collection, but in general, collections return references to objects and thus do not require specific update methods on the collection. For example, by getting an object out of a Vector , you can directly manipulate it and affect the "collected" object, as shown in Listing 7-1.

Listing 7-1: Simple Collection Example
start example
 Vector v = new Vector(1); SimplePerson p1 = new SimplePerson(); p1.name = "Paul"; System.out.println("Putting Paul into the Hashtable"); v.add(p1); SimplePerson p2 = (SimplePerson)v.get(0); System.out.println("Getting the Person and Changing the Name"); p2.name = "Paul2"; SimplePerson p3 = (SimplePerson)v.get(0);     // the following line prints "Paul2", showing the Vector     // collects and returns references, not copies System.out.println("Name from Vector was: "+p3.name); 
end example
 

The notion of having a reference to an object as opposed to a copy of the object is difficult to achieve with Web Services. Throughout this chapter, you will see the notion of object copying as opposed to object references inherent in the Java collections.

The Iterator class in Java is another difference between what is possible in Java and what a Web Service is capable of achieving. Java iterators are knowledgeable about changes that occur to a collection outside of your own thread. When the collection changes, an iterator throws an exception to let you know that your iterator is no longer valid. Web Services are stateless; maintaining references to individual iterators from the server-side object simply does not make sense. Further, translating iterators to the Web Service model could make for abysmal performance, especially considering the performance variability of Web Services and the need to continuously return to the server for each business object.

This chapter addresses the problem of the iterator by simply leaving it off your collection pattern. Instead, you will return to the original concept of a collection that contains the ability to create, read, update, and delete on the primary collection interface.




Web Service Patterns
Web Services Patterns: Java Edition
ISBN: 1590590848
EAN: 2147483647
Year: 2003
Pages: 190

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net