Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Command pattern
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Uses== ; GUI buttons and menu items: In [[Swing (Java)|Swing]] and [[Borland]] [[Delphi (software)|Delphi]] programming, an {{Javadoc:SE|javax/swing|Action|module=java.desktop}} is a command object. In addition to the ability to perform the desired command, an {{Mono|Action}} may have an associated icon, [[keyboard shortcut]], [[tooltip]] text, and so on. A toolbar button or menu item component may be completely initialized using only the {{Mono|Action}} object. ; [[Macro (computer science)|Macro]] recording: If all user actions are represented by command objects, a program can record a sequence of actions simply by keeping a list of the command objects as they are executed. It can then "play back" the same actions by executing the same command objects again in sequence. If the program embeds a scripting engine, each command object can implement a {{Mono|toScript()}} method, and user actions can then be easily recorded as scripts. ; [[Code mobility|Mobile code]]: Using languages such as Java where code can be streamed/slurped from one location to another via URLClassloaders and Codebases the commands can enable new behavior to be delivered to remote locations (EJB Command, Master Worker). ; Multi-level [[undo]]: If all user actions in a program are implemented as command objects, the program can keep a stack of the most recently executed commands. When the user wants to undo a command, the program simply pops the most recent command object and executes its {{Mono|undo()}} method. ; Networking: It is possible to send whole command objects across the network to be executed on the other machines, for example player actions in computer games. ; Parallel processing: Where the commands are written as tasks to a shared resource and executed by many threads in parallel (possibly on remote machines; this variant is often referred to as the Master/Worker pattern) ; [[Progress bar]]s: Suppose a program has a sequence of commands that it executes in order. If each command object has a {{Mono|getEstimatedDuration()}} method, the program can easily estimate the total duration. It can show a progress bar that meaningfully reflects how close the program is to completing all the tasks. ; [[Thread pool]]s: A typical, general-purpose thread pool class might have a public {{Mono|addTask()}} method that adds a work item to an internal queue of tasks waiting to be done. It maintains a pool of threads that execute commands from the queue. The items in the queue are command objects. Typically these objects implement a common interface such as {{Mono|java.lang.Runnable}} that allows the thread pool to execute the command even though the thread pool class itself was written without any knowledge of the specific tasks for which it would be used. ; [[database transaction|Transactional]] behavior: Similar to undo, a [[database engine]] or software installer may keep a list of operations that have been or will be performed. Should one of them fail, all others can be reversed or discarded (usually called ''rollback''). For example, if two database tables that refer to each other must be updated, and the second update fails, the transaction can be rolled back, so that the first table does not now contain an invalid reference. ; [[wizard (software)|Wizard]]s: Often a wizard presents several pages of configuration for a single action that happens only when the user clicks the "Finish" button on the last page. In these cases, a natural way to separate user interface code from application code is to implement the wizard using a command object. The command object is created when the wizard is first displayed. Each wizard page stores its GUI changes in the command object, so the object is populated as the user progresses. "Finish" simply triggers a call to {{Mono|execute()}}. This way, the command class will work.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)