design patterns - singleton&command
DESCRIPTION
A brief introduction to the Singleton and Command design patterns.TRANSCRIPT
Design-Patterns
(Singleton & Command)
The Command Pattern
Context
Scenario
• Object A just wants to issue requests but doesn't care about the request's Receiver or it’s actual processing.
Context
Scenario
• Object A just wants to issue requests but doesn't care about the request's Receiver or it’s actual processing.
oEncapsulate requests as objects and provide an generic interface to execute operations.
oRequests might be: o LoggedoQueuedo or support undoable operations
Problem
• An Application needs to:o issue requests to objects without knowing:
the operation being requestedthe time the request is actually processedthe receiver of the request
Solution
• Requests become first class objectso 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.
Structure
So when to use Command ?
• Decoupling of invocation and implementation oGUI-Toolkits
• Decoupling a request's invocation- and execution-time oQueuing oThread-Pools
• Remembering the operation a request has executed oUndo/Redoo Logging oTransactions
Participants and Responsibilities
1.Client creates a Command and sets its Receiver2.Invoker stores Command3.Invoker calls Execute() on Command 4.Command invokes actual Operation on its Receiver
1
2
34
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.
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
Source-Code Sample - Java
Command Interface
Concrete Command
Receiver Object
Invoker Class
Source-Code Sample - Python
Invoker Class
Command Interface is realized by using callables
Concrete Command
Receiver Object
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
The Singleton Pattern
Context
• A way to make sure there is only a single instanceof a certain object
Examples
• Syslog• Printer / Printerspooler• One logical filesystem• Global reporting system
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......
Forces
• Give us a mechanism that provides us global access to an object and controlls number of instantiation at the same time
Solution
• Provide the class the responsibility to keep trackthere is only one instance of itself and a wayto make it accessible for participants
• This is what we call the singleton pattern
Structure
Participants & Responsibilities
• Instance operation to create a new unique instance of an object
• Make sure the instance is unique
Strategies
• Creation and access of the singleton class using the same method
Consequences
• Controlled access to sole instance• Reduced name space• Permits refinement of operations and represantation (sub
classes)• Permits a controlled number of several instances
Code sample PHP