Effective Java


// Inappropriate use of static utility - inflexible & untestable!



Download 2,19 Mb.
Pdf ko'rish
bet29/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   25   26   27   28   29   30   31   32   ...   341
Bog'liq
Effective Java

// Inappropriate use of static utility - inflexible & untestable!
public class SpellChecker {
private static final Lexicon dictionary = ...;
private SpellChecker() {} // Noninstantiable
public static boolean isValid(String word) { ... }
public static List suggestions(String typo) { ... }
}
Similarly, it’s not uncommon to see them implemented as singletons (Item 3):
// Inappropriate use of singleton - inflexible & untestable!
public class SpellChecker {
private final Lexicon dictionary = ...;
private SpellChecker(...) {}
public static INSTANCE = new SpellChecker(...);
public boolean isValid(String word) { ... }
public List suggestions(String typo) { ... }
}
Neither of these approaches is satisfactory, because they assume that there is
only one dictionary worth using. In practice, each language has its own dictionary,
and special dictionaries are used for special vocabularies. Also, it may be
desirable to use a special dictionary for testing. It is wishful thinking to assume
that a single dictionary will suffice for all time. 
You could try to have 
SpellChecker
support multiple dictionaries by making
the 
dictionary
field nonfinal and adding a method to change the dictionary in an
existing spell checker, but this would be awkward, error-prone, and unworkable in
a concurrent setting. 
Static utility classes and singletons are inappropriate for
classes whose behavior is parameterized by an underlying resource. 
What is required is the ability to support multiple instances of the class (in our
example, 
SpellChecker
), each of which uses the resource desired by the client (in
our example, the dictionary). A simple pattern that satisfies this requirement is to
pass the resource into the constructor when creating a new instance
. This is
one form of 
dependency injection
: the dictionary is a 
dependency
of the spell
checker and is 
injected
into the spell checker when it is created.


ITEM 5: PREFER DEPENDENCY INJECTION TO HARDWIRING RESOURCES
21

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   25   26   27   28   29   30   31   32   ...   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