Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



Download 4,06 Mb.
Pdf ko'rish
bet251/288
Sana07.04.2022
Hajmi4,06 Mb.
#535140
1   ...   247   248   249   250   251   252   253   254   ...   288
Bog'liq
GOF Design Patterns

Design Patterns: Elements of Reusable Object-Oriented Software 
373 
public: 
virtual ~Element(); 
virtual void Accept(Visitor&) = 0; 
protected: 
Element(); 
}; 
class ElementA : public Element { 
public: 
ElementA(); 
virtual void Accept(Visitor& v) { v.VisitElementA(this); } 
}; 
class ElementB : public Element { 
public: 
ElementB(); 
virtual void Accept(Visitor& v) { v.VisitElementB(this); } 
}; 
A CompositeElement class might implement Acceptlike this: 
class CompositeElement : public Element { 
public: 
virtual void Accept(Visitor&); 
private: 
List* _children; 
}; 
void CompositeElement::Accept (Visitor& v) { 
ListIterator i(_children); 
for (i.First(); !i.IsDone(); i.Next()) { 
i.CurrentItem()->Accept(v); 

v.VisitCompositeElement(this); 

Here are two other implementation issues that arise when you apply theVisitor 
pattern: 
1.
Double dispatch.
Effectively, the Visitor pattern lets you add operations 
to classeswithout changing them. Visitor achieves this by using a 
techniquecalled 
double-dispatch
. It's a well-known technique. Infact, some 


Design Patterns: Elements of Reusable Object-Oriented Software 
374 
programming languages support it directly (CLOS, forexample). Languages 
like C++ and Smalltalk support
single-dispatch

In single-dispatch languages, two criteria determine which operationwill 
fulfill a request: the name of the request and the type ofreceiver. For 
example, the operation that a GenerateCode request willcall depends on the 
type of node object you ask. In C++, callingGenerateCode on an instance 
of VariableRefNode willcall VariableRefNode::GenerateCode (which 
generates code for avariable reference). Calling GenerateCode on 
anAssignmentNode will callAssignmentNode::GenerateCode (which will 
generate code for anassignment). The operation that gets executed depends 
both on the kindof request and the type of the receiver. 
"Double-dispatch" simply means the operation that gets executeddepends on 
the kind of request and the types of 
two
receivers.Accept is a 
double-dispatch operation. Its meaning dependson two types: the Visitor's 
and the Element's. Double-dispatchinglets visitors request different 
operations on each class ofelement.
11
This is the key to the Visitor pattern: The operation that getsexecuted 
depends on both the type of Visitor and the type of Elementit visits. Instead 
of binding operations statically into the Elementinterface, you can 
consolidate the operations in a Visitor and useAccept to do the binding 
at run-time. Extending the Elementinterface amounts to defining one new 
Visitor subclass rather than many newElement subclasses. 
2.
Who is responsible for traversing the object structure?
A visitor must visit 
each element of the object structure. The questionis, how does it get there? 
We can put responsibility for traversal inany of three places: in the object 
structure, in the visitor, or in aseparate iterator object (see Iterator 
(289)). 
Often the object structure is responsible for iteration. A collectionwill 
simply iterate over its elements, calling the Accept operation oneach. A 
composite will commonly traverse itself by having each Acceptoperation 
traverse the element's children and call Accept on each ofthem recursively. 
Another solution is to use an iterator to visit the elements. In C++,you 
could use either an internal or external iterator, depending on whatis 
available and what is most efficient. In Smalltalk, you usually usean 
internal iterator using do: and a block. Since internaliterators are 
implemented by the object structure, using an internaliterator is a lot 
like making the object structure responsible foriteration. The main 
difference is that an internal iterator will notcause 
double-dispatching

it will call an operation on the 
visitor
with an 
element



Download 4,06 Mb.

Do'stlaringiz bilan baham:
1   ...   247   248   249   250   251   252   253   254   ...   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