Design patterns - Singleton&Command

2,622 views

Published on

A brief introduction to the Singleton and Command design patterns.

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,622
On SlideShare
0
From Embeds
0
Number of Embeds
309
Actions
Shares
0
Downloads
36
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Design patterns - Singleton&Command

  1. 1. Design-Patterns (Singleton & Command)
  2. 2. The Command Pattern
  3. 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. 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. 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. 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.
  7. 7. Structure
  8. 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. 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. 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. 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
  12. 12. Source-Code Sample - Java Command Interface Concrete Command Receiver Object Invoker Class
  13. 13. Source-Code Sample - Python Command Interface is realized by using callables Invoker Class Concrete Command Receiver Object
  14. 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
  15. 15. The Singleton Pattern
  16. 16. Context • A way to make sure there is only a single instance of a certain object
  17. 17. Examples • Syslog • Printer / Printerspooler • One logical filesystem • Global reporting system
  18. 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. 19. Forces • Give us a mechanism that provides us global access to an object and controlls number of instantiation at the same time
  20. 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
  21. 21. Structure
  22. 22. Participants & Responsibilities • Instance operation to create a new unique instance of an object • Make sure the instance is unique
  23. 23. Strategies • Creation and access of the singleton class using the same method
  24. 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
  25. 25. Code sample PHP

×