Section 5.5. Templates


5.5. Templates

Templates are an advanced but useful feature of object orientation. A templateor parameterized class, as they are sometimes referred tois helpful when you want to postpone the decision as to which classes a class will work with. When you declare a template, as shown in Figure 5-19, it is similar to declaring, "I know this class will have to work with other classes, but I don't know or necessarily care what those classes actually end up being."

Figure 5-19. A template in UML is shown by providing an extra box with a dashed border to the top right of the regular class box


The ListOfThings class in Figure 5-19 is parameterized with the type referred to as E. There is no class in our model called E; E is nothing more than a placeholder that can be used at a later point to tell the ListOfThings class the type of object that it will need to store.

Lists

Lists tend to be the most common examples of how to use templates , and with very good reason. Lists and their cousins, such as maps and sets, all store objects in different ways, but they don't actually care what classes those objects are constructed from. For this reason, one of the best real-world uses of templates is in the Java collection classes. Prior to Java 5, the Java programming language did not have a means of specifying templates. With the release of Java 5 and its generics feature, you can now not only create your own templates, but the original collection classes are all available to use as templates as well. To find out more about Java 5 generics, check out the latest edition of Java in a Nutshell (O'Reilly).


To use a class that is a template, you first need to bind its parameters. The ListOfSomething class template doesn't yet know what it's supposed to be storing; you need to tell the template what actual classes it will be working with; you need to bind the parameter referred to so far as just E to an actual class.

You can bind a template's parameters to a specific set of classes in one of two ways. First, you can subclass the template, binding the parameters as you go, as shown in Figure 5-20.

Figure 5-20. The ListOfThings class is subclassed into a ListOfBlogEntries, binding the single parameter E to the concrete BlogEntry class


Binding by subclass in Figure 5-20 allows you to reuse all of the generic behavior in the ListOfThings class and restrict that behavior in the ListOfBlogEntries class to only adding and removing BlogEntry objects.

The real power of templates is much more obvious when you use the second approach to template parameter bindingbinding at runtime. You bind at runtime when a template is told the type of parameters it will have as it is constructed into an object.

Runtime template binding is about objects rather than classes; therefore, a new type of diagram is needed: the object diagram. Object diagrams use classes to show some of the important ways they are used as your system runs. As luck would have it, object diagrams are the subject of the very next chapter.




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