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

Like this? Share it with your network

Share

Design patterns - Singleton&Command

on

  • 2,344 views

A brief introduction to the Singleton and Command design patterns.

A brief introduction to the Singleton and Command design patterns.

Statistics

Views

Total Views
2,344
Views on SlideShare
2,062
Embed Views
282

Actions

Likes
0
Downloads
22
Comments
0

3 Embeds 282

http://ka010.wordpress.com 254
http://ka010.soup.io 25
http://www.soup.io 3

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Design patterns - Singleton&Command Presentation 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