Behavioral Design Pattern

Slides:



Advertisements
Similar presentations
Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Advertisements

Chain of Responsibility Pattern Gof pp Yuyang Chen.
Matt Klein 7/2/2009.  Intent  Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.
Zaki Alasadi Supervisor:Dr noorhosseini.
1 Pattern Games CS : Software Design Winter /T9.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
Algorithm Programming Behavioral Design Patterns Bar-Ilan University תשס " ו by Moshe Fresko.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
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.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
Pattern Oriented Design Chain of Responsibility From:Design Patterns Gamma. Johnson Helm. Vlissides (GoF) Present: F 楊汝康 R 徐德皓 R 高稚翔.
Chain of Responsibility Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Chain of Responsibility Behavioral Pattern. Defination Avoid coupling between the sender and receiver by giving more than one object a chance to handle.
Chain of Responsibility A graphical user interface and a user that needs help; a security system with multiple sensors; a banking automated coin storage.
Design Patterns Introduction
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 31. Review Creational Design Patterns – Singleton Pattern – Builder Pattern.
The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
The Chain of Responsibility Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Overview of Behavioral Patterns ©SoftMoore ConsultingSlide 1.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Design Patterns: MORE Examples
Mediator Design Pattern
Design Patterns: Brief Examples
Topics Graphical User Interfaces Using the tkinter Module
Chapter 10 Design Patterns.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Factory Patterns 1.
Observer Design Pattern
State pattern – A logical ‘Finite State Machine’
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
How to be a Good Developer
Important terms Black-box testing White-box testing Regression testing
Interfaces and Inheritance
Object Oriented Analysis and Design
Design Patterns Satya Puvvada Satya Puvvada.
Predefined Dialog Boxes
Design Patterns A Case Study: Designing a Document Editor
Decorator Pattern Intent
Jim Fawcett CSE776 – Design Patterns Summer 2003
Mediator Design Pattern (Behavioral)
08/15/09 Design Patterns James Brucker.
Introduction to Behavioral Patterns (1)
Informatics 122 Software Design II
Generation Gap By Kurt Rehwinkel
Behavioral Patterns Part-I introduction UNIT-VI
Interpreter Pattern.
Advanced Programming in Java
Strategy Design Pattern
Constructors, GUI’s(Using Swing) and ActionListner
Introduction to Design Patterns
Fundaments of Game Design
SE-2811 Software Component Design
Informatics 122 Software Design II
Chapter 11 Inheritance and Polymorphism Part 1
Decorator Pattern.
Software Design Lecture : 39.
Presentation transcript:

Behavioral Design Pattern

Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects. Behavioral patterns describe not just patterns of objects or classes but also the patterns of communication between them. These patterns characterize complex control flow that's difficult to follow at run-time. They shift your focus away from flow of control to let you concentrate just on the way objects are interconnected. Behavioral class patterns use inheritance to distribute behavior between classes.

Chain of Responsibility

As the name suggests, the chain of responsibility pattern creates a chain of receiver objects for a request. This pattern decouples sender and receiver of a request based on type of request. This pattern comes under behavioral patterns. In this pattern, normally each receiver contains reference to another receiver. If one object cannot handle the request then it passes the same to the next receiver and so on. Example: ATM(rupees of 1000,500,100 etc)

Intent 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. Motivation Consider a context-sensitive help facility for a graphical user interface. The user can obtain help information on any part of the interface just by clicking on it. The help that's provided depends on the part of the interface that's selected and its context; for example, a button widget in a dialog box might have different Help information than a similar button in the main window. If no specific help information exists for that part of the interface, then the help system should display a more general help message about the immediate context—the dialog box as a whole

Applicability Use Chain of Responsibility when more than one object may handle a request, and the handler isn't known a priori. The handler should be ascertained automatically. you want to issue a request to one of several objects without specifying the receiver explicitly. the set of objects that can handle a request should be specified dynamically.

Participants Handler (Help Handler) o defines an interface for handling requests. ConcreteHandler (PrintButton, PrintDialog) o handles requests it is responsible for. Client o initiates the request to a ConcreteHandler object on the chain.

Collaborations When a client issues a request, the request propagates along the chain until a Concrete Handler object takes responsibility for handling it. Consequences Chain of Responsibility has the following benefits and liabilities: Reduced coupling. Added flexibility in assigning responsibilities to objects. Chain of Responsibility gives you added flexibility in Distributing responsibilities among objects.

Implementation We have created an abstract class AbstractLogger with a level of logging. Then we have created three types of loggers extending the AbstractLogger. Each logger checks the level of message to its level and print accordingly otherwise does not print and pass the message to its next logger.

Step 1 Create an abstract logger class. AbstractLogger.java public abstract class AbstractLogger { public static int INFO = 1; public static int DEBUG = 2; public static int ERROR = 3; protected int level; //next element in chain of responsibility protected AbstractLogger nextLogger; public void setNextLogger(AbstractLogger nextLogger) this.nextLogger = nextLogger; }

public void logMessage (int level, String message) { if(this. level <= level) write(message); } if(nextLogger !=null) nextLogger.logMessage(level, message); abstract protected void write(String message);

Step 2 Create concrete classes extending the logger. ConsoleLogger.java public class ConsoleLogger extends AbstractLogger { public ConsoleLogger(int level) this.level = level; } @Override protected void write(String message) System.out.println("Standard Console::Logger: " + message);

ErrorLogger.java public class ErrorLogger extends AbstractLogger { public ErrorLogger(int level) this.level = level; } @Override protected void write(String message) System.out.println("Error Console::Logger: " + message);

FileLogger.java public class FileLogger extends AbstractLogger { public FileLogger(int level) this.level = level; } @Override protected void write(String message) System.out.println("File::Logger: " + message);

Step 3 Create different types of loggers Step 3 Create different types of loggers. Assign them error levels and set next logger in each logger. Next logger in each logger represents the part of the chain. ChainPatternDemo.java public class ChainPatternDemo { private static AbstractLogger getChainOfLoggers() AbstractLogger errorLogger = new ErrorLogger(AbstractLogger.ERROR); AbstractLogger fileLogger = new FileLogger(AbstractLogger.DEBUG); AbstractLogger consoleLogger = new ConsoleLogger(AbstractLogger.INFO); errorLogger.setNextLogger(fileLogger); fileLogger.setNextLogger(consoleLogger); return errorLogger; }

public static void main(String[] args) { AbstractLogger loggerChain = getChainOfLoggers(); loggerChain.logMessage(AbstractLogger.INFO, "This is an information."); loggerChain.logMessage(AbstractLogger.DEBUG, "This is a debug level information."); loggerChain.logMessage(AbstractLogger.ERROR, "This is an error information."); }

Step 4 Verify the output. Standard Console::Logger: This is an information. File::Logger: This is a debug level information. Standard Console::Logger: This is a debug level information. Error Console::Logger: This is an error information. File::Logger: This is an error information. Standard Console::Logger: This is an error information.