What are parameterized classes in UML?

John Moore

Some object-oriented languages such as C++ and Ada support the concept of parameterized classes. C++ calls this facility “templates”, and Ada calls it “generics”; UML uses the C++ term. Java does not currently support the concept of parameterized classes, but it is a topic of research and is being considered for future versions of Java. See, for example, JSR-000014 Add Generic Types To The Java Programming Language and A Generic Java Language Extension.

To understand templates, suppose that you have an application where you need a list of objects of class Customer. In Java, you could create a list (e.g., using LinkedList) and then put customers on the list, but the list itself is not restricted to objects of class Customer; i.e., no type checking is performed when objects are added to the list. You could create an object of another class, say Company, and then add that object to the list. Furthermore, the list is actually a list of Objects, so if you remove an object from the list you must cast it to an appropriate type in order to perform meaningful operations. You could, of course, create a new class, say CustomerList, which would only allow objects of class Customer and which would return objects of class Customer, and this new class could be implemented using a Java LinkedList object in its private fields. But creating a whole new class which implements all of the List functionality just for Customers would be a lot of work. And what if you also wanted a list of Company objects? Templates would greatly simplify the creation of these specialized classes.

Using pseudocode with a C++/Java-like syntax, we might declare a List template as follows:

template <class Element>
public class List
  {
    // ...

    public void add(Element e)
      {
        ...
      }

    public Element get(int index)
      {
        ...
      }

    public boolean isEmpty()
      {
        ...
      }

    ...  // other List methods
  }
Notice that many of the list operations are defined in terms of a type parameter called Element. Notice also that the template doesn’t actually define a class, but it defines a way of creating a class based on a given Element type. In order to use a template, we must first create a class based on the template, a process commonly referred to as instantiation, and then use the newly created list class for creating list objects. Some languages allow both of these steps to be combined in a single statement. Again using a C++/Java like syntax, we might use the above template as follows:
// instantiate the template List class for Customers
class CustomerList = new List<Customer>;

// create an object of class CustomerList
CustomerList custList = new CustomerList();

// create a new customer object
Customer c = new Customer();

...  // do something with the customer object

// put the customer on the list
custList.add(c)
Many useful classes (e.g., list) and algorithms (e.g., sort) have the property that the types of objects they work with are not important to their overall purpose. Templates provide a mechanism which retains all of the benefits of strong typing but allow parameterization of classes by types.
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact