Design Patterns : Elements of Reusable Object-Oriented Software


Design Patterns: Elements of Reusable Object-Oriented Software



Download 4,06 Mb.
Pdf ko'rish
bet178/288
Sana07.04.2022
Hajmi4,06 Mb.
#535140
1   ...   174   175   176   177   178   179   180   181   ...   288
Bog'liq
GOF Design Patterns

Design Patterns: Elements of Reusable Object-Oriented Software 
264 
Menus can be implemented easily with Command objects. Each choice ina Menu is 
an instance of a MenuItem class. An Application class createsthese menus and their 
menu items along with the rest of the user interface.The Application class also 
keeps track of Document objects that a user hasopened. 
The application configures each MenuItem with an instance of aconcrete Command 
subclass. When the user selects a MenuItem, theMenuItem calls Execute on its 
command, and Execute carries out theoperation. MenuItems don't know which subclass 
of Command they use.Command subclasses store the receiver of the request and invoke 
one ormore operations on the receiver. 
For example, PasteCommand supports pasting text from the clipboardinto a Document. 
PasteCommand's receiver is the Document object it issupplied upon instantiation. 
The Execute operation invokes Paste onthe receiving Document. 
OpenCommand's Execute operation is different: it prompts the userfor a document 
name, creates a corresponding Document object, adds thedocument to the receiving 
application, and opens the document. 


Design Patterns: Elements of Reusable Object-Oriented Software 
265 
Sometimes a MenuItem needs to execute a 
sequence
of commands.For example, a 
MenuItem for centering a page at normal size could beconstructed from a 
CenterDocumentCommand object and aNormalSizeCommand object. Because it's common 
to string commandstogether in this way, we can define a MacroCommand class to 
allow aMenuItem to execute an open-ended number of commands. MacroCommand isa 
concrete Command subclass that simply executes a sequence ofCommands. MacroCommand 
has no explicit receiver, because the commandsit sequences define their own 
receiver. 
In each of these examples, notice how the Command pattern decouplesthe object 
that invokes the operation from the one having theknowledge to perform it. This 
gives us a lot of flexibility indesigning our user interface. An application can 
provide both a menuand a push button interface to a feature just by making the 
menu andthe push button share an instance of the same concrete Command subclass.We 
can replace commands dynamically, which would be useful forimplementing 
context-sensitive menus. We can also support commandscripting by composing 
commands into larger ones. All of this ispossible because the object that issues 
a request only needs to knowhow to issue it; it doesn't need to know how the request 
will be carried out. 
Applicability 
Use the Command pattern when you want to 

parameterize objects by an action to perform, as MenuItem objects did above. 
You can express such parameterization in a procedural language with a 
callback
function, that is, a function that's registered somewhere to be 
called at a later point. Commands are an object-oriented replacement for 
callbacks. 



Download 4,06 Mb.

Do'stlaringiz bilan baham:
1   ...   174   175   176   177   178   179   180   181   ...   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