BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
(c) 2009 University of California, Irvine – André van der Hoek1June 13, 2015 – 21:42:16 Informatics 122 Software Design II Lecture 8 André van der Hoek.
Nov, 1, Design Patterns PROBLEM CONTEXT SOLUTION A design pattern documents a proven solution to a recurring problem in a specific context and its.
Software Design & Documentation – Design Pattern: Command Design Pattern: Command Christopher Lacey September 15, 2003.
Dept. of Computer Engineering, Amirkabir University of Tech. 1 Design Patterns Dr. Noorhosseini Introduction.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
Design Patterns CS is not simply about programming
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
(c) 2010 University of California, Irvine – André van der Hoek1June 29, 2015 – 08:55:05 Informatics 122 Software Design II Lecture 8 André van der Hoek.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
Vrije Universiteit amsterdamPostacademische Cursus Informatie Technologie Idioms and Patterns polymorphism -- inheritance and delegation idioms -- realizing.
Design Patterns Discussion of pages: xi-11 Sections: Preface, Forward, Chapter
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
© Spiros Mancoridis 27/09/ Software Design Topics in Object-Oriented Design Patterns Material drawn from [Gamma95] and [Coplien95] Revised and augmented.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VII Observer, Command, and Memento.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
ECE450S – Software Engineering II
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem.
Proxy, Observer, Symbolic Links Rebecca Chernoff.
Behavioural Design Patterns Quote du jour: ECE450S – Software Engineering II I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison.
DESIGN PATTERNS -BEHAVIORAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Design Pattern Catalog - Page L3-1 PS95&96-MEF-L10-1 Dr. M.E. Fayad Creationa.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Design Patterns Introduction
Using Software Design Patterns Bill Anderson. About me Fox developer since 1987 Fox developer since 1987 Program Director, Los Angeles Visual Foxpro Developers.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 31. Review Creational Design Patterns – Singleton Pattern – Builder Pattern.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Chapter 8 Object Design Reuse and Patterns. More Patterns Abstract Factory: Provide manufacturer independence Builder: Hide a complex creation process.
CSE 332: Design Patterns (Part II) Last Time: Part I, Familiar Design Patterns We’ve looked at patterns related to course material –Singleton: share a.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
Overview of Behavioral Patterns ©SoftMoore ConsultingSlide 1.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Jim Fawcett CSE776 – Design Patterns Summer 2006
Design Patterns: MORE Examples
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Software Design Patterns
Jim Fawcett CSE776 – Design Patterns Fall 2016
Structural Patterns Structural patterns control the relationships between large portions of your applications. Structural patterns affect applications.
Design Patterns Lecture part 2.
Introduction to Design Patterns
Behavioral Design Patterns
Software Design and Architecture
object oriented Principles of software design
Informatics 122 Software Design II
Strategy Design Pattern
Informatics 122 Software Design II
Presentation transcript:

BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada

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

3 How patterns arise Problem Context Solution Benefits Related Patterns Consequences Forces

4 Description of Design Pattern Pattern name and classification Intent Motivation Applicability Structure (diagram) Sample Code Known Uses

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

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.

7 Design Patterns Catalog Purpose CreationalStructuralBehavioral ScopeClass Factory MethodAdapterInterpreter Template Method Object Abstract Factory Builder Prototype Singleton Adapter Bridge Composite Decorator Facade Flyweight Proxy Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor

8

9 Design Patterns PatternIntent 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. CommandEncapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations InterpreterGiven a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language. IteratorProvide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. MediatorDefine 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.

10 Design Patterns PatternIntent MementoWithout violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later. ObserverDefine a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. StateAllow an object to alter its behavior when its internal state changes. The object will appear to change its class. StrategyDefine 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. VisitorRepresent 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.

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.

12 Template Pattern - Structure

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.

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.

15 Template Method Example

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.

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.

18 Command Pattern (Class) – Structure

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.

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.

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. ‒.

22 Observer Pattern - Structure

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.

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.

25 Observer Pattern Example

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.

27 Strategy Pattern - Structure

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.

29 Strategy Pattern– Related Patterns Flyweight  Strategy objects often make good flyweights.

30 Mindset on Patterns

31 Design Pattern Questions

32 Sanjeeb Kumar Nanda