Clean Code



Download 3,58 Mb.
Pdf ko'rish
bet254/384
Sana05.04.2022
Hajmi3,58 Mb.
#530298
1   ...   250   251   252   253   254   255   256   257   ...   384
Bog'liq
Clean Code

Then Make It Right
EARLIEST_DATE_ORDINAL
and
LATEST_DATE_ORDINAL
do not really belong in 
DayDate
and
should be moved to 
SpreadsheetDate
[G6].
Indeed, a search of the code shows that these variables are used only within
SpreadsheetDate
. Nothing in 
DayDate
, nor in any other class in the 
JCommon
framework, uses
them. Therefore, I’ll move them down into 
SpreadsheetDate
.
The next variables, 
MINIMUM_YEAR_SUPPORTED
, and 
MAXIMUM_YEAR_SUPPORTED
(line 104
and line 107), provide something of a dilemma. It seems clear that if 
DayDate
is an abstract
class that provides no foreshadowing of implementation, then it should not inform us
about a minimum or maximum year. Again, I am tempted to move these variables down
into
SpreadsheetDate
[G6]. However, a quick search of the users of these variables shows
that one other class uses them: 
RelativeDayOfWeekRule
(Listing B-6, page 390). We see that
usage at line 177 and line 178 in the 
getDate
function, where they are used to check that
the argument to 
getDate
is a valid year. The dilemma is that a user of an abstract class
needs information about its implementation. 
What we need to do is provide this information without polluting 
DayDate
itself.
Usually, we would get implementation information from an instance of a derivative.
However, the 
getDate
function is not passed an instance of a 
DayDate
. It does, however,
return such an instance, which means that somewhere it must be creating it. Line 187
through line 205 provide the hint. The 
DayDate
instance is being created by one of the
three functions, 
getPreviousDayOfWeek
,
getNearestDayOfWeek
, or 
getFollowingDayOfWeek
.
Looking back at the 
DayDate
listing, we see that these functions (lines 638–724) all return
a date created by 
addDays
(line 571), which calls 
createInstance
(line 808), which creates
a
SpreadsheetDate
! [G7].
It’s generally a bad idea for base classes to know about their derivatives. To fix this, we
should use the A
BSTRACT
F
ACTORY
3
pattern and create a 
DayDateFactory
. This factory will
create the instances of 
DayDate
that we need and can also answer questions about the
implementation, such as the maximum and minimum dates.
3.
[GOF].
public abstract class DayDateFactory {
private static DayDateFactory factory = new SpreadsheetDateFactory();
public static void setInstance(DayDateFactory factory) {
DayDateFactory.factory = factory;
}
protected abstract DayDate _makeDate(int ordinal);
protected abstract DayDate _makeDate(int day, DayDate.Month month, int year);
protected abstract DayDate _makeDate(int day, int month, int year);
protected abstract DayDate _makeDate(java.util.Date date);
protected abstract int _getMinimumYear();
protected abstract int _getMaximumYear();
public static DayDate makeDate(int ordinal) {
return factory._makeDate(ordinal);
}


274

Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   250   251   252   253   254   255   256   257   ...   384




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