Effective Java


// Skeletal implementation class



Download 2,19 Mb.
Pdf ko'rish
bet96/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   92   93   94   95   96   97   98   99   ...   341
Bog'liq
Effective Java

// Skeletal implementation class
public abstract class AbstractMapEntry
implements Map.Entry {
// Entries in a modifiable map must override this method
@Override public V setValue(V value) {
throw new UnsupportedOperationException();
}


ITEM 20: PREFER INTERFACES TO ABSTRACT CLASSES
103
// Implements the general contract of Map.Entry.equals
@Override public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Map.Entry))
return false;
Map.Entry e = (Map.Entry) o;
return Objects.equals(e.getKey(),
getKey())
&& Objects.equals(e.getValue(), getValue());
}
// Implements the general contract of Map.Entry.hashCode
@Override public int hashCode() {
return Objects.hashCode(getKey())
^ Objects.hashCode(getValue());
}
@Override public String toString() {
return getKey() + "=" + getValue();
}
}
Note that this skeletal implementation could not be implemented in the
Map.Entry
interface or as a subinterface because default methods are not permit-
ted to override 
Object
methods such as 
equals

hashCode
, and 
toString
.
Because skeletal implementations are designed for inheritance, you should
follow all of the design and documentation guidelines in Item 19. For brevity’s
sake, the documentation comments were omitted from the previous example, but
good documentation is absolutely essential in a skeletal implementation,
whether it consists of default methods on an interface or a separate abstract class.
A minor variant on the skeletal implementation is the 
simple implementation,
exemplified by 
AbstractMap.SimpleEntry
. A simple implementation is like a
skeletal implementation in that it implements an interface and is designed for
inheritance, but it differs in that it isn’t abstract: it is the simplest possible working
implementation. You can use it as it stands or subclass it as circumstances warrant.
To summarize, an interface is generally the best way to define a type that
permits multiple implementations. If you export a nontrivial interface, you should
strongly consider providing a skeletal implementation to go with it. To the extent
possible, you should provide the skeletal implementation via default methods on
the interface so that all implementors of the interface can make use of it. That said,
restrictions on interfaces typically mandate that a skeletal implementation take the
form of an abstract class.


CHAPTER 4
CLASSES AND INTERFACES
104

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   92   93   94   95   96   97   98   99   ...   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