Section 6.3. Binding Class Templates


6.3. Binding Class Templates

In Chapter 5, we saw how the parameters declared on class templates could be realized using subclassing on a class diagram. Although this approach works fine, the real power of templates comes when you bind template parameters at runtime. To do this, you take a template and tell it the types that its parameters are going to be as it is constructed into an object.

Object diagrams are ideal for modeling how runtime binding takes place. When you use runtime template parameter binding, you are really talking about objects rather than classes, so you can't really model this information on a regular class diagram.

Although this book talks about UML in terms of diagram types, the UML specification is not actually constrained to a particular set of diagrams. In fact, you could show object diagram notation on a class diagram if you wanted to group your classes and their runtime bindings on the same diagram.


Figure 6-11 shows a simple class template for a list collection taken from Chapter 4. Collections are great candidates for templates because they need to manage a collection of objects, but they don't usually care what classes those objects are.

Figure 6-11. The ListOfThings collection can store and remove any class of object to which the E parameter is bound


To model that the ListOfThings template's E parameter is to be bound at runtime to a particular class, all you need to do is add the parameter binding details to the end of the object's class description, as shown in Figure 6-12.

Figure 6-12. The listOfBlogEntries reuses the generic ListOfThings template, binding the E parameter to the BlogEntry class to store only objects of the BlogEntry class


Up until recently, showing runtime binding of templates in Java would have been impossible; the language did not support templates at all. However, with the release of Java 5 and the new generics language features, the runtime binding of the BlogEntry class to the E parameter on the ListOfThings template can now be implemented, as shown in Example 6-2.

Example 8-2. Using Java 5 generics to implement the ListOfThings template and a runtime binding to the ListOfBlogEntries

 public class ListOfThings<E> {      // We're cheating a bit here; Java actually already has a List template    // and so we're using that for our ListOfThings templates operations.    private List[E] elements;      public ListOfThings {       elements = new ArrayList<E>(  );    }      public int add(E object) {       return elements.add(object);    }      public E remove(int index) {       return elements.remove(index);    } }   public class Application {      public static void main(String[] args) {       // Binding the E parameter on the ListOfThings template to a Musician class       // to create a ListOfThings that will only store Musician objects.       ListOfThings <BlogEntry>listOfBlogEntries= new ListOfThings<BlogEntry>(  );    } } 

There is a lot more to generics in Java than the simple template implementation provided here. For more examples, see Java 5 Tiger: A Developer's Notebook (O'Reilly).




Learning UML 2.0
Learning UML 2.0
ISBN: 0596009828
EAN: 2147483647
Year: 2007
Pages: 175

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