Behavioral patterns
In softwareengineering, behavioral design patterns are design patterns
that identify common communication patterns between objects and
realize these patterns. By doing so, these patterns increase flexibility in
carrying out this communication.
5.
Types of BehaviorPatterns
Observer
A way of notifying change to a number of classes
Chain of responsibility
A way of passing a request between a chain of objects
Command
Encapsulate a command request as an object
Interpreter
A way to include language elements in a program
6.
Types of BehaviorPatterns
Iterator
Sequentially access the elements of a collection
Mediator
Defines simplified communication between classes
Memento
Capture and restore an object's internal state
Null Object
7.
Types of BehaviorPatterns
State
Alter an object's behavior when its state changes
Strategy
Encapsulates an algorithm inside a class
Template method
Defer the exact steps of an algorithm to a subclass
Visitor
Defines a new operation to a class without change
8.
Observer Pattern
Observer isa behavioral design pattern that lets you define a
subscription mechanism to notify multiple objects about any
events that happen to the object they’re observing.
9.
Observer Pattern
An ObserverPattern says that "just define a one-to-one
dependency so that when one object changes state, all its
dependents are notified and updated automatically".
The observer pattern is also known as Dependents or Publish-
Subscribe.
10.
Continue…
There are threerecurring themes in these patterns:
• Behavioral class patterns use inheritance to distribute behavior between
classes.
• Behavioral object patterns use object composition rather than inheritance.
• Behavioral object patterns are concerned with encapsulating behavior in an
object and delegating requests to it.
11.
Benefits and Usages
•It describes the coupling between the objects and the
observer.
• It provides the support for broadcast-type communication.
• When the change of a state in one object must be reflected
in another object without keeping the objects tight coupled.
• When the framework we writes and needs to be enhanced
in future with new observers with minimal chamges.
12.
When to useObserver Method
Use the Observer pattern in any of the following situations:
• When an abstraction has two aspects, one dependent on
the other. Encapsulating these aspects in separate objects
lets you vary and reuse them independently.
• When a change to one object requires changing others, and
you don’t know how many objects need to be changed.
• When an object should be able to notify other objects
without making assumptions about who these objects are.In
other words, you don’t want these objects tightly coupled.