design patterns - singleton&command

25
Design-Patterns (Singleton & Command)

Upload: kai-aras

Post on 06-May-2015

2.100 views

Category:

Documents


0 download

DESCRIPTION

A brief introduction to the Singleton and Command design patterns.

TRANSCRIPT

Page 1: Design patterns - Singleton&Command

Design-Patterns

(Singleton & Command)

Page 2: Design patterns - Singleton&Command

The Command Pattern

Page 3: Design patterns - Singleton&Command

Context

Scenario

• Object A just wants to issue requests but doesn't care about the request's Receiver or it’s actual processing.

Page 4: Design patterns - Singleton&Command

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

Page 5: Design patterns - Singleton&Command

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

Page 6: Design patterns - Singleton&Command

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.

Page 7: Design patterns - Singleton&Command

Structure

Page 8: Design patterns - Singleton&Command

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

Page 9: Design patterns - Singleton&Command

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

Page 10: Design patterns - Singleton&Command

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.

Page 11: Design patterns - Singleton&Command

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

Page 12: Design patterns - Singleton&Command

Source-Code Sample - Java

Command Interface

Concrete Command

Receiver Object

Invoker Class

Page 13: Design patterns - Singleton&Command

Source-Code Sample - Python

Invoker Class

Command Interface is realized by using callables

Concrete Command

Receiver Object

Page 14: Design patterns - Singleton&Command

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

Page 15: Design patterns - Singleton&Command

The Singleton Pattern

Page 16: Design patterns - Singleton&Command

Context

• A way to make sure there is only a single instanceof a certain object

Page 17: Design patterns - Singleton&Command

Examples

• Syslog• Printer / Printerspooler• One logical filesystem• Global reporting system

Page 18: Design patterns - Singleton&Command

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

Page 19: Design patterns - Singleton&Command

Forces

• Give us a mechanism that provides us global access to an object and controlls number of instantiation at the same time

Page 20: Design patterns - Singleton&Command

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

Page 21: Design patterns - Singleton&Command

Structure

Page 22: Design patterns - Singleton&Command

Participants & Responsibilities

• Instance operation to create a new unique instance of an object

• Make sure the instance is unique

Page 23: Design patterns - Singleton&Command

Strategies

• Creation and access of the singleton class using the same method

Page 24: Design patterns - Singleton&Command

Consequences

• Controlled access to sole instance• Reduced name space• Permits refinement of operations and represantation (sub

classes)• Permits a controlled number of several instances

Page 25: Design patterns - Singleton&Command

Code sample PHP