Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



Download 4,06 Mb.
Pdf ko'rish
bet70/288
Sana07.04.2022
Hajmi4,06 Mb.
#535140
1   ...   66   67   68   69   70   71   72   73   ...   288
Bog'liq
GOF Design Patterns

Design Patterns: Elements of Reusable Object-Oriented Software 
88 
This approach works for finding spelling errors, but how does it helpus support 
multiple kinds of analysis? It looks like we have to addan operation like 
CheckMe(SpellingChecker&) to Glyph andits subclasses whenever we add a new kind 
of analysis. That's true ifwe insist on an 
independent
class for every analysis. 
Butthere's no reason why we can't give 
all
analysis classes thesame interface. 
Doing so lets us use them polymorphically. Thatmeans we can replace 
analysis-specific operations likeCheckMe(SpellingChecker&) with an 
analysis-independentoperation that takes a more general parameter. 
Visitor Class and Subclasses 
We'll use the term 
visitor
to refer generally to classesof objects that "visit" 
other objects during a traversal and dosomething appropriate.
12
In this case we 
can define aVisitor class that defines an abstract interface forvisiting glyphs 
in a structure. 
class Visitor { 
public: 
virtual void VisitCharacter(Character*) { } 
virtual void VisitRow(Row*) { } 
virtual void VisitImage(Image*) { } 
// ... and so forth 
}; 


Design Patterns: Elements of Reusable Object-Oriented Software 
89 
Concrete subclasses of Visitor perform different analyses.For example, we could 
have a SpellingCheckingVisitorsubclass for checking spelling, and a 
HyphenationVisitorsubclass for hyphenation. SpellingCheckingVisitor wouldbe 
implemented exactly as we implemented SpellingCheckerabove, except the operation 
names would reflect the more generalVisitor interface. For example,CheckCharacter 
would be called VisitCharacter. 
Since CheckMe isn't appropriate for visitors that don'tcheck anything, we'll give 
it a more general name: 
Accept. Its argument must also change to take aVisitor&, reflecting the fact 
that it can accept any visitor.Now adding a new analysis requires just defining 
a new subclass ofVisitor

we don't have to touch any of the glyph classes.We support 
all future analyses by adding this one operationto Glyph and its subclasses. 
We've already seen how spelling checking works. We use a similarapproach in 
HyphenationVisitor to accumulate text. Butonce HyphenationVisitor's 
VisitCharacter operationhas assembled an entire word, it works a little 
differently. Insteadof checking the word for misspelling, it applies a 
hyphenationalgorithm to determine the potential hyphenation points in the word,if 
any. Then at each hyphenation point, it inserts a 
discretionary
glyph into the 
composition. Discretionaryglyphs are instances of Discretionary, a subclass 
ofGlyph. 
A discretionary glyph has one of two possible appearances depending onwhether 
or not it is the last character on a line. If it's the lastcharacter, then the 
discretionary looks like a hyphen; if it's not atthe end of a line, then the 
discretionary has no appearancewhatsoever. The discretionary checks its parent 
(a Row object) to seeif it is the last child. The discretionary makes this check 
wheneverit's called on to draw itself or calculate its boundaries. Theformatting 
strategy treats discretionaries the same as whitespace,making them candidates 
for ending a line. The following diagram shows howan embedded discretionary can 
appear. 



Download 4,06 Mb.

Do'stlaringiz bilan baham:
1   ...   66   67   68   69   70   71   72   73   ...   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