Your SlideShare is downloading. ×
Design patterns - Singleton&Command
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Design patterns - Singleton&Command

2,028
views

Published on

A brief introduction to the Singleton and Command design patterns.

A brief introduction to the Singleton and Command design patterns.


0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,028
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
23
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Design-Patterns (Singleton & Command)
  • 2. The Command Pattern
  • 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.
  • 7. Structure
  • 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
  • 12. Source-Code Sample - Java Command Interface Concrete Command Receiver Object Invoker Class
  • 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
  • 15. The Singleton Pattern
  • 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
  • 21. Structure
  • 22. Participants & Responsibilities • Instance operation to create a new unique instance of an object • Make sure the instance is unique
  • 23. Strategies • Creation and access of the singleton class using the same method
  • 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. Code sample PHP