Effective Java



Download 2,19 Mb.
Pdf ko'rish
bet162/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   158   159   160   161   162   163   164   165   ...   341
Bog'liq
Effective Java



ITEM 38: EMULATE EXTENSIBLE ENUMS WITH INTERFACES
179
The resulting code is a bit less complex, and the 
test
method is a bit more flexi-
ble: it allows the caller to combine operations from multiple implementation
types. On the other hand, you forgo the ability to use 
EnumSet
(Item 36) and
EnumMap
(Item 37) on the specified operations.
Both programs shown previously will produce this output when run with com-
mand line arguments 
4
and 
2
:
4.000000 ^ 2.000000 = 16.000000
4.000000 % 2.000000 = 0.000000
A minor disadvantage of the use of interfaces to emulate extensible enums is
that implementations cannot be inherited from one enum type to another. If the
implementation code does not rely on any state, it can be placed in the interface,
using default implementations (Item 20). In the case of our 
Operation
example,
the logic to store and retrieve the symbol associated with an operation must be
duplicated in 
BasicOperation
and 
ExtendedOperation
. In this case it doesn’t
matter because very little code is duplicated. If there were a larger amount of
shared functionality, you could encapsulate it in a helper class or a static helper
method to eliminate the code duplication.
The pattern described in this item is used in the Java libraries. For example,
the 
java.nio.file.LinkOption
enum type implements the 
CopyOption
and
OpenOption
interfaces. 
In summary, 

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   158   159   160   161   162   163   164   165   ...   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