Behavioral Design Patterns

Slides:



Advertisements
Similar presentations
Welcome to. Who am I? A better way to code Design Patterns ???  What are design patterns?  How many are there?  How do I use them?  When do I use.
Advertisements

Computer Science 313 – Advanced Programming Topics.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Design Patterns In OPM Presented by: Galia Shlezinger Instructors: Prop. Dov Dori, Dr. Iris Berger.
Software Design & Documentation – Design Pattern: Command Design Pattern: Command Christopher Lacey September 15, 2003.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
GoF Sections 2.7 – 2.9 More Fun with Lexi. Lexi Document Editor Lexi tasks discussed:  Document structure  Formatting  Embellishing the user interface.
Reuse Activities Selecting Design Patterns and Components
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
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.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
More Design Patterns In Delphi Jim Cooper Falafel Software Session Code: D3.03 Track: Delphi.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
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.
Lexi case study (Part 2) Presentation by Matt Deckard.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Object Oriented Software Development
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
CS 210 Final Review November 28, CS 210 Adapter Pattern.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
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.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
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.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
Design Patterns: Behavioral Design Patterns General and reusable solutions to common problems in software design Software University
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.
Jim Fawcett CSE776 – Design Patterns Summer 2006
Design Patterns: MORE Examples
Abstract Factory Pattern
Chapter 10 Design Patterns.
Software Design Patterns
Design Patterns Lecture part 2.
Introduction to Design Patterns
Inheritance and Polymorphism
Observer Design Pattern
Object-Oriented Programming
Instructor: Dr. Hany H. Ammar
Week 4 Object-Oriented Programming (1): Inheritance
object oriented Principles of software design
Abstract Factory Pattern
Decorator Design Pattern
Strategy Design Pattern
MSIS 670 Object-Oriented Software Engineering
DESIGN PATTERNS : Strategy Pattern
Structural Patterns: Adapter and Bridge
Informatics 122 Software Design II
Presentation transcript:

Behavioral Design Patterns

Template Design Pattern Have a generic algorithm or task to perform Want the code to be reusable But the details differ Create an abstract class that performs the task Abstract functions code stuff that changes Subclass behaviour of these things using polymorphism

Or even more detailed templating

Template Pattern Advantages Separates what changes from what does not Bulk of unchanging code can be easily reused Detailed differences are all that the subclasses worry about Much that might change can also be a no-op This pattern is being used every time in Java you extend a class public class MyFrame extends Jframe public class MyApplet extends Applet

Template Pattern Definition The Template Method defines the skeleton of an operation either not specifying some steps to be performed, or giving them a generic default behaviour The Template Method lets subclasses redefine these steps of the algorithm without otherwise changing the behaviour or intent of the generic algorithm

Strategy Design Pattern In real life objects can have many behaviours Need to be able to mix and match behaviours Across instances of the same class Across different subclasses of a class Across very different types of class Behaviour might dynamically change Hard to do with the template design pattern Program to interface not an implementation

Bad approaches Put behaviour is common superclass This forces behaviour on all subclasses Then use polymorphism to alter behaviour Massive amount of code duplication Risk that fail to do this in all necessary cases Use multiple inheritance Causes an explosion in the types of classes Need a class for all combinations of behaviour Compile time solutions don’t permit change!!

Solution Separate the behaviour from the object Define generic “do behaviour” as an interface Implement actual behaviours as subclasses of this Logistically think object has-a behaviour not is-an object with consequently this behaviour In original objects needing a behaviour Point at behaviour with behavioural interface In construction assign actual behaviour to object

Avoids explosion in classes Ducks have 8 types of quack and 8 styles of flying Need 64 types of duck represent this But need only 1 type of Duck, 8 types of quack, 8 styles of flying Ie. 17 classes using the strategy design pattern Easy to create new types of quack or flying Easy to add further types of behaviour Architecture grows linearly, not exponentially

Other Advantages Assignment of behaviour now done at run time Can easily increase types of behaviour with no new class definitions Code needs to know how to invoke behaviour but not how actual behaviour is implemented Behaviours can be shared and reused Behaviour can be dynamically changed Can modify object without touching behaviour Can modify behaviour without impacting object

Formal Description The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and (by using a common interface) makes them interchangeable. This lets the algorithm vary independently of the clients that use it.

The Command Design Pattern We can view any behaviour as an action We can unify different behaviour interfaces execute() and undo() We can combine sequences of behaviour Arrays of pointer to successive actions We can define a single behaviour from many Behaviour object that performs sequence of behaviours (these can be also nested) We can easily implement do, undo, redo Simply save in a list the actions as performed

Typical use might be in a menu

If it has one Strategy or does the work itself

More advantages We can create no-op if no action required Ducks can now quack when they fly A concrete command may itself be targeted at different receivers Represent the receiver within the command object using the strategy design pattern We can queue up future commands Using event driven message pump We can cache commands to disk and repeat If want to be able to recover from backup

One disadvantage Can’t easily parameterize an action Must add any parameterization to the command as part of its construction But could have a common interface with an object passed containing parameters or actual parameters to execute if can agree on what these should always be

Command Pattern Definition The Command Pattern encapsulates a request as an object (reification), letting you parameterize other objects with different requests, queue or log requests, and supports repeat and undoable operations.

State Design Pattern A specialisation of the strategy pattern Consider modelling problem as state changes Define a complete list of actions interface Represent each state by a class having interface A class defines ∃ state ∀ actions Current state is referenced using strategy pattern

Object pool Strategy Delegation Change state

Advantages Implemented within a object the object appears to have behaviour predicated on state But pointers to this object do not change Behaviour in a given state encapsulated within a single class Less risk of failing to handle an action when in an arbitrary state Easy to change or extend the state diagram without having to consider the entire subsystem

State Pattern Definition The State Pattern allows an object to alter its behaviour when its internal state changes The object will appear to change its class But the object is neither deleted or recreated Internally the object can use singleton for each state, can create them using a virtual proxy or have an array of all possible states to facilitate state transition

Visitor Design Pattern Use when a task involves “acting” on a lot of node types within a composite tree Don’t want to add each task action to the nodes being visited Might not be permitted to change class Create a parallel abstract class It is notified as each node type is “visited” The actions to be performed are implemented by templating this parallel abstract class

Passes each node seen to visitor Actual visitor behaviour determined by templating Walks through structure

Visitor Pattern Advantages Separate what changes from what does not The parallel visitor class does this Classes open for extension; closed for modification The parallel visitor class achieves this Each class should have one responsibility Each visitor class is designed for one purpose The what to do code is centralised in the visitor class Not spread across all of the composite class members

Visitor Pattern Definition Use the Visitor Design Pattern when you want to add capabilities to a composite of objects and encapsulation within these objects is not important, undesirable, or not possible.

Chain of Responsibility Design Pattern Use the chain of responsibility design pattern when you want to give a sequence of objects in turn a chance to handle a request Each object in chain delegates to next object in chain if it can’t handle request

Advantages Decouples a request from all of the things that might handle it Allows the chain of things called to be changed at run time Need a catch all if going to be certain a command is handled But can be harder to debug

Interpreter Design Pattern Convert a grammar into an Abstract Syntax Tree Have the nodes in this tree be instances of classes Have an interpret function in each node that computes the actions to be performed by the command I.E. Construct and execute a plan

Interpreter Advantages Easy to implement language from grammar Can easily change/extend the language Can extend plan using composite design pattern to do other things Eg. Dump plan