SlideShare ist ein Scribd-Unternehmen logo
1 von 25
Downloaden Sie, um offline zu lesen
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.



    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
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
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.
Structure
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
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
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

                    Command Interface is
                    realized by using callables


                             Invoker Class




                             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 instance
  of 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 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
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

Weitere ähnliche Inhalte

Was ist angesagt?

Model View Command Pattern
Model View Command PatternModel View Command Pattern
Model View Command PatternAkash Kava
 
Chain of Responsibility Pattern
Chain of Responsibility PatternChain of Responsibility Pattern
Chain of Responsibility PatternHüseyin Ergin
 
Vb script tutorial for qtp[1]
Vb script tutorial for qtp[1]Vb script tutorial for qtp[1]
Vb script tutorial for qtp[1]srikanthbkm
 
PowerCLI in the Enterprise Breaking the Magicians Code original
PowerCLI in the Enterprise Breaking the Magicians Code   originalPowerCLI in the Enterprise Breaking the Magicians Code   original
PowerCLI in the Enterprise Breaking the Magicians Code originaljonathanmedd
 
Intorudction into VBScript
Intorudction into VBScriptIntorudction into VBScript
Intorudction into VBScriptVitaliy Ganzha
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascriptrelay12
 
Lesson10 behavioral patterns
Lesson10 behavioral patternsLesson10 behavioral patterns
Lesson10 behavioral patternsOktJona
 
Lesson12 other behavioural patterns
Lesson12 other behavioural patternsLesson12 other behavioural patterns
Lesson12 other behavioural patternsOktJona
 
Lesson11 more behavioural patterns
Lesson11 more behavioural patternsLesson11 more behavioural patterns
Lesson11 more behavioural patternsOktJona
 
QTP VB Script Trainings
QTP VB Script TrainingsQTP VB Script Trainings
QTP VB Script TrainingsAli Imran
 
Fork and join framework
Fork and join frameworkFork and join framework
Fork and join frameworkMinh Tran
 
Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#Rizwan Ali
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in PracticeAlina Dolgikh
 
Operators used in vb.net
Operators used in vb.netOperators used in vb.net
Operators used in vb.netJaya Kumari
 
Java Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and TrendsJava Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and TrendsCarol McDonald
 
Struts2
Struts2Struts2
Struts2yuvalb
 

Was ist angesagt? (20)

Model View Command Pattern
Model View Command PatternModel View Command Pattern
Model View Command Pattern
 
Chain of Responsibility Pattern
Chain of Responsibility PatternChain of Responsibility Pattern
Chain of Responsibility Pattern
 
Vb script tutorial for qtp[1]
Vb script tutorial for qtp[1]Vb script tutorial for qtp[1]
Vb script tutorial for qtp[1]
 
PowerCLI in the Enterprise Breaking the Magicians Code original
PowerCLI in the Enterprise Breaking the Magicians Code   originalPowerCLI in the Enterprise Breaking the Magicians Code   original
PowerCLI in the Enterprise Breaking the Magicians Code original
 
React hooks
React hooksReact hooks
React hooks
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
Intorudction into VBScript
Intorudction into VBScriptIntorudction into VBScript
Intorudction into VBScript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Lesson10 behavioral patterns
Lesson10 behavioral patternsLesson10 behavioral patterns
Lesson10 behavioral patterns
 
Lesson12 other behavioural patterns
Lesson12 other behavioural patternsLesson12 other behavioural patterns
Lesson12 other behavioural patterns
 
Lesson11 more behavioural patterns
Lesson11 more behavioural patternsLesson11 more behavioural patterns
Lesson11 more behavioural patterns
 
QTP VB Script Trainings
QTP VB Script TrainingsQTP VB Script Trainings
QTP VB Script Trainings
 
Tech talk
Tech talkTech talk
Tech talk
 
Fork and join framework
Fork and join frameworkFork and join framework
Fork and join framework
 
Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in Practice
 
Operators used in vb.net
Operators used in vb.netOperators used in vb.net
Operators used in vb.net
 
Understanding Subroutines and Functions in VB6
Understanding Subroutines and Functions in VB6Understanding Subroutines and Functions in VB6
Understanding Subroutines and Functions in VB6
 
Java Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and TrendsJava Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and Trends
 
Struts2
Struts2Struts2
Struts2
 

Andere mochten auch

Client-Server-Kommunikation mit dem Command Pattern
Client-Server-Kommunikation mit dem Command PatternClient-Server-Kommunikation mit dem Command Pattern
Client-Server-Kommunikation mit dem Command Patternpgt technology scouting GmbH
 
Command and Adapter Pattern
Command and Adapter PatternCommand and Adapter Pattern
Command and Adapter PatternJonathan Simon
 
Design Patterns
Design PatternsDesign Patterns
Design Patternsppd1961
 
Senior_Environmental_Expert_CV-Beray_Kochisarli
Senior_Environmental_Expert_CV-Beray_KochisarliSenior_Environmental_Expert_CV-Beray_Kochisarli
Senior_Environmental_Expert_CV-Beray_KochisarliBeray Kochisarli
 
Fundamentals of Engineering Graphics
Fundamentals of Engineering GraphicsFundamentals of Engineering Graphics
Fundamentals of Engineering GraphicsRathnavel Ponnuswami
 

Andere mochten auch (8)

Client-Server-Kommunikation mit dem Command Pattern
Client-Server-Kommunikation mit dem Command PatternClient-Server-Kommunikation mit dem Command Pattern
Client-Server-Kommunikation mit dem Command Pattern
 
Command and Adapter Pattern
Command and Adapter PatternCommand and Adapter Pattern
Command and Adapter Pattern
 
Command Pattern
Command PatternCommand Pattern
Command Pattern
 
An Introduction to
An Introduction to An Introduction to
An Introduction to
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Senior_Environmental_Expert_CV-Beray_Kochisarli
Senior_Environmental_Expert_CV-Beray_KochisarliSenior_Environmental_Expert_CV-Beray_Kochisarli
Senior_Environmental_Expert_CV-Beray_Kochisarli
 
OpenGL Basics
OpenGL BasicsOpenGL Basics
OpenGL Basics
 
Fundamentals of Engineering Graphics
Fundamentals of Engineering GraphicsFundamentals of Engineering Graphics
Fundamentals of Engineering Graphics
 

Ähnlich wie Design patterns - Singleton&Command

Javascript classes and scoping
Javascript classes and scopingJavascript classes and scoping
Javascript classes and scopingPatrick Sheridan
 
Rethinking the debugger
Rethinking the debuggerRethinking the debugger
Rethinking the debuggerIulian Dragos
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8Heartin Jacob
 
Design patterns
Design patternsDesign patterns
Design patternsAlok Guha
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternNitin Bhide
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java scriptvivek p s
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaErick M'bwana
 
Reactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-JavaReactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-JavaKasun Indrasiri
 
Bccon use notes objects in memory and other useful
Bccon   use notes objects in memory and other usefulBccon   use notes objects in memory and other useful
Bccon use notes objects in memory and other usefulFrank van der Linden
 
Testing – With Mock Objects
Testing – With Mock ObjectsTesting – With Mock Objects
Testing – With Mock Objectsemmettwalsh
 
08 iec t1_s1_oo_ps_session_11
08 iec t1_s1_oo_ps_session_1108 iec t1_s1_oo_ps_session_11
08 iec t1_s1_oo_ps_session_11Niit Care
 
Presentation about Overthere for J-Fall 2011
Presentation about Overthere for J-Fall 2011Presentation about Overthere for J-Fall 2011
Presentation about Overthere for J-Fall 2011Vincent Partington
 

Ähnlich wie Design patterns - Singleton&Command (20)

06.1 .Net memory management
06.1 .Net memory management06.1 .Net memory management
06.1 .Net memory management
 
Javascript classes and scoping
Javascript classes and scopingJavascript classes and scoping
Javascript classes and scoping
 
Rethinking the debugger
Rethinking the debuggerRethinking the debugger
Rethinking the debugger
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
CS8392 OOP
CS8392 OOPCS8392 OOP
CS8392 OOP
 
Design patterns
Design patternsDesign patterns
Design patterns
 
oop unit1.pptx
oop unit1.pptxoop unit1.pptx
oop unit1.pptx
 
Iterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design patternIterator - a powerful but underappreciated design pattern
Iterator - a powerful but underappreciated design pattern
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java script
 
Presentation on java
Presentation  on  javaPresentation  on  java
Presentation on java
 
JS Event Loop
JS Event LoopJS Event Loop
JS Event Loop
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - Kenya
 
ppt_on_java.pptx
ppt_on_java.pptxppt_on_java.pptx
ppt_on_java.pptx
 
Reactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-JavaReactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-Java
 
Bccon use notes objects in memory and other useful
Bccon   use notes objects in memory and other usefulBccon   use notes objects in memory and other useful
Bccon use notes objects in memory and other useful
 
Testing – With Mock Objects
Testing – With Mock ObjectsTesting – With Mock Objects
Testing – With Mock Objects
 
Design_Patterns_Dr.CM.ppt
Design_Patterns_Dr.CM.pptDesign_Patterns_Dr.CM.ppt
Design_Patterns_Dr.CM.ppt
 
08 iec t1_s1_oo_ps_session_11
08 iec t1_s1_oo_ps_session_1108 iec t1_s1_oo_ps_session_11
08 iec t1_s1_oo_ps_session_11
 
Presentation about Overthere for J-Fall 2011
Presentation about Overthere for J-Fall 2011Presentation about Overthere for J-Fall 2011
Presentation about Overthere for J-Fall 2011
 
Eclipse UI automation
Eclipse UI automationEclipse UI automation
Eclipse UI automation
 

Mehr von Kai Aras

Jailbreaking iOS
Jailbreaking iOSJailbreaking iOS
Jailbreaking iOSKai Aras
 
from Realtime Operating systems to unlocking iPhones in less than 30 slides
from Realtime Operating systems to unlocking iPhones in less than 30 slidesfrom Realtime Operating systems to unlocking iPhones in less than 30 slides
from Realtime Operating systems to unlocking iPhones in less than 30 slidesKai Aras
 
OpenAmi - a short Introduction
OpenAmi - a short IntroductionOpenAmi - a short Introduction
OpenAmi - a short IntroductionKai Aras
 
Projektdokumentation Kai Aras Ss08
Projektdokumentation Kai Aras Ss08Projektdokumentation Kai Aras Ss08
Projektdokumentation Kai Aras Ss08Kai Aras
 
Sounddesign - Pi - Kai Aras - WS08/09
Sounddesign - Pi - Kai Aras - WS08/09Sounddesign - Pi - Kai Aras - WS08/09
Sounddesign - Pi - Kai Aras - WS08/09Kai Aras
 
a quick Introduction to PyPy
a quick Introduction to PyPya quick Introduction to PyPy
a quick Introduction to PyPyKai Aras
 
Virtual Reality - Tracking Applications
Virtual Reality - Tracking ApplicationsVirtual Reality - Tracking Applications
Virtual Reality - Tracking ApplicationsKai Aras
 

Mehr von Kai Aras (8)

Jailbreaking iOS
Jailbreaking iOSJailbreaking iOS
Jailbreaking iOS
 
from Realtime Operating systems to unlocking iPhones in less than 30 slides
from Realtime Operating systems to unlocking iPhones in less than 30 slidesfrom Realtime Operating systems to unlocking iPhones in less than 30 slides
from Realtime Operating systems to unlocking iPhones in less than 30 slides
 
OpenAmi - a short Introduction
OpenAmi - a short IntroductionOpenAmi - a short Introduction
OpenAmi - a short Introduction
 
Projektdokumentation Kai Aras Ss08
Projektdokumentation Kai Aras Ss08Projektdokumentation Kai Aras Ss08
Projektdokumentation Kai Aras Ss08
 
Sounddesign - Pi - Kai Aras - WS08/09
Sounddesign - Pi - Kai Aras - WS08/09Sounddesign - Pi - Kai Aras - WS08/09
Sounddesign - Pi - Kai Aras - WS08/09
 
a quick Introduction to PyPy
a quick Introduction to PyPya quick Introduction to PyPy
a quick Introduction to PyPy
 
Algorythm
AlgorythmAlgorythm
Algorythm
 
Virtual Reality - Tracking Applications
Virtual Reality - Tracking ApplicationsVirtual Reality - Tracking Applications
Virtual Reality - Tracking Applications
 

Design patterns - Singleton&Command

  • 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
  • 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
  • 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
  • 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