Effective Java


the Builder pattern is a good choice when designing classes



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

 the Builder pattern is a good choice when designing classes
whose constructors or static factories would have more than a handful of
parameters
, especially if many of the parameters are optional or of identical type.
Client code is much easier to read and write with builders than with telescoping
constructors, and builders are much safer than JavaBeans.


ITEM 3: ENFORCE THE SINGLETON PROPERTY WITH A PRIVATE CONSTRUCTOR OR AN ENUM TYPE
17
Item 3:
Enforce the singleton property with a private 
constructor or an enum type 

singleton
is simply a class that is instantiated exactly once [Gamma95]. Single-
tons typically represent either a stateless object such as a function (Item 24) or a
system component that is intrinsically unique. 
Making a class a singleton can
make it difficult to test its clients
because it’s impossible to substitute a mock
implementation for a singleton unless it implements an interface that serves as its
type.
There are two common ways to implement singletons. Both are based on
keeping the constructor private and exporting a public static member to provide
access to the sole instance. In one approach, the member is a final field:
// Singleton with public final field
public class Elvis {
public static final Elvis INSTANCE = new Elvis();
private Elvis() { ... }
public void leaveTheBuilding() { ... }
}
The private constructor is called only once, to initialize the public static final
field 
Elvis.INSTANCE
. The lack of a public or protected constructor 
guarantees
a
“monoelvistic” universe: exactly one 
Elvis
instance will exist once the 
Elvis
class is initialized—no more, no less. Nothing that a client does can change this,
with one caveat: a privileged client can invoke the private constructor reflectively
(Item 65) with the aid of the 
AccessibleObject.setAccessible
method. If you
need to defend against this attack, modify the constructor to make it throw an
exception if it’s asked to create a second instance.
In the second approach to implementing singletons, the public member is a
static factory method:

Download 2,19 Mb.

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