Design Patterns : Elements of Reusable Object-Oriented Software



Download 4,06 Mb.
Pdf ko'rish
bet45/288
Sana07.04.2022
Hajmi4,06 Mb.
#535140
1   ...   41   42   43   44   45   46   47   48   ...   288
Bog'liq
GOF Design Patterns

Strategy Pattern 
Encapsulating an algorithm in an object is the intent of the Strategy (349) pattern. 
The key participants in thepattern are Strategy objects (which encapsulate 
different algorithms)and the context in which they operate. Compositors are 


Design Patterns: Elements of Reusable Object-Oriented Software 
56 
strategies;they encapsulate different formatting algorithms. A composition is 
thecontext for a compositor strategy. 
The key to applying the Strategy pattern is designing interfaces forthe strategy 
and its context that are general enough to support arange of algorithms. You 
shouldn't have to change the strategy orcontext interface to support a new 
algorithm. In our example, thebasic Glyph interface's support for child access, 
insertion, andremoval is general enough to let Compositor subclasses change 
thedocument's physical structure, regardless of the algorithm they use todo it. 
Likewise, the Compositor interface gives compositions whateverthey need to 
initiate formatting. 
Embellishing the User Interface 
We consider two embellishments in Lexi's user interface. Thefirst adds a border 
around the text editing area to demarcate the pageof text. The second adds scroll 
bars that let the user view differentparts of the page. To make it easy to add 
and remove theseembellishments (especially at run-time), we shouldn't use 
inheritanceto add them to the user interface. We achieve the most flexibilityif 
other user interface objects don't even know the embellishments arethere. That 
will let us add and remove the embellishments withoutchanging other classes. 
Transparent Enclosure 
From a programming point of view, embellishing the user interface involves 
extending existing code. Using inheritance to do such extension 
precludesrearranging embellishments at run-time, but an equally serious problem 
is the explosion of classes that can result from an inheritance-basedapproach. 
We could add a border to Composition by subclassing it to yield 
aBorderedComposition class. Or we could add a scrolling interface inthe same way 
to yield a ScrollableComposition. If we want both scrollbars and a border, we 
might produce a BorderedScrollableComposition,and so forth. In the extreme, we 
end up with a class for everypossible combination of embellishments, a solution 
that quicklybecomes unworkable as the variety of embellishments grows. 
Object composition offers a potentially more workable and flexibleextension 
mechanism. But what objects do we compose? Since we knowwe're embellishing an 
existing glyph, we could make the embellishmentitself an object (say, an instance 
of class 
Border
). Thatgives us two candidates for composition, the glyph and the 
border. Thenext step is to decide who composes whom. We could have the bordercontain 
the glyph, which makes sense given that the border willsurround the glyph on the 
screen. Or we could do the opposite

putthe border into the glyph

but then we must 



Download 4,06 Mb.

Do'stlaringiz bilan baham:
1   ...   41   42   43   44   45   46   47   48   ...   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