3. Context
Scenario
• Object A just wants to issue requests but doesn't care about
the request's Receiver or it’s actual processing.
4. Context
Scenario
• Object A just wants to issue requests but doesn't care about
the request's Receiver or it’s actual processing.
o Encapsulate requests as objects and provide an generic
interface to execute operations.
o Requests might be:
o Logged
o Queued
o or support undoable operations
5. Problem
• An Application needs to:
o issue requests to objects without knowing:
the operation being requested
the time the request is actually processed
the receiver of the request
6. Solution
• Requests become first class objects
o realized by providing a generic Command Interface which
declares an interface for executing operations.
• Each concrete Command class stores a reference to it's
Receiver as an instance variable.
8. So when to use Command ?
• Decoupling of invocation and implementation
o GUI-Toolkits
• Decoupling a request's invocation- and execution-time
o Queuing
o Thread-Pools
• Remembering the operation a request has executed
o Undo/Redo
o Logging
o Transactions
9. Participants and Responsibilities
1.Client creates a Command and sets its Receiver
2.Invoker stores Command
3.Invoker calls Execute() on Command
4.Command invokes actual Operation on its Receiver
1
2
4 3
10. Strategies
• Object-Oriented Languages such as Java
• Use external- or anonymous inner classes for implementing
command-handlers
• Declare a Command interface providing a generic interface to
execute operations.
• Make the Command-object a first class object.
11. Strategies
• Object-Oriented Languages such as Java
• Use external- or anonymous inner classes for implementing
command-handlers
• Declare a Command interface providing a generic interface to
execute operations.
• Make the Command-object a first class object.
• Functional Programming Languages such as Python
• Functions already are first class objects
• Use Closures/Callables, Eval/Exec to simplify the Command
implementation
13. Source-Code Sample - Python
Command Interface is
realized by using callables
Invoker Class
Concrete Command
Receiver Object
14. Consequences
• Command Invocation and Execution is decoupled
• Commands are first-class objects
• Complex Commands can be achieved by using Composition
• New Commands can be added easily
16. Context
• A way to make sure there is only a single instance
of a certain object
17. Examples
• Syslog
• Printer / Printerspooler
• One logical filesystem
• Global reporting system
18. Problem
• Global variables provide a way to access an objects attribute
• But, they don't prevent instantiation of mutliple instances of an
object!
• But sometimes we need to be sure there is only one......
19. Forces
• Give us a mechanism that provides us global access to an
object and controlls number of instantiation at the same time
20. Solution
• Provide the class the responsibility to keep track
there is only one instance of itself and a way
to make it accessible for participants
• This is what we call the singleton pattern
24. Consequences
• Controlled access to sole instance
• Reduced name space
• Permits refinement of operations and represantation (sub
classes)
• Permits a controlled number of several instances