JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Chain of Responsibility Pattern
  • In Object Oriented Design, the chain-of-responsibility pattern is
    a design pattern consisting of a source of command objects and a
    series of processing objects.
  • Each processing object contains a set of logic that describes the types
    of command objects that it can handle, and how to pass off those that
    it cannot to the next processing object in the chain.
  • A mechanism also exists for adding new processing objects to the end
    of this chain.
  • This pattern promotes the idea of loose coupling, which is considered a
    programming best practice.
Purpose
  • 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

Applications
  • 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.
Non-software examples of Chain of Responsibility design patterns

The Chain of Responsibility pattern avoids coupling the sender of a request to the
receiver by giving more than one object a chance to handle the request.

Example:-1

Handler.java
public class Handler {
private static java.util.Random s_rn = new java.util.Random();
private static int s_next = 1;
private int m_id = s_next++;
private Handler m_next;
public void add(Handler next) {
if (m_next == null)
m_next = next;
else
m_next.add(next);
}
public void wrap_around(Handler root) {
if (m_next == null)
m_next = root;
else
m_next.wrap_around(root);
}
public void handle(int num) {
if (s_rn.nextInt(4!= 0) {
System.out.print(m_id + "-busy ");
m_next.handle(num);
else
System.out.println(m_id + "-handled-" + num);
}
}


ChainDemo.java
public class ChainDemo {
public static void main(String[] args) {
Handler chain_root = new Handler();
chain_root.add(new Handler());
chain_root.add(new Handler());
chain_root.add(new Handler());
chain_root.wrap_around(chain_root);
for (int i = 1; i <= 3; i++)
chain_root.handle(i);
}
}

Output:

Download zip code