behavioral patterns 13-sep-2012 presenters sanjeeb kumar nanda & shankar gogada
TRANSCRIPT
![Page 1: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/1.jpg)
BEHAVIORAL PATTERNS
13-Sep-2012
PresentersSanjeeb Kumar Nanda & Shankar Gogada
![Page 2: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/2.jpg)
2
What is a pattern?
• Pattern is a recurring solution to a standard problem
• Each Pattern describes a problem which occurs over and over again in our environment and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice
• Not code reuse
‒ Instead, solution/strategy reuse
‒ Sometimes, interface reuse
![Page 3: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/3.jpg)
3
How patterns arise
ProblemProblem
Context
SolutionSolution
Benefits
Related Patterns
Consequences
Forces
![Page 4: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/4.jpg)
4
Description of Design Pattern
• Pattern name and classification
• Intent
• Motivation
• Applicability
• Structure (diagram)
• Sample Code
• Known Uses
![Page 5: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/5.jpg)
5
Four essential elements of pattern
• Pattern Name Naming a pattern immediately increases our design vocabulary. It let's
design at a higher level of abstraction.
• Problem It explains the problem and its context. It might describe specific
design problems such as how to represent algorithms as objects.
• Solution The solution describes the elements that make up the design, their
relationships, responsibilities, and collaborations.
• Consequences The consequences are the results and trade-offs of applying the
pattern
![Page 6: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/6.jpg)
6
Pattern Categories
Categories are based on granularity and level of abstraction
• Creational
‒ It helps in the process of object creation
‒ Factory Method, Abstract Factory, Singleton., etc.
• Structural
‒ Defines composition of objects and classes
‒ Uses inheritance to compose classes, while the Structural object patterns describe ways to assemble objects.
‒ Adapter, Façade, Proxy., etc.
• Behavioral
‒ Characterizes the way in which classes or objects interact
‒ The Behavioral class patterns use inheritance to describe algorithms and flow of control, whereas the Behavioral object patterns describe how a group of objects
co-operate to perform a task that no single object can carry out alone .
‒ Observer, Iterator, Visitor, etc.
![Page 7: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/7.jpg)
7
Design Patterns Catalog
Purpose
Creational Structural Behavioral
Scope Class Factory Method Adapter InterpreterTemplate Method
Object Abstract Factory Builder Prototype Singleton
Adapter BridgeComposite Decorator FacadeFlyweight Proxy
Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor
![Page 8: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/8.jpg)
8
![Page 9: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/9.jpg)
9
Design Patterns
Pattern Intent
Chain of Responsibility
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
Command Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations
Interpreter Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
Iterator Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Mediator Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
![Page 10: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/10.jpg)
10
Design Patterns
Pattern Intent
Memento Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.
Observer Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
State Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
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.
Visitor Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
![Page 11: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/11.jpg)
11
Template Method Pattern
• Intent
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.
• Frequency of use : Medium
• Applicability
To implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary.
When common behavior among subclasses should be factored and localized in a common class to avoid code duplication. Identify the differences in the existing code and then separate the differences into new operations. Finally, you replace the differing code with a template method that calls one of these new operations.
To control subclasses extensions, we can define a template method that calls "hook" operations at specific points, thereby permitting extensions only at those points.
![Page 12: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/12.jpg)
12
Template Pattern - Structure
![Page 13: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/13.jpg)
13
Template Method Pattern - Participants
• AbstractClass Defines the interface for objects that can have responsibilities added
to them dynamically.
Defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm.
Implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.
• ConcreteClass
Implements the primitive operations to carry out subclass-specific steps of the algorithm.
![Page 14: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/14.jpg)
14
Template Method Pattern - Related Patterns
• Factory Methods
Are often called by template methods.
• Strategy
Template methods use inheritance to vary part of an algorithm. Strategies use delegation to vary the entire algorithm.
![Page 15: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/15.jpg)
15
Template Method Example
![Page 16: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/16.jpg)
16
Command Pattern
• Intent
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
• Also Known As Action, Transaction
• Frequency of use: Medium - high
• Applicability
Use the Adapter pattern when
parameterize objects by an action to perform, you can express such parameterization in a procedural language with a callback function, that is, a function that's registered somewhere to be called at a later point. Commands are an object-oriented replacement for callbacks .
specify, queue, and execute requests at different times. A Command object can have a lifetime independent of the original request. If the receiver of a request can be represented in an address space-independent way, then you can transfer a command object for the request to a different process and fulfill the request there.
![Page 17: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/17.jpg)
17
Command Pattern
• Applicability (Contd …)
support undo. The Command's Execute operation can store state for reversing its effects in the command itself. The Command interface must have an added Un-execute operation that reverses the effects of a previous call to Execute. Executed commands are stored in a history list. Unlimited-level undo and redo is achieved by traversing this list backwards and forwards calling Un-execute and Execute, respectively.
support logging changes so that they can be reapplied in case of a system crash. By augmenting the Command interface with load and store operations, you can keep a persistent log of changes. Recovering from a crash involves reloading logged commands from disk and re-executing them with the Execute operation.
structure a system around high-level operations built on primitives operations. Such a structure is common in information systems that support transactions. A transaction encapsulates a set of changes to data. The Command pattern offers a way to model transactions. Commands have a common interface, letting you invoke all transactions the same way. The pattern also makes it easy to extend the system with new transactions.
![Page 18: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/18.jpg)
18
Command Pattern (Class) – Structure
![Page 19: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/19.jpg)
19
Command Pattern - Participants
• Command
declares an interface for executing an operation.
• ConcreteCommand
defines a binding between a Receiver object and an action.
implements Execute by invoking the corresponding operation(s) on Receiver.
• Client
creates a ConcreteCommand object and sets its receiver.
• Invoker
asks the command to carry out the request.
• Receiver
knows how to perform the operations associated with carrying out a request. Any class may serve as a Receiver.
![Page 20: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/20.jpg)
20
Command Pattern - Related Patterns
• Composite
A Composite can be used to implement Macro Commands.
• Memento
A Memento can keep state the command requires to undo its effect.
• Prototype
A command that must be copied before being placed on the history list acts as a Prototype.
![Page 21: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/21.jpg)
21
Observer Pattern
Intent Define a one-to-many dependency between objects so that when one
object changes state, all its dependents are notified and updated automatically .
Frequency of use: high
• Also Known As
Dependents, Publish-Subscribe
Applicability 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.
‒ .
![Page 22: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/22.jpg)
22
Observer Pattern - Structure
![Page 23: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/23.jpg)
23
Observer Pattern - Participants
• Subject
knows its observers. Any number of Observer objects may observe a subject.
provides an interface for attaching and detaching Observer objects.
• Observer
defines an updating interface for objects that should be notified of changes in a subject.
• ConcreteSubject
stores state of interest to ConcreteObserver objects.
sends a notification to its observers when its state changes.
• ConcreteObserver
maintains a reference to a ConcreteSubject object.
stores state that should stay consistent with the subject's.
implements the Observer updating interface to keep its state consistent with the subject's.
![Page 24: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/24.jpg)
24
Observer Pattern - Related Patterns
• Mediator
By encapsulating complex update semantics, the mediator can act between subjects and observers.
• Singleton
The Mediator may use the Singleton pattern to make it unique and globally accessible.
![Page 25: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/25.jpg)
25
Observer Pattern Example
![Page 26: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/26.jpg)
26
Strategy Pattern
Intent
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Frequency of use: Medium-High
• Also Known As : Policy
Applicability many related classes differ only in their behavior. Strategies provide a
way to configure a class with one of many behaviors.
Strategies can be used when these variants are implemented as a class hierarchy of algorithms .
an algorithm uses data that clients shouldn't know about. Use the Strategy pattern to avoid exposing complex, algorithm-specific data structures.
a class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditionals, move related conditional branches into their own Strategy class.
![Page 27: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/27.jpg)
27
Strategy Pattern - Structure
![Page 28: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/28.jpg)
28
Strategy Pattern - Participants
• Strategy
declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy .
• ConcreteStrategy
implements the algorithm using the Strategy interface.
• Context
is configured with a ConcreteStrategy object.
maintains a reference to a Strategy object.
may define an interface that lets Strategy access its data.
![Page 29: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/29.jpg)
29
Strategy Pattern– Related Patterns
• Flyweight
Strategy objects often make good flyweights.
![Page 30: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/30.jpg)
30
Mindset on Patterns
![Page 31: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/31.jpg)
31
Design Pattern Questions
![Page 32: BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada](https://reader035.vdocuments.mx/reader035/viewer/2022062410/5697bfdd1a28abf838cb1c90/html5/thumbnails/32.jpg)
32
Sanjeeb Kumar Nanda