Domain-Driven Design: Tackling Complexity in the Heart of Software



Download 7,21 Mb.
Pdf ko'rish
bet151/343
Sana17.11.2022
Hajmi7,21 Mb.
#867526
1   ...   147   148   149   150   151   152   153   154   ...   343
Bog'liq
Eric Evans 2003 - Domain-Driven Design - Tackling Complexity in the Heart of Software

Figure 9.10.
A simple invariant like this can be enforced using case logic in each operation capable of changing
contents.
class Bucket {
private float capacity;
private float contents;
public void pourIn(float addedVolume) {
if (contents + addedVolume > capacity) {
contents = capacity;
} else {
contents = contents + addedVolume;


}
}
}
This logic is so simple that the rule is obvious. But you can easily imagine this constraint getting
lost in a more complicated class. Let's factor it into a separate method, with a name that clearly
and explicitly expresses the significance of the constraint.
class Bucket {
private float capacity;
private float contents;
public void pourIn(float addedVolume) {
float volumePresent = contents + addedVolume;
contents = constrainedToCapacity(volumePresent);
}
private float constrainedToCapacity(float volumePlacedIn) {
if (volumePlacedIn > capacity) return capacity;
return volumePlacedIn;
}
}
Both versions of this code enforce the constraint, but the second has a more obvious relationship
to the model (the basic requirement of 
MODEL-DRIVEN DESIGN
). This very simple rule was
understandable in its original form, but when the rules being enforced are more complex, they
start to overwhelm the object or operation they apply to, as any implicit concept does. Factoring
the constraint into its own method allows us to give it an intention-revealing name that makes the
constraint explicit in our design. It is now a named thing we can discuss. This approach also gives
the constraint room. A more complex rule than this might easily produce a method longer than its
caller (the 
pourIn()
method, in this case). This way, the caller stays simple and focused on its
task while the constraint can grow in complexity if need be.
This separate method gives the constraint some room to grow, but there are lots of cases when a
constraint just can't fit comfortably in a single method. Or even if the method stays simple, it may
call on information that the object doesn't need for its primary responsibility. The rule may just
have no good home in an existing object.
Here are some warning signs that a constraint is distorting the design of its host object.
Evaluating a constraint requires data that does not otherwise fit the object's definition.

Download 7,21 Mb.

Do'stlaringiz bilan baham:
1   ...   147   148   149   150   151   152   153   154   ...   343




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2025
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