Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



Download 4,06 Mb.
Pdf ko'rish
bet59/288
Sana07.04.2022
Hajmi4,06 Mb.
#535140
1   ...   55   56   57   58   59   60   61   62   ...   288
Bog'liq
GOF Design Patterns

Design Patterns: Elements of Reusable Object-Oriented Software 
73 
Encapsulating a Request 
From our perspective as designers, a pull-down menu is just anotherkind of glyph 
that contains other glyphs. What distinguishespull-down menus from other glyphs 
that have children is that mostglyphs in menus do some work in response to an 
up-click. 
Let's assume that these work-performing glyphs are instances of aGlyph subclass 
called 
MenuItem
and that they do their work inresponse to a request from a 
client.
9
Carrying out therequest might involve an operation on one object, or many 
operationson many objects, or something in between. 
We could define a subclass of MenuItem for every user operation andthen hard-code 
each subclass to carry out the request. But that's notreally right; we don't need 
a subclass of MenuItem for each requestany more than we need a subclass for each 
text string in a pull-downmenu. Moreover, this approach couples the request to 
a particularuser interface, making it hard to fulfill the request through 
adifferent user interface. 
To illustrate, suppose you could advance to the last page in thedocument both 
through a MenuItem in a pull-down menu 
and
bypressing a page icon at the bottom 
of Lexi's interface (which mightbe more convenient for short documents). If we 
associate the requestwith a MenuItem through inheritance, then we must do the 
same for thepage icon and any other kind of widget that might issue such arequest. 
That can give rise to a number of classes approaching theproduct of the number 
of widget types and the number of requests. 
What's missing is a mechanism that lets us parameterize menu items bythe request 
they should fulfill. That way we avoid a proliferation ofsubclasses and allow 
for greater flexibility at run-time. We couldparameterize MenuItem with a function 
to call, but that's not a completesolution for at least three reasons: 
1.
It doesn't address the undo/redo problem. 
2.
It's hard to associate state with a function. For example, afunction that 
changes the font needs to know 
which
font. 
3.
Functions are hard to extend, and it's hard to reuse parts of them. 
These reasons suggest that we should parameterize MenuItems with an
object
, not 
a function. Then we can use inheritance to extendand reuse the request's 
implementation. We also have a place to storestate and implement undo/redo 
functionality. Here we have anotherexample of encapsulating the concept that 
varies, in this case arequest. We'll encapsulate each request in a 

Download 4,06 Mb.

Do'stlaringiz bilan baham:
1   ...   55   56   57   58   59   60   61   62   ...   288




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