SlideShare a Scribd company logo
1 of 23
Object Behavioral Pattern
Motivation
       Ever tried help.desk@ua.edu
       A central starting point for UA members to get help
        about all subjects.
       You can ask questions about many things
        (academic, registrar, actioncard, IT…)
       Your question will be solved by the related
        department and a reply will be sent.
       That means you can ask a question, and do not care
        which department will answer it.
       You just need the answer, and you will get it.


    2
Intuitive Solution
       There will be a manager, who receives all mails
        those are sent to help.desk@ua.edu
       The manager will process every mail and redirects it
        to the appropriate department.
       In this case, the manager must have references to
        all departments.




    3
An Efficient Solution
       You send the mail to help.desk@ua.edu
       Your mail will be forwarded to the first department to
        be answered.
       If that department do not have the answer, it will
        forward the mail to next department.
       Forward chain will go on until a department will
        answer your question.




    4
Chain of Responsibility Pattern
       Avoid coupling the sender of a request to its receiver
        by giving more than one object to handle the
        request.
       Chain the receiving objects and pass the request
        along the chain until an object handles it.




    5
Participants
       Handler
           Defines the interface for handling requests
           Can implement the successor link
       Concrete Handler
           Handles requests it is responsible for
           Can access its successor
           If can handle request, it does so; else forwards the
            request to its successor
       Client
           Initiates the request to a ConcreteHandler object on the
            chain.

    6
Collaboration
       In this sequence diagram, two requests are sent to
        c1 and one is replied by c2, other one is replied by
        c3. We can see request is passed from one another
        in chain.




    7
Consequences
       Reduced Coupling:
           The sender does not know which other object will handle
            the request.
           It only has to know that the request will be handled
            appropriately.
           A request in the chain also does not have any info about
            the chain structure.
       Flexibility
           The chain structure and the responsibilities of chain
            members can be modified at run time (Adding new
            handlers etc.)


    8
Consequences – cont’d
       Receipt:
           Since a request is not targeting an explicit receiver,
            handling it is not guaranteed.
           The request can pass through from all chain members
            without being handled and can fall off the end of chain.
           The chain must be configured properly.
       What if two concrete handlers try to handle the same
        request?
           An approach is introduced on next slide!
           What do you think?



    9
Request Structure
   If we only have one execution method for the
    request, in the previous scenario (multiple
    handlers), one of the handlers may never handle the
    request!
   Solution:
       We can have a Request class and may have many
        methods inside it. (Command Pattern?)
       Each handler handles the request’s some part.
       And passes the request along the chain.
  In normal version of pattern, a request is not
   forwarded to the other handler in the chain after
   processed.
   But this time, it MUST be forwarded even after
 10
Implementation Issues
    Default handler method may be implemented in two
     way:
        First one is: leaving the method totally abstract that every
         concrete handler have to implement that function and
         pass the request to successor if will not handle it.
        Second is: the handler method will implement the passing
         the request to successor, and a concrete handler will
         override the handler method only if it will handle a
         request.
    PseudoCode examples of two variations on next
     slide!


    11
Default Handler Behavior
          First method                              Second method
abstract class Handler {                  abstract class Handler {
    Handler successor = null;                 Handler successor = null;
    abstract void handleRequest(r);           abstract void handleRequest(r) {
}                                                 if(successor != null)
                                                      successor.handleRequest(r);
// a class handling the request               }
class ConcHandler1 extends Handler {      }
    void handleRequest(r) {
        process(r);                       // a class handling the request
        if(successor != null)             class ConcHandler1 extends Handler {
            successor.handleRequest(r);       void handleRequest(r) {
    }                                             process(r);
}                                                 if(successor != null)
                                                      successor.handleRequest(r);
// a class not handling the request           }
class ConcHandler2 extends Handler {      }
    void handleRequest(r) {
        if(successor != null)             // a class not handling the request
            successor.handleRequest(r);   class ConcHandler2 extends Handler {
    }                                     }
}



    12
Implementation Issues – cont’d
    Can you see the Chain of Responsibility in the
     Composite Pattern?
    What is different than classic Chain of Responsibility?
    You can just add a handle function to Component here
     and you can use children link to provide the successor
     effect.
    You have to process the request in composite nodes and
     after processing, you should deliver it to right child to be
     handled.
    Not a single link chain, but a tree chain!        Path of a request



               handle(Request r)




         handle(Request r)

                         handle(Request r)

    13
«Exception Handling» Example
    Exception handling uses Chain of Responsibility
     pattern.
    When an exception occurs, the program will start to
     search for a handler from the class which exception
     occurs through super classes.
    If an appropriate handler is found in one of
     superclasses, ok. If not, it will be handled globally.
        Class Diagram                 Object Diagram




    14
«Cache» Example
    Think of a processor with level 3 cache




    CPU will first look up level1 cache, then level2 and
     then level3, at last data will be read from main
     memory.
    Not a concrete example of the pattern, but the logic.
    15
«Email Handler» Example
    You have a big company and you are receiving lots
     of email from customers.
    Fan emails, spams, complaints, or a request of new
     location.
    Here’s what you have to do:




    16
Pipes and Filters Pattern
    Email handling example looks like filtering, right?
    It is also an example of architechtural pattern called
     Pipes and Filters.
    We can use this pattern to divide a larger processing
     task, into a sequence of smaller and independent
     steps (filters) that are connected by channels
     (pipes).
    Here is an example of order handling steps:
     *Receive Order 1-Decrypt the secure order 2-
     Authenticate customer 3-Remove duplicate orders
     *Process

    17
Non-software Example
    Coin sorter of a bank ATM




    When we drop coin to ATM, the ATM will try to put
     the coin to correct location, will start trying from
     quarter until cent.

    18
Brainstorm
    handleRequest method always accepted ONE
     request as parameter till now, how can we change it
     to handle a request list? What has to change?
        Inside the method, we will handle the appropriate requests,
         remove them out from the list, and pass remaining list to
         successor.
    What if we configure the chain wrong and started
     forwarding requests not from the first handler but from
     the third or fourth one?
        We can set the successor of the last handler in the chain to
         the first one. But we must ensure, the request is handled or
         it may cause with a loop.

    19
Brainstorm – cont’d
    If each handler modifies some part of the request
     (partially handles) and passes the request to
     successor, will it again be chain of responsibility?
        What do you think?
        I think YES.
    Chain of Responsibility vs Pipes and Filters
        Don’t worry, you don’t have to select one of them. Pipes
         and Filters is a subclass of Chain of Responsibility.
         (variant to be used in architectural purposes)




    20
Demo
    It is demo time!
    First demo is implemented by me.
    Second demo is totally taken from a webpage.
        You can find it in references.




    21
Questions




22
References
    GoF Design Patterns Book
    http://www.javacamp.org/designPattern/chains.html
    http://www.unilim.fr/sci/wiki/_media/cali/cpumemory.p
     df
    Oreilly Head First: Design Patterns Book
    Fry of Futurama for Questions Picture
    http://eaipatterns.com/PipesAndFilters.html
    Non-Software Examples of Software Design
     Patterns by Michael Duell



    23

More Related Content

What's hot

Java/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCJava/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCFAKHRUN NISHA
 
ASP.NET - Life cycle of asp
ASP.NET - Life cycle of aspASP.NET - Life cycle of asp
ASP.NET - Life cycle of asppriya Nithya
 
Proxy design pattern (Class Ambassador)
Proxy design pattern (Class Ambassador)Proxy design pattern (Class Ambassador)
Proxy design pattern (Class Ambassador)Sameer Rathoud
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Hitesh-Java
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJSBrainhub
 
Domain Driven Design Made Functional with Python
Domain Driven Design Made Functional with Python Domain Driven Design Made Functional with Python
Domain Driven Design Made Functional with Python Jean Carlo Machado
 
RxJS & Angular Reactive Forms @ Codemotion 2019
RxJS & Angular Reactive Forms @ Codemotion 2019RxJS & Angular Reactive Forms @ Codemotion 2019
RxJS & Angular Reactive Forms @ Codemotion 2019Fabio Biondi
 
Reasons to use React Query
Reasons to use React QueryReasons to use React Query
Reasons to use React Queryjavaria javaid
 
Proxy Design Patterns
Proxy Design PatternsProxy Design Patterns
Proxy Design PatternsZafer Genc
 
Celery - A Distributed Task Queue
Celery - A Distributed Task QueueCelery - A Distributed Task Queue
Celery - A Distributed Task QueueDuy Do
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JSArno Lordkronos
 
The Singleton Pattern Presentation
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern PresentationJAINIK PATEL
 
Event driven microservices with axon and spring boot-excitingly boring
Event driven microservices with axon and spring boot-excitingly boringEvent driven microservices with axon and spring boot-excitingly boring
Event driven microservices with axon and spring boot-excitingly boringAllard Buijze
 

What's hot (20)

Server side rendering review
Server side rendering reviewServer side rendering review
Server side rendering review
 
Memento pattern
Memento patternMemento pattern
Memento pattern
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
Java/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBCJava/Servlet/JSP/JDBC
Java/Servlet/JSP/JDBC
 
React hooks
React hooksReact hooks
React hooks
 
ASP.NET - Life cycle of asp
ASP.NET - Life cycle of aspASP.NET - Life cycle of asp
ASP.NET - Life cycle of asp
 
Proxy design pattern (Class Ambassador)
Proxy design pattern (Class Ambassador)Proxy design pattern (Class Ambassador)
Proxy design pattern (Class Ambassador)
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
 
Domain Driven Design Made Functional with Python
Domain Driven Design Made Functional with Python Domain Driven Design Made Functional with Python
Domain Driven Design Made Functional with Python
 
RxJS & Angular Reactive Forms @ Codemotion 2019
RxJS & Angular Reactive Forms @ Codemotion 2019RxJS & Angular Reactive Forms @ Codemotion 2019
RxJS & Angular Reactive Forms @ Codemotion 2019
 
Reasons to use React Query
Reasons to use React QueryReasons to use React Query
Reasons to use React Query
 
Sequelize
SequelizeSequelize
Sequelize
 
Proxy Design Patterns
Proxy Design PatternsProxy Design Patterns
Proxy Design Patterns
 
Celery - A Distributed Task Queue
Celery - A Distributed Task QueueCelery - A Distributed Task Queue
Celery - A Distributed Task Queue
 
Spring MVC Framework
Spring MVC FrameworkSpring MVC Framework
Spring MVC Framework
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 
The Singleton Pattern Presentation
The Singleton Pattern PresentationThe Singleton Pattern Presentation
The Singleton Pattern Presentation
 
Event driven microservices with axon and spring boot-excitingly boring
Event driven microservices with axon and spring boot-excitingly boringEvent driven microservices with axon and spring boot-excitingly boring
Event driven microservices with axon and spring boot-excitingly boring
 
Proxy design pattern
Proxy design patternProxy design pattern
Proxy design pattern
 

Viewers also liked

Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design PatternShahriar Hyder
 
Strategy and Template Pattern
Strategy and Template PatternStrategy and Template Pattern
Strategy and Template PatternJonathan Simon
 
20120420 - Design pattern bridge
20120420 - Design pattern bridge20120420 - Design pattern bridge
20120420 - Design pattern bridgeLearningTech
 
Bridge Design Pattern
Bridge Design PatternBridge Design Pattern
Bridge Design Patternsahilrk911
 
Human Factors and User Interface Design
Human Factors and User Interface DesignHuman Factors and User Interface Design
Human Factors and User Interface DesignSaggitariusArrow
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingHüseyin Ergin
 
Andrew Bannister, Head of Environmental Management at VW UK - Interaction wit...
Andrew Bannister, Head of Environmental Management at VW UK - Interaction wit...Andrew Bannister, Head of Environmental Management at VW UK - Interaction wit...
Andrew Bannister, Head of Environmental Management at VW UK - Interaction wit...Global Business Events
 
MVC and Other Design Patterns
MVC and Other Design PatternsMVC and Other Design Patterns
MVC and Other Design PatternsJonathan Simon
 
PATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsPATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsMichael Heron
 
Command and Adapter Pattern
Command and Adapter PatternCommand and Adapter Pattern
Command and Adapter PatternJonathan Simon
 
Microclimate
MicroclimateMicroclimate
MicroclimateSHS Geog
 
Supply Chain Metrics That Matter: A Focus on Retail
Supply Chain Metrics That Matter: A Focus on RetailSupply Chain Metrics That Matter: A Focus on Retail
Supply Chain Metrics That Matter: A Focus on RetailLora Cecere
 

Viewers also liked (20)

Command Design Pattern
Command Design PatternCommand Design Pattern
Command Design Pattern
 
Chain of Responsibility Pattern
Chain of Responsibility PatternChain of Responsibility Pattern
Chain of Responsibility Pattern
 
Mediator Design Pattern
Mediator Design PatternMediator Design Pattern
Mediator Design Pattern
 
Bridge pattern
Bridge patternBridge pattern
Bridge pattern
 
Bridge Pattern Derek Weeks
Bridge Pattern   Derek WeeksBridge Pattern   Derek Weeks
Bridge Pattern Derek Weeks
 
Strategy and Template Pattern
Strategy and Template PatternStrategy and Template Pattern
Strategy and Template Pattern
 
Bridge Pattern
Bridge PatternBridge Pattern
Bridge Pattern
 
20120420 - Design pattern bridge
20120420 - Design pattern bridge20120420 - Design pattern bridge
20120420 - Design pattern bridge
 
Bridge Design Pattern
Bridge Design PatternBridge Design Pattern
Bridge Design Pattern
 
Human Factors and User Interface Design
Human Factors and User Interface DesignHuman Factors and User Interface Design
Human Factors and User Interface Design
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Andrew Bannister, Head of Environmental Management at VW UK - Interaction wit...
Andrew Bannister, Head of Environmental Management at VW UK - Interaction wit...Andrew Bannister, Head of Environmental Management at VW UK - Interaction wit...
Andrew Bannister, Head of Environmental Management at VW UK - Interaction wit...
 
Command Pattern
Command PatternCommand Pattern
Command Pattern
 
MVC and Other Design Patterns
MVC and Other Design PatternsMVC and Other Design Patterns
MVC and Other Design Patterns
 
Command pattern
Command patternCommand pattern
Command pattern
 
PATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design PatternsPATTERNS03 - Behavioural Design Patterns
PATTERNS03 - Behavioural Design Patterns
 
Command and Adapter Pattern
Command and Adapter PatternCommand and Adapter Pattern
Command and Adapter Pattern
 
Microclimate
MicroclimateMicroclimate
Microclimate
 
Supply Chain Metrics That Matter: A Focus on Retail
Supply Chain Metrics That Matter: A Focus on RetailSupply Chain Metrics That Matter: A Focus on Retail
Supply Chain Metrics That Matter: A Focus on Retail
 
Dbn163 # 04. macro micro climate
Dbn163 # 04. macro micro climateDbn163 # 04. macro micro climate
Dbn163 # 04. macro micro climate
 

Similar to Chain of Responsibility Pattern

note2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptxnote2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptxReemaAsker1
 
Design Patterns : Solution to Software Design Problems
Design Patterns : Solution to Software Design ProblemsDesign Patterns : Solution to Software Design Problems
Design Patterns : Solution to Software Design ProblemsEdureka!
 
Java apptitude-questions-part-2
Java apptitude-questions-part-2Java apptitude-questions-part-2
Java apptitude-questions-part-2vishvavidya
 
Tcs NQTExam technical questions
Tcs NQTExam technical questionsTcs NQTExam technical questions
Tcs NQTExam technical questionsAniketBhandare2
 
A Multi-Agent System Approach to Load-Balancing and Resource Allocation for D...
A Multi-Agent System Approach to Load-Balancing and Resource Allocation for D...A Multi-Agent System Approach to Load-Balancing and Resource Allocation for D...
A Multi-Agent System Approach to Load-Balancing and Resource Allocation for D...Soumya Banerjee
 
M03_1_Structur alDiagrams.ppt
M03_1_Structur                         alDiagrams.pptM03_1_Structur                         alDiagrams.ppt
M03_1_Structur alDiagrams.pptnesarahmad37
 
Technical_Interview_Questions.pdf
Technical_Interview_Questions.pdfTechnical_Interview_Questions.pdf
Technical_Interview_Questions.pdfadityashukla939020
 
Core java interview faq
Core java interview faqCore java interview faq
Core java interview faqKumaran K
 
A Framework for Performance Analysis of Computing Clouds
A Framework for Performance Analysis of Computing CloudsA Framework for Performance Analysis of Computing Clouds
A Framework for Performance Analysis of Computing Cloudsijsrd.com
 
Review of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiReview of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiNico Ludwig
 
an Introduction to Redux
an Introduction to Reduxan Introduction to Redux
an Introduction to ReduxAmin Ashtiani
 
New folderIMAG2318.jpgNew folderIMAG2319.jpgNew folder.docx
New folderIMAG2318.jpgNew folderIMAG2319.jpgNew folder.docxNew folderIMAG2318.jpgNew folderIMAG2319.jpgNew folder.docx
New folderIMAG2318.jpgNew folderIMAG2319.jpgNew folder.docxhenrymartin15260
 
Latest .Net Questions and Answers
Latest .Net Questions and Answers Latest .Net Questions and Answers
Latest .Net Questions and Answers Narasimhulu Palle
 
25 java tough interview questions
25 java tough interview questions25 java tough interview questions
25 java tough interview questionsArun Banotra
 
M03 2 Behavioral Diagrams
M03 2 Behavioral DiagramsM03 2 Behavioral Diagrams
M03 2 Behavioral DiagramsDang Tuan
 
Object Oriented Analysis and Design with UML2 part2
Object Oriented Analysis and Design with UML2 part2Object Oriented Analysis and Design with UML2 part2
Object Oriented Analysis and Design with UML2 part2Haitham Raik
 
CHP-4.pptx
CHP-4.pptxCHP-4.pptx
CHP-4.pptxFamiDan
 
Chapter 5:Understanding Variable Scope and Class Construction
Chapter 5:Understanding Variable Scope and Class ConstructionChapter 5:Understanding Variable Scope and Class Construction
Chapter 5:Understanding Variable Scope and Class ConstructionIt Academy
 

Similar to Chain of Responsibility Pattern (20)

note2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptxnote2_DesignPatterns (1).pptx
note2_DesignPatterns (1).pptx
 
Design Patterns : Solution to Software Design Problems
Design Patterns : Solution to Software Design ProblemsDesign Patterns : Solution to Software Design Problems
Design Patterns : Solution to Software Design Problems
 
Java apptitude-questions-part-2
Java apptitude-questions-part-2Java apptitude-questions-part-2
Java apptitude-questions-part-2
 
Tcs NQTExam technical questions
Tcs NQTExam technical questionsTcs NQTExam technical questions
Tcs NQTExam technical questions
 
A Multi-Agent System Approach to Load-Balancing and Resource Allocation for D...
A Multi-Agent System Approach to Load-Balancing and Resource Allocation for D...A Multi-Agent System Approach to Load-Balancing and Resource Allocation for D...
A Multi-Agent System Approach to Load-Balancing and Resource Allocation for D...
 
M03_1_Structur alDiagrams.ppt
M03_1_Structur                         alDiagrams.pptM03_1_Structur                         alDiagrams.ppt
M03_1_Structur alDiagrams.ppt
 
Technical_Interview_Questions.pdf
Technical_Interview_Questions.pdfTechnical_Interview_Questions.pdf
Technical_Interview_Questions.pdf
 
Lecture 20-21
Lecture 20-21Lecture 20-21
Lecture 20-21
 
Core java interview faq
Core java interview faqCore java interview faq
Core java interview faq
 
A Framework for Performance Analysis of Computing Clouds
A Framework for Performance Analysis of Computing CloudsA Framework for Performance Analysis of Computing Clouds
A Framework for Performance Analysis of Computing Clouds
 
Review of c_sharp2_features_part_ii
Review of c_sharp2_features_part_iiReview of c_sharp2_features_part_ii
Review of c_sharp2_features_part_ii
 
Bad Smells in Code
Bad Smells in CodeBad Smells in Code
Bad Smells in Code
 
an Introduction to Redux
an Introduction to Reduxan Introduction to Redux
an Introduction to Redux
 
New folderIMAG2318.jpgNew folderIMAG2319.jpgNew folder.docx
New folderIMAG2318.jpgNew folderIMAG2319.jpgNew folder.docxNew folderIMAG2318.jpgNew folderIMAG2319.jpgNew folder.docx
New folderIMAG2318.jpgNew folderIMAG2319.jpgNew folder.docx
 
Latest .Net Questions and Answers
Latest .Net Questions and Answers Latest .Net Questions and Answers
Latest .Net Questions and Answers
 
25 java tough interview questions
25 java tough interview questions25 java tough interview questions
25 java tough interview questions
 
M03 2 Behavioral Diagrams
M03 2 Behavioral DiagramsM03 2 Behavioral Diagrams
M03 2 Behavioral Diagrams
 
Object Oriented Analysis and Design with UML2 part2
Object Oriented Analysis and Design with UML2 part2Object Oriented Analysis and Design with UML2 part2
Object Oriented Analysis and Design with UML2 part2
 
CHP-4.pptx
CHP-4.pptxCHP-4.pptx
CHP-4.pptx
 
Chapter 5:Understanding Variable Scope and Class Construction
Chapter 5:Understanding Variable Scope and Class ConstructionChapter 5:Understanding Variable Scope and Class Construction
Chapter 5:Understanding Variable Scope and Class Construction
 

More from Hüseyin Ergin

Is there life after code?
Is there life after code?Is there life after code?
Is there life after code?Hüseyin Ergin
 
SITE - CS Presentation
SITE - CS PresentationSITE - CS Presentation
SITE - CS PresentationHüseyin Ergin
 
Software Versioning with Bitbucket and Eclipse
Software Versioning with Bitbucket and EclipseSoftware Versioning with Bitbucket and Eclipse
Software Versioning with Bitbucket and EclipseHüseyin Ergin
 
From the book: 50 Proven Ways to Be Persuasive
From the book: 50 Proven Ways to Be PersuasiveFrom the book: 50 Proven Ways to Be Persuasive
From the book: 50 Proven Ways to Be PersuasiveHüseyin Ergin
 
Object Oriented Paradigm
Object Oriented ParadigmObject Oriented Paradigm
Object Oriented ParadigmHüseyin Ergin
 

More from Hüseyin Ergin (7)

Is there life after code?
Is there life after code?Is there life after code?
Is there life after code?
 
SITE - CS Presentation
SITE - CS PresentationSITE - CS Presentation
SITE - CS Presentation
 
Software Versioning with Bitbucket and Eclipse
Software Versioning with Bitbucket and EclipseSoftware Versioning with Bitbucket and Eclipse
Software Versioning with Bitbucket and Eclipse
 
From the book: 50 Proven Ways to Be Persuasive
From the book: 50 Proven Ways to Be PersuasiveFrom the book: 50 Proven Ways to Be Persuasive
From the book: 50 Proven Ways to Be Persuasive
 
Object Oriented Paradigm
Object Oriented ParadigmObject Oriented Paradigm
Object Oriented Paradigm
 
Proxy Pattern
Proxy PatternProxy Pattern
Proxy Pattern
 
Flyweight Pattern
Flyweight PatternFlyweight Pattern
Flyweight Pattern
 

Recently uploaded

VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...Suhani Kapoor
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...Call Girls in Nagpur High Profile
 
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Yantram Animation Studio Corporation
 
Design Portfolio - 2024 - William Vickery
Design Portfolio - 2024 - William VickeryDesign Portfolio - 2024 - William Vickery
Design Portfolio - 2024 - William VickeryWilliamVickery6
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Delhi Call girls
 
Revit Understanding Reference Planes and Reference lines in Revit for Family ...
Revit Understanding Reference Planes and Reference lines in Revit for Family ...Revit Understanding Reference Planes and Reference lines in Revit for Family ...
Revit Understanding Reference Planes and Reference lines in Revit for Family ...Narsimha murthy
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxjanettecruzeiro1
 
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130  Available With RoomVIP Kolkata Call Girl Gariahat 👉 8250192130  Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Roomdivyansh0kumar0
 
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Delhi Call girls
 
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiVIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiSuhani Kapoor
 
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...Amil baba
 
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵anilsa9823
 
A level Digipak development Presentation
A level Digipak development PresentationA level Digipak development Presentation
A level Digipak development Presentationamedia6
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceanilsa9823
 
Fashion trends before and after covid.pptx
Fashion trends before and after covid.pptxFashion trends before and after covid.pptx
Fashion trends before and after covid.pptxVanshNarang19
 
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...Amil baba
 
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`dajasot375
 
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai DouxDubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai Douxkojalkojal131
 

Recently uploaded (20)

VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
 
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
 
Design Portfolio - 2024 - William Vickery
Design Portfolio - 2024 - William VickeryDesign Portfolio - 2024 - William Vickery
Design Portfolio - 2024 - William Vickery
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
 
Revit Understanding Reference Planes and Reference lines in Revit for Family ...
Revit Understanding Reference Planes and Reference lines in Revit for Family ...Revit Understanding Reference Planes and Reference lines in Revit for Family ...
Revit Understanding Reference Planes and Reference lines in Revit for Family ...
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130  Available With RoomVIP Kolkata Call Girl Gariahat 👉 8250192130  Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
 
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
 
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiVIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
 
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
 
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...
NO1 Famous Amil Baba In Karachi Kala Jadu In Karachi Amil baba In Karachi Add...
 
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance VVIP 🍎 SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SER...Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance VVIP 🍎 SER...
 
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
 
A level Digipak development Presentation
A level Digipak development PresentationA level Digipak development Presentation
A level Digipak development Presentation
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
 
Fashion trends before and after covid.pptx
Fashion trends before and after covid.pptxFashion trends before and after covid.pptx
Fashion trends before and after covid.pptx
 
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
NO1 Trending kala jadu Love Marriage Black Magic Punjab Powerful Black Magic ...
 
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
 
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai DouxDubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
 

Chain of Responsibility Pattern

  • 2. Motivation  Ever tried help.desk@ua.edu  A central starting point for UA members to get help about all subjects.  You can ask questions about many things (academic, registrar, actioncard, IT…)  Your question will be solved by the related department and a reply will be sent.  That means you can ask a question, and do not care which department will answer it.  You just need the answer, and you will get it. 2
  • 3. Intuitive Solution  There will be a manager, who receives all mails those are sent to help.desk@ua.edu  The manager will process every mail and redirects it to the appropriate department.  In this case, the manager must have references to all departments. 3
  • 4. An Efficient Solution  You send the mail to help.desk@ua.edu  Your mail will be forwarded to the first department to be answered.  If that department do not have the answer, it will forward the mail to next department.  Forward chain will go on until a department will answer your question. 4
  • 5. Chain of Responsibility Pattern  Avoid coupling the sender of a request to its receiver by giving more than one object to handle the request.  Chain the receiving objects and pass the request along the chain until an object handles it. 5
  • 6. Participants  Handler  Defines the interface for handling requests  Can implement the successor link  Concrete Handler  Handles requests it is responsible for  Can access its successor  If can handle request, it does so; else forwards the request to its successor  Client  Initiates the request to a ConcreteHandler object on the chain. 6
  • 7. Collaboration  In this sequence diagram, two requests are sent to c1 and one is replied by c2, other one is replied by c3. We can see request is passed from one another in chain. 7
  • 8. Consequences  Reduced Coupling:  The sender does not know which other object will handle the request.  It only has to know that the request will be handled appropriately.  A request in the chain also does not have any info about the chain structure.  Flexibility  The chain structure and the responsibilities of chain members can be modified at run time (Adding new handlers etc.) 8
  • 9. Consequences – cont’d  Receipt:  Since a request is not targeting an explicit receiver, handling it is not guaranteed.  The request can pass through from all chain members without being handled and can fall off the end of chain.  The chain must be configured properly.  What if two concrete handlers try to handle the same request?  An approach is introduced on next slide!  What do you think? 9
  • 10. Request Structure  If we only have one execution method for the request, in the previous scenario (multiple handlers), one of the handlers may never handle the request!  Solution:  We can have a Request class and may have many methods inside it. (Command Pattern?)  Each handler handles the request’s some part.  And passes the request along the chain.  In normal version of pattern, a request is not forwarded to the other handler in the chain after processed. But this time, it MUST be forwarded even after  10
  • 11. Implementation Issues  Default handler method may be implemented in two way:  First one is: leaving the method totally abstract that every concrete handler have to implement that function and pass the request to successor if will not handle it.  Second is: the handler method will implement the passing the request to successor, and a concrete handler will override the handler method only if it will handle a request.  PseudoCode examples of two variations on next slide! 11
  • 12. Default Handler Behavior  First method Second method abstract class Handler { abstract class Handler { Handler successor = null; Handler successor = null; abstract void handleRequest(r); abstract void handleRequest(r) { } if(successor != null) successor.handleRequest(r); // a class handling the request } class ConcHandler1 extends Handler { } void handleRequest(r) { process(r); // a class handling the request if(successor != null) class ConcHandler1 extends Handler { successor.handleRequest(r); void handleRequest(r) { } process(r); } if(successor != null) successor.handleRequest(r); // a class not handling the request } class ConcHandler2 extends Handler { } void handleRequest(r) { if(successor != null) // a class not handling the request successor.handleRequest(r); class ConcHandler2 extends Handler { } } } 12
  • 13. Implementation Issues – cont’d  Can you see the Chain of Responsibility in the Composite Pattern?  What is different than classic Chain of Responsibility?  You can just add a handle function to Component here and you can use children link to provide the successor effect.  You have to process the request in composite nodes and after processing, you should deliver it to right child to be handled.  Not a single link chain, but a tree chain! Path of a request handle(Request r) handle(Request r) handle(Request r) 13
  • 14. «Exception Handling» Example  Exception handling uses Chain of Responsibility pattern.  When an exception occurs, the program will start to search for a handler from the class which exception occurs through super classes.  If an appropriate handler is found in one of superclasses, ok. If not, it will be handled globally.  Class Diagram Object Diagram 14
  • 15. «Cache» Example  Think of a processor with level 3 cache  CPU will first look up level1 cache, then level2 and then level3, at last data will be read from main memory.  Not a concrete example of the pattern, but the logic. 15
  • 16. «Email Handler» Example  You have a big company and you are receiving lots of email from customers.  Fan emails, spams, complaints, or a request of new location.  Here’s what you have to do: 16
  • 17. Pipes and Filters Pattern  Email handling example looks like filtering, right?  It is also an example of architechtural pattern called Pipes and Filters.  We can use this pattern to divide a larger processing task, into a sequence of smaller and independent steps (filters) that are connected by channels (pipes).  Here is an example of order handling steps: *Receive Order 1-Decrypt the secure order 2- Authenticate customer 3-Remove duplicate orders *Process 17
  • 18. Non-software Example  Coin sorter of a bank ATM  When we drop coin to ATM, the ATM will try to put the coin to correct location, will start trying from quarter until cent. 18
  • 19. Brainstorm  handleRequest method always accepted ONE request as parameter till now, how can we change it to handle a request list? What has to change?  Inside the method, we will handle the appropriate requests, remove them out from the list, and pass remaining list to successor.  What if we configure the chain wrong and started forwarding requests not from the first handler but from the third or fourth one?  We can set the successor of the last handler in the chain to the first one. But we must ensure, the request is handled or it may cause with a loop. 19
  • 20. Brainstorm – cont’d  If each handler modifies some part of the request (partially handles) and passes the request to successor, will it again be chain of responsibility?  What do you think?  I think YES.  Chain of Responsibility vs Pipes and Filters  Don’t worry, you don’t have to select one of them. Pipes and Filters is a subclass of Chain of Responsibility. (variant to be used in architectural purposes) 20
  • 21. Demo  It is demo time!  First demo is implemented by me.  Second demo is totally taken from a webpage.  You can find it in references. 21
  • 23. References  GoF Design Patterns Book  http://www.javacamp.org/designPattern/chains.html  http://www.unilim.fr/sci/wiki/_media/cali/cpumemory.p df  Oreilly Head First: Design Patterns Book  Fry of Futurama for Questions Picture  http://eaipatterns.com/PipesAndFilters.html  Non-Software Examples of Software Design Patterns by Michael Duell 23