[ Team LiB ] |
![]() ![]() |
Template (Parameterized) ClassSeveral languages, most noticeably C++, have the notion of a parameterized class, or template. (Templates are on the list to be included in Java and C# in the near future.) This concept is most obviously useful for working with collections in a strongly typed language. This way, you can define behavior for sets in general by defining a template class Set. class Set <T> { void insert (T newElement); void remove (T anElement); When you have done this, you can use the general definition to make Set classes for more specific elements: Set <Employee> employeeSet; You declare a template class in the UML by using the notation shown in Figure 5.17. The T in the diagram is a placeholder for the type parameter. (You may have more than one.) Figure 5.17. Template class
A use of a parameterized class, such as Set<Employee>, is called a derivation. You can show a derivation in two ways. The first way mirrors the C++ syntax (see Figure 5.18). You describe the derivation expression within angle brackets in the form <parameter-name::parameter-value>. If there's only one parameter, conventional use often omits the parameter name. The alternative notation (see Figure 5.19) reinforces the link to the template and allows you to rename the bound element. Figure 5.18. Bound element (version 1)
Figure 5.19. Bound element (version 2)
The «bind» keyword is a stereotype on the refinement relationship. This relationship indicates that EmployeeSet will conform to the interface of Set. You can think of the EmployeeSet as a subtype of Set. This fits the other way of implementing type-specific collections, which is to declare all appropriate subtypes. Using a derivation is not the same as subtyping, however. You are not allowed to add features to the bound element, which is completely specified by its template; you are adding only restricting type information. If you want to add features, you must create a subtype. ![]() |
[ Team LiB ] |
![]() ![]() |