Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



Download 4,06 Mb.
Pdf ko'rish
bet174/288
Sana07.04.2022
Hajmi4,06 Mb.
#535140
1   ...   170   171   172   173   174   175   176   177   ...   288
Bog'liq
GOF Design Patterns

Design Patterns: Elements of Reusable Object-Oriented Software 
257 
case Help: 
// cast argument to appropriate type 
HandleHelp((HelpRequest*) theRequest); 
break; 
case Print: 
HandlePrint((PrintRequest*) theRequest); 
// ... 
break; 
default: 
// ... 
break; 


Subclasses can extend the dispatch by overridingHandleRequest. The 
subclass handles only therequests in which it's interested; other requests 
are forwarded to theparent class. In this way, subclasses effectively extend 
(rather thanoverride) the HandleRequest operation.For example, here's how 
an ExtendedHandler subclass extendsHandler's version of HandleRequest: 
class ExtendedHandler : public Handler { 
public: 
virtual void HandleRequest(Request* theRequest); 
// ... 
}; 
void ExtendedHandler::HandleRequest(Request*theRequest){ 
switch (theRequest->GetKind()) { 
case Preview: 
// handle the Preview request 
break; 
default: 
// let Handler handle other requests 
Handler::HandleRequest(theRequest); 


4.
Automatic forwarding in Smalltalk.
You can use the doesNotUnderstand 
mechanism in Smalltalk toforward requests. Messages that have no 
corresponding methods aretrapped in the implementation of 
doesNotUnderstand, whichcan be overridden to forward the message to an 
object's successor.Thus it isn't necessary to implement forwarding 
manually; the classhandles only the request in which it's interested, and 
it relies ondoesNotUnderstand to forward all others. 


Design Patterns: Elements of Reusable Object-Oriented Software 
258 
Sample Code 
The following example illustrates how a chain of responsibility canhandle requests 
for an on-line help system like the one describedearlier. The help request is 
an explicit operation. We'll use existingparent references in the widget hierarchy 
to propagate requestsbetween widgets in the chain, and we'll define a reference 
in theHandler class to propagate help requests between nonwidgets in thechain. 
The HelpHandler class defines the interface for handlinghelp requests. It 
maintains a help topic (which is empty by default)and keeps a reference to its 
successor on the chain of help handlers.The key operation is HandleHelp, which 
subclassesoverride. HasHelp is a convenience operation for checkingwhether there 
is an associated help topic. 
typedef int Topic
const Topic NO_HELP_TOPIC = -1; 
class HelpHandler { 
public: 
HelpHandler(HelpHandler* = 0, Topic = NO_HELP_TOPIC); 
virtual bool HasHelp(); 
virtual void SetHandler(HelpHandler*, Topic); 
virtual void HandleHelp(); 
private: 
HelpHandler* _successor; 
Topic _topic; 
}; 
HelpHandler::HelpHandler (
HelpHandler* h, Topic t
) : _successor(h), _topic(t) { } 
bool HelpHandler::HasHelp () { 
return _topic != NO_HELP_TOPIC; 

void HelpHandler::HandleHelp () { 
if (_successor != 0) { 
successor->HandleHelp(); 





Download 4,06 Mb.

Do'stlaringiz bilan baham:
1   ...   170   171   172   173   174   175   176   177   ...   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