• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

parametrize the key instead of the container

 
jacob deiter
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generics restrict numbers of parameters collection can contain.But if collection to contain more type of element with type-safe . The idea is to parametrize the key instead of the container.
I really not understand this, what they really means ,anybody explain it with some understandable example.
[ December 04, 2008: Message edited by: jacob deiter ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jacob deiter:
Generics restrict numbers of parameters collection can contain.But if collection to contain more type of element with type-safe . The idea is to parametrize the key instead of the container.
I really not understand this, what they really means ,anybody explain it with some understandable example.

[ December 04, 2008: Message edited by: jacob deiter ]


Can you give a link to where you got the quote from. I'm not sure many people will understand this - especially the second sentence.
 
jacob deiter
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Actually It is from the Book "Effective Java"

http://safari.informit.com/9780137150021/ch05lev1sec7

Item 29: Consider typesafe heterogeneous containers

The most common use of generics is for collections, such as Set and Map, and single-element containers, such as ThreadLocal and AtomicReference. In all of these uses, it is the container that is parameterized. This limits you to a fixed number of type parameters per container. Normally that is exactly what you want. A Set has a single type parameter, representing its element type; a Map has two, representing its key and value types; and so forth.

Sometimes, however, you need more flexibility. For example, a database row can have arbitrarily many columns, and it would be nice to be able to access all of them in a typesafe manner. Luckily, there is an easy way to achieve this effect. The idea is to parameterize the key instead of the container. Then present the parameterized key to the container to insert or retrieve a value. The generic type system is used to guarantee that the type of the value agrees with its key.

As a simple example of this approach, consider a Favorites class that allows its clients to store and retrieve a "favorite" instance of arbitrarily many other classes. The Class object will play the part of the parameterized key. The reason this works is that class Class was generified in release 1.5. The type of a class literal is no longer simply Class, but Class<T>. For example, String.class is of type Class<String>, and Integer.class is of type Class<Integer>. When a class literal is passed among methods to communicate both compile-time and runtime type information, it is called a type token [Bracha04].

The API for the Favorites class is simple. It looks just like a simple map, except that the key is parameterized instead of the map. The client presents a Class object when setting and getting favorites. Here is the API:

// Typesafe heterogeneous container pattern - API
public class Favorites {
public <T> void putFavorite(Class<T> type, T instance);
public <T> T getFavorite(Class<T> type);
 
Campbell Ritchie
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure I can improve on that. Bloch's writing is usually very clear. Have you read the whole chapter?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic