Effective Java


// Singleton with static factory



Download 2,19 Mb.
Pdf ko'rish
bet26/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   22   23   24   25   26   27   28   29   ...   341
Bog'liq
Effective Java

// Singleton with static factory
public class Elvis {
private
static final Elvis INSTANCE = new Elvis();
private Elvis() { ... }
public static Elvis getInstance()
{ return INSTANCE; }
public void leaveTheBuilding() { ... }
}
All calls to 
Elvis.getInstance
return the same object reference, and no other
Elvis
instance will ever be created (with the same caveat mentioned earlier).


CHAPTER 2
CREATING AND DESTROYING OBJECTS
18
The main advantage of the public field approach is that the API makes it clear
that the class is a singleton: the public static field is final, so it will always contain
the same object reference. The second advantage is that it’s simpler.
One advantage of the static factory approach is that it gives you the flexibility
to change your mind about whether the class is a singleton without changing its
API. The factory method returns the sole instance, but it could be modified to
return, say, a separate instance for each thread that invokes it. A second advantage
is that you can write a 
generic singleton factory
if your application requires it
(Item 30). A final advantage of using a static factory is that a 
method reference
can
be used as a supplier, for example 
Elvis::instance
is a 
Supplier
.
Unless one of these advantages is relevant, the public field approach is preferable.
To make a singleton class that uses either of these approaches 
serializable
(Chapter 12), it is not sufficient merely to add 
implements
Serializable
to its
declaration. To maintain the singleton guarantee, declare all instance fields
transient
and provide a 
readResolve
method (Item 89). Otherwise, each time a
serialized instance is deserialized, a new instance will be created, leading, in the
case of our example, to spurious 
Elvis
sightings. To prevent this from happening,
add this 
readResolve
method to the 
Elvis
class:

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   22   23   24   25   26   27   28   29   ...   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