Behavioral Patterns: Daniel Dinu 2013

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Behavioral Patterns

Daniel Dinu
2013
Software Engineering Daniel Dinu
2 | P a g e




Contents
Behavioral Patterns ....................................................................................................................................... 3
Iterator ...................................................................................................................................................... 3
Observer .................................................................................................................................................... 4
Command .................................................................................................................................................. 5
Strategy ..................................................................................................................................................... 6
More .............................................................................................................................................................. 7







Software Engineering Daniel Dinu
3 | P a g e

Behavioral Patterns
Iterator
Definition: Provide a way to access the elements of an aggregate object sequentially without
exposing its underlying representation.
UML Class Diagram:
+CreateIterator()
Aggregate Client
+First()
+Next()
+IsDone()
+CurrentItem()
Iterator
+CreateIterator()
ConcreteAggregate ConcreteIterator
return new ConcreteIterator(this)

Participants:
Iterator
o defines an interface for accessing and traversing elements
ConcreteIterator
o implements the Iterator interface
o keeps track of the current position in the traversal of the aggregate
Aggregate
o defines an interface for creating an Iterator object
ConcreteAggregate
o implements the Iterator creation interface to return an instance of the proper
ConcreteIterator
Software Engineering Daniel Dinu
4 | P a g e


Observer
Definition: Define a one-to-many dependency between objects so that when one object changes
state, all its dependents are notified and updated automatically.
UML Class Diagram:
+Attach(in Onbserver)
+Detach(in Observer)
+Notify()
Subject
+Update()
Observer
+GetState()
-subjectState
ConcreteSubject
+Update()
-observerState
ConcreteObserver
foreach o in observers
o.Update()
return subjectState observerState = subject.GetState()
subject
observer

Participants:
Subject
o knows its observers. Any number of Observer objects may observe a subject
o provides an interface for attaching and detaching Observer objects
ConcreteSubject
o stores state of interest to ConcreteObserver
o sends a notification to its observers when its state changes
Observer
o defines an updating interface for objects that should be notified of changes in a
subject
ConcreteObserver
o mantains a reference to a ConcreteSubject object
o stores state that should stay consistent with the subjects
o implements the Observer updating interface to keep its state with the subjects
Software Engineering Daniel Dinu
5 | P a g e

Command
Definition: Encapsulate a request as an object, thereby letting you parameterize clients with
different requests, queue or log requests, and support undoable operations.
UML Class Diagram:
Client Invoker
+Execute()
Command
+Action()
Receiver
+Execute()
-state
ConcreteCommand
receiver.Action()
receiver

Participants:
Command
o declares an interface for executing an operation
ConcreteCommand
o defines a binding between a Receiver object and an action
o implements Execute by invoking the corresponding operation(s) on Receiver
Client
o creates a ConcreteCommand object and sets its receiver
Invoker
o asks the command to carry out the request
Receiver
o knows how to perform the operations associated with carrying out the request




Software Engineering Daniel Dinu
6 | P a g e

Strategy
Definition: Define a family of algorithms, encapsulate each one, and make them
interchangeable. Strategy lets the algorithm vary independently from clients that use it.
UML Class Diagram:
+ContextInterface()
Context
+AlgorithmInterface()
Strategy
+AlgorithmInterface()
ConcreteStrategyA
+AlgorithmInterface()
ConcreteStrategyB
+AlgorithmInterface()
ConcreteStrategyC
strategy

Participants:
Strategy
o declares an interface common to all supported algorithms. Context uses this
interface to call the algorithm defined by a ConcreteStrategy
ConcreteStrategy
o implements the algorithm using the Strategy interface
Context
o is configured with a ConcreteStrategy object
o maintains a reference to a Strategy object
o may define an interface that lets Strategy access its data






Software Engineering Daniel Dinu
7 | P a g e

More
1. http://www.dofactory.com/Patterns/PatternIterator.aspx
2. http://en.wikipedia.org/wiki/Iterator_pattern
3. http://sourcemaking.com/design_patterns/iterator
4. http://www.codeproject.com/Articles/362986/Understanding-and-Implementing-the-Iterator-
Patter
5. http://www.dofactory.com/Patterns/PatternObserver.aspx
6. http://en.wikipedia.org/wiki/Observer_pattern
7. http://sourcemaking.com/design_patterns/observer
8. http://www.codeproject.com/Articles/6384/Observer-Pattern-in-NET
9. http://www.codeproject.com/Articles/38002/Observer-Design-Pattern-in-C
10. http://www.dofactory.com/Patterns/PatternCommand.aspx
11. http://en.wikipedia.org/wiki/Command_pattern
12. http://sourcemaking.com/design_patterns/command
13. http://www.codeproject.com/Articles/15207/Design-Patterns-Command-Pattern
14. http://www.dofactory.com/Patterns/PatternStrategy.aspx
15. http://en.wikipedia.org/wiki/Strategy_pattern
16. http://sourcemaking.com/design_patterns/strategy
17. http://www.codeproject.com/Articles/346873/Understanding-and-Implementing-the-Strategy-
Patter

You might also like