Design Pattern
Behavioral patterns
Computer Science Faculty
Network Engineering Department
Slide:- 03
Year:- 2024
Contents
• Behavioral patterns
• Types of Behavior Patterns
• Observer Pattern
• When to use Observer Method
Behavioral patterns
In software engineering, 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.
Types of Behavior Patterns
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
Types of Behavior Patterns
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
Types of Behavior Patterns
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
Observer Pattern
Observer is a 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.
Observer Pattern
An Observer Pattern 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.
Continue…
There are three recurring 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.
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.
When to use Observer 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.
UML for Observer Pattern

Design Pattern Slide 03aaaaaaaaaaaaaaaaaaa.pptx

  • 1.
    Design Pattern Behavioral patterns ComputerScience Faculty Network Engineering Department Slide:- 03 Year:- 2024
  • 2.
    Contents • Behavioral patterns •Types of Behavior Patterns • Observer Pattern • When to use Observer Method
  • 3.
    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.
  • 13.