0% found this document useful (0 votes)
139 views

Behavioral Design Patterns

Behavioral design patterns are patterns that focus on communication between objects. The document discusses several behavioral design patterns including observer, strategy, template method, and iterator patterns. It provides descriptions of each pattern, UML class diagrams to illustrate them, and real-world examples of how each pattern is used.

Uploaded by

Manjunath Mk
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

Behavioral Design Patterns

Behavioral design patterns are patterns that focus on communication between objects. The document discusses several behavioral design patterns including observer, strategy, template method, and iterator patterns. It provides descriptions of each pattern, UML class diagrams to illustrate them, and real-world examples of how each pattern is used.

Uploaded by

Manjunath Mk
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Behavioral Design Patterns

-Manjunath M K 1MS09IS048 ISE MSRIT

Behavioral Design Patterns


Behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. One-to-many dependency model, so that when one object changes state, all its dependents are notified and updated automatically without coupling the notifying object to the objects that are notified. Deal with dynamic interactions among societies of classes and objects. Example: Button expose a clicked event that encapsulate click state, thus publish himself as an observable. Clients that are interested in this event register to it, thus becomes observers.

Behavioral Design Patterns


TYPES : Observer : Define a one-to-many dependency between objects where a state
change in one object results in all its dependents being notified and updated automatically.

Strategy : Define a family of algorithms, encapsulate each one, and make


them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Template Method : Define the skeleton of an algorithm in an operation,


deferring some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

Iterator: Provide a way to access the elements of an aggregate object


sequentially without exposing its underlying representation.

Behavioral Design Patterns - Observer Pattern


The Observer Pattern defines a one-to-many dependency between a subject object and any number of observer objects so that when the subject object changes state, all its dependents are notified and updated automatically. There may be many observers, Each observer may react differently to the same notification. The subject should be as decoupled as possible from the observers to allow observers to change independently of the subject. Two parts to the Observer Pattern Subject Observer Relationship between subject and observer is one-to-many; it needs to be decoupled to make the subject and observer independently reusable.

Behavioral Design Patterns - Observer Pattern


UML class diagram of Observer pattern

Behavioral Design Patterns - Observer Pattern


Real World Example
For example see when we subscribe for New Phone connection whenever customer is registered with that company all other departments are notified accordingly and then depending upon the state the do their jobs like do the verification of their address then if customer state is verified then dispatch the welcome kit etc.

For Example Company updates all its shareholders for any decision they make here Company is Subject and Shareholders are Observers, any change in policy of company and Company notifies all its Shareholders or Observer.

Another Example is when you have a Loan on which interest rate is subject to change and on change, Loan notifies to Newspaper or Internet media to display new loan interest rate. To implement this we have a Subject interface which contains methods for adding, removing and notifying Observers and an Observer interface which contains update(int interest) method which will be called by Subject implementation when interest rate changes.

Behavioral Design Patterns - Strategy Pattern


Make a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithmvary independently from clients that use them. Input depends on the algorithm. Calculations are based on the clients abstraction (not using clients implementation or global data). Allow you to define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Behavioral Design Patterns - Strategy Pattern


UML class diagram of strategy pattern

Behavioral Design Patterns - Strategy Pattern


Real World Example To explain the strategy in the real world, let's take the example of a software developer. If language isn't an issue I might ask a developer to write a piece of code for me to create a user interface. One developer's chosen language is Java, so he'll develop the UI with Swing. Meanwhile, the other developer decides to use C#. I don't mind, I've left the details of how to write the UI to the developers, and both have applied their own strategy. At any stage, the developer should change their strategy, choosing to use a different language if they feel it's necessary. It's all about dynamically changing behaviours.

Situation: A GUI container object wants to decide at run-time what strategy it should use to layout the GUI components it contains. Many different layout strategies are already available. Solution: Encapsulate the different layout strategies using the Strategy pattern!

Behavioral Design Patterns - Template Method Pattern


Define the skeleton of an algorithm, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm, without changing the algorithms structure.

Use Template Method Pattern To implement the invariant part of an algorithm, and leave it up to subclasses to implement the part that can vary. When common code in subclasses should be factored and localized in a common base class to avoid code duplication. To control extensions to subclasses.

Behavioral Design Patterns - Template Method Pattern


UML class diagram of Template pattern

Behavioral Design Patterns - Template Method Pattern


Real World Example The Template Method pattern is used when two or more implementations of a similar algorithm exist. In the real world templates are used all the time: for architectural plans, and throughout the engineering domain. A template plan may be defined which is then built on with further variations. For example, a basic house plan can have many variations such as adding an extensions or using a different heating system. Suppose, youre designing a Sort API that you intend to ship as a jar file to your customer. the customer would like the flexibility of adding newer or better sort implementations (specialized sorts are needed in a large scale system, applications) in their code. Often, you wouldnt want to invoke default sort implementation of Java for large data. For instance, your customer wants to sort a very large array of 0s and 1s. They would write their own sort logic yielding in-place, O(n) time-complexity, rather than use default Arrays.sort(..) with O( n log n) in time. Obviously, they dont want you to modify code for them every time. You want details of sort to be implemented in the sub classes that your customer writes

Behavioral Design Patterns - Iterator Method Pattern

An iterator pattern can be used when one class is a collection of things and would like to provide a standardized method of accessing its collection to another class.
Provides a way to access elements of a collection object sequentially without exposing its underlying representation. The iterator object takes the responsibility of traversing a collection away from collection object. This simplifies the collection interface and implementation.

Behavioral Design Patterns - Iterator Method Pattern


UML class diagram of Iterator pattern

Behavioral Design Patterns - Iterator Method Pattern


Real World Example MP3 player control is a good example of an iterator. The user doesnt mind how to view their list of songs , once they get to see them somehow . In older mp3 players ,this was done using simple forward and back buttons .With the iPod this changed to the wheel navigation concept. The iPhone moves this on further to use swipe movements . Nevertheless , the same idea is provided by all interface a way to iterate through you music collection. Some Facts About the Iterator Pattern An iterator can iterate forward and backwards. Ordering of elements is dictated by the underlying collection. Promotes the use of polymorphic iteration by writing methods that take Iterators as parameters.

Thank you

You might also like