Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



Download 4,06 Mb.
Pdf ko'rish
bet183/288
Sana07.04.2022
Hajmi4,06 Mb.
#535140
1   ...   179   180   181   182   183   184   185   186   ...   288
Bog'liq
GOF Design Patterns
Копасов Игорь (1), Т Е Л Е Ф О Н О Г Р А М М А ЗООМ, 20201546 п.80 использ., Oliy matematika, презентация индустрия мутакил иши, 3000 most common words in English, Sun'iy intellekt, 1638511850 AE, 1638511850 AE, Hisob-raqan (Patent boji), O'RQ-600 23-MODDA, Talabaning haftalik hisoboti namunasi , iikinchi hafta amaliyoti., Таҳлил тест
Design Patterns: Elements of Reusable Object-Oriented Software 
270 
class OpenCommand : public Command { 
public: 
OpenCommand(Application*); 
virtual void Execute(); 
protected: 
virtual const char* AskUser(); 
private: 
Application* _application; 
char* _response; 
}; 
OpenCommand::OpenCommand (Application* a) { 
_application = a; 

void OpenCommand::Execute () { 
const char* name = AskUser(); 
if (name != 0) { 
Document* document = new Document(name); 
_application->Add(document); 
document->Open(); 


A PasteCommand must be passed a Document object asits receiver. The receiver is 
given as a parameter to PasteCommand'sconstructor. 
class PasteCommand : public Command { 
public: 
PasteCommand(Document*); 
virtual void Execute(); 
private: 
Document* _document; 
}; 
PasteCommand::PasteCommand (Document* doc) { _document = doc; } void 
PasteCommand::Execute () { _document->Paste(); } 
For simple commands that aren't undoable and don't require arguments,we can use 
a class template to parameterize the command's receiver.We'll define a template 
subclass SimpleCommand for suchcommands. SimpleCommand is parameterized by 
theReceiver type and maintains a binding between a receiver objectand an action 
stored as a pointer to a member function. 


Design Patterns: Elements of Reusable Object-Oriented Software 
271 
template  
class SimpleCommand : public Command { 
public: 
typedef void (Receiver::* Action)(); 
SimpleCommand(Receiver* r, Action a) : 
_receiver(r), _action(a) { } 
virtual void Execute(); 
private: 
Action _action; 
Receiver* _receiver; 
}; 
The constructor stores the receiver and the action in the correspondinginstance 
variables. Execute simply applies the action to thereceiver. 
template  
void SimpleCommand<Receiver>::Execute () { (_receiver->*_action)(); } 
To create a command that calls Actionon an instance of class MyClass, a client 
simply writes 
MyClass* receiver = new MyClass
// ...
Command* aCommand = 
new SimpleCommand(receiver, &MyClass::Action); 
// ... 
aCommand->Execute(); 
Keep in mind that this solution only works for simple commands. More complex 
commands that keep track of not only their receivers but also arguments and/or 
undo state require a Command subclass. 
A MacroCommand manages a sequence of subcommands and providesoperations for adding 
and removing subcommands. No explicit receiveris required, because the subcommands 
already define their receiver. 
class MacroCommand : public Command { 
public: 
MacroCommand(); 
virtual ~MacroCommand(); 
virtual void Add(Command*); 
virtual void Remove(Command*); 
virtual void Execute(); 
private: 
List* _cmds; 



Download 4,06 Mb.

Do'stlaringiz bilan baham:
1   ...   179   180   181   182   183   184   185   186   ...   288




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2022
ma'muriyatiga murojaat qiling

    Bosh sahifa
davlat universiteti
ta’lim vazirligi
axborot texnologiyalari
maxsus ta’lim
zbekiston respublikasi
guruh talabasi
O’zbekiston respublikasi
nomidagi toshkent
o’rta maxsus
davlat pedagogika
texnologiyalari universiteti
toshkent axborot
xorazmiy nomidagi
rivojlantirish vazirligi
pedagogika instituti
Ўзбекистон республикаси
tashkil etish
haqida tushuncha
таълим вазирлиги
vazirligi muhammad
O'zbekiston respublikasi
toshkent davlat
махсус таълим
respublikasi axborot
kommunikatsiyalarini rivojlantirish
vazirligi toshkent
saqlash vazirligi
fanidan tayyorlagan
bilan ishlash
Toshkent davlat
sog'liqni saqlash
uzbekistan coronavirus
respublikasi sog'liqni
coronavirus covid
koronavirus covid
vazirligi koronavirus
qarshi emlanganlik
covid vaccination
risida sertifikat
sertifikat ministry
vaccination certificate
Ishdan maqsad
fanidan mustaqil
matematika fakulteti
o’rta ta’lim
haqida umumiy
fanlar fakulteti
pedagogika universiteti
ishlab chiqarish
moliya instituti
fanining predmeti