Effective Java


Item 33: Consider typesafe heterogeneous containers



Download 2,19 Mb.
Pdf ko'rish
bet140/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   136   137   138   139   140   141   142   143   ...   341
Bog'liq
Effective Java

Item 33: Consider typesafe heterogeneous containers
Common uses of generics include 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 parame-
terized 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 types. The
Class
object for the type will play the part of the parameterized key. The reason
this works is that class 
Class
is generic. The type of a class literal is not simply
Class
, but 
Class
. For example, 
String.class
is of type 
Class
, and
Integer.class
is of type 
Class
. 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:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   136   137   138   139   140   141   142   143   ...   341




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish