<s>
In	O
object-oriented	B-Language
programming	I-Language
,	O
the	B-Language
command	I-Language
pattern	I-Language
is	O
a	O
behavioral	O
design	O
pattern	O
in	O
which	O
an	O
object	O
is	O
used	O
to	O
encapsulate	B-Application
all	O
information	O
needed	O
to	O
perform	O
an	O
action	O
or	O
trigger	O
an	O
event	O
at	O
a	O
later	O
time	O
.	O
</s>
<s>
Four	O
terms	O
always	O
associated	O
with	O
the	B-Language
command	I-Language
pattern	I-Language
are	O
command	O
,	O
receiver	O
,	O
invoker	O
and	O
client	O
.	O
</s>
<s>
The	O
central	O
ideas	O
of	O
this	O
design	O
pattern	O
closely	O
mirror	O
the	O
semantics	O
of	O
first-class	B-Application
functions	I-Application
and	O
higher-order	B-Language
functions	I-Language
in	O
functional	B-Language
programming	I-Language
languages	I-Language
.	O
</s>
<s>
Specifically	O
,	O
the	O
invoker	O
object	O
is	O
a	O
higher-order	B-Language
function	I-Language
of	O
which	O
the	O
command	O
object	O
is	O
a	O
first-class	O
argument	O
.	O
</s>
<s>
design	O
pattern	O
is	O
one	O
of	O
the	O
twenty-three	O
well-known	O
GoF	O
design	O
patterns	O
that	O
describe	O
how	O
to	O
solve	O
recurring	O
design	O
problems	O
to	O
design	O
flexible	O
and	O
reusable	O
object-oriented	B-Language
software	O
,	O
that	O
is	O
,	O
objects	O
that	O
are	O
easier	O
to	O
implement	O
,	O
change	O
,	O
test	O
,	O
and	O
reuse	O
.	O
</s>
<s>
Define	O
separate	O
(	O
command	O
)	O
objects	O
that	O
encapsulate	B-Application
a	O
request	O
.	O
</s>
<s>
See	O
also	O
the	O
UML	B-Language
class	O
and	O
sequence	O
diagram	O
below	O
.	O
</s>
<s>
In	O
the	O
above	O
UML	B-Language
class	O
diagram	O
,	O
the	O
Invoker	O
class	O
does	O
n't	O
implement	O
a	O
request	O
directly	O
.	O
</s>
<s>
GUI	O
buttons	O
and	O
menu	O
items	O
In	O
Swing	B-Language
and	O
Borland	B-Language
Delphi	I-Language
programming	O
,	O
an	O
is	O
a	O
command	O
object	O
.	O
</s>
<s>
Mobile	B-Operating_System
code	I-Operating_System
Using	O
languages	O
such	O
as	O
Java	O
where	O
code	O
can	O
be	O
streamed/slurped	O
from	O
one	O
location	O
to	O
another	O
via	O
URLClassloaders	O
and	O
Codebases	O
the	O
commands	O
can	O
enable	O
new	O
behavior	O
to	O
be	O
delivered	O
to	O
remote	O
locations	O
(	O
EJB	O
Command	O
,	O
Master	O
Worker	O
)	O
.	O
</s>
<s>
Multi-level	O
undo	B-Application
If	O
all	O
user	O
actions	O
in	O
a	O
program	O
are	O
implemented	O
as	O
command	O
objects	O
,	O
the	O
program	O
can	O
keep	O
a	O
stack	O
of	O
the	O
most	O
recently	O
executed	O
commands	O
.	O
</s>
<s>
When	O
the	O
user	O
wants	O
to	O
undo	B-Application
a	O
command	O
,	O
the	O
program	O
simply	O
pops	O
the	O
most	O
recent	O
command	O
object	O
and	O
executes	O
its	O
method	O
.	O
</s>
<s>
Thread	B-Operating_System
pools	I-Operating_System
A	O
typical	O
,	O
general-purpose	O
thread	B-Operating_System
pool	I-Operating_System
class	O
might	O
have	O
a	O
public	O
method	O
that	O
adds	O
a	O
work	O
item	O
to	O
an	O
internal	O
queue	O
of	O
tasks	O
waiting	O
to	O
be	O
done	O
.	O
</s>
<s>
Typically	O
these	O
objects	O
implement	O
a	O
common	O
interface	O
such	O
as	O
that	O
allows	O
the	O
thread	B-Operating_System
pool	I-Operating_System
to	O
execute	O
the	O
command	O
even	O
though	O
the	O
thread	B-Operating_System
pool	I-Operating_System
class	O
itself	O
was	O
written	O
without	O
any	O
knowledge	O
of	O
the	O
specific	O
tasks	O
for	O
which	O
it	O
would	O
be	O
used	O
.	O
</s>
<s>
Transactional	B-General_Concept
behavior	O
Similar	O
to	O
undo	B-Application
,	O
a	O
database	O
engine	O
or	O
software	O
installer	O
may	O
keep	O
a	O
list	O
of	O
operations	O
that	O
have	O
been	O
or	O
will	O
be	O
performed	O
.	O
</s>
<s>
The	O
terminology	O
used	O
to	O
describe	O
command	B-Language
pattern	I-Language
implementations	O
is	O
not	O
consistent	O
and	O
can	O
therefore	O
be	O
confusing	O
.	O
</s>
<s>
This	O
is	O
the	O
result	O
of	O
ambiguity	O
,	O
the	O
use	O
of	O
synonyms	B-Application
,	O
and	O
implementations	O
that	O
may	O
obscure	O
the	O
original	O
pattern	O
by	O
going	O
well	O
beyond	O
it	O
.	O
</s>
<s>
If	O
the	O
former	O
command	O
is	O
added	O
twice	O
to	O
an	O
undo	B-Application
stack	O
,	O
both	O
items	O
on	O
the	O
stack	O
refer	O
to	O
the	O
same	O
command	O
instance	O
.	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
if	O
the	O
latter	O
commands	O
are	O
added	O
to	O
an	O
undo	B-Application
stack	O
,	O
the	O
stack	O
refers	O
to	O
two	O
separate	O
objects	O
.	O
</s>
<s>
For	O
example	O
,	O
to	O
undo	B-Application
a	O
delete	O
selection	O
command	O
,	O
the	O
object	O
may	O
contain	O
a	O
copy	O
of	O
the	O
deleted	O
text	O
so	O
that	O
it	O
can	O
be	O
re-inserted	O
,	O
if	O
the	O
delete	O
selection	O
command	O
must	O
be	O
undone	O
.	O
</s>
<s>
However	O
,	O
in	O
Microsoft	O
's	O
Windows	B-Device
Presentation	I-Device
Foundation	I-Device
a	O
command	O
is	O
considered	O
to	O
have	O
been	O
executed	O
when	O
the	O
command	O
's	O
execute	O
method	O
has	O
been	O
invoked	O
,	O
but	O
that	O
does	O
not	O
necessarily	O
mean	O
that	O
the	O
application	O
code	O
has	O
run	O
.	O
</s>
<s>
Synonyms	B-Application
and	O
homonyms	O
.	O
</s>
<s>
Command	O
Manager	O
,	O
Undo	B-Application
Manager	O
,	O
Scheduler	O
,	O
Queue	O
,	O
Dispatcher	O
,	O
Invoker	O
:	O
an	O
object	O
that	O
puts	O
command/event	O
objects	O
on	O
an	O
undo	B-Application
stack	O
or	O
redo	B-Application
stack	O
,	O
or	O
that	O
holds	O
on	O
to	O
command/event	O
objects	O
until	O
other	O
objects	O
are	O
ready	O
to	O
act	O
on	O
them	O
,	O
or	O
that	O
routes	O
the	O
command/event	O
objects	O
to	O
the	O
appropriate	O
receiver/target	O
object	O
or	O
handler	O
code	O
.	O
</s>
<s>
Implementations	O
that	O
go	O
well	O
beyond	O
the	O
original	O
command	B-Language
pattern	I-Language
.	O
</s>
<s>
Microsoft	O
's	O
(	O
WPF	O
)	O
,	O
introduces	O
routed	O
commands	O
,	O
which	O
combine	O
the	B-Language
command	I-Language
pattern	I-Language
with	O
event	O
processing	O
.	O
</s>
<s>
A	O
benefit	O
of	O
this	O
particular	O
implementation	O
of	O
the	B-Language
command	I-Language
pattern	I-Language
is	O
that	O
the	O
switch	O
can	O
be	O
used	O
with	O
any	O
device	O
,	O
not	O
just	O
a	O
light	O
.	O
</s>
