Serving Industries Worldwide

Innovative Ways - Satisfied Clientele

A complete guide on Behavioral Design Pattern and Chain of Responsibility Pattern in Java


Listening is fun too.

Straighten your back and cherish with coffee - PLAY !

 
 

A_complete_guide_on_Behavioral_Design_Pattern_and_Chain_of_Responsibility_Pattern_in_Java

Table of Content

In this blog, we’ll learn behavioral design patterns where and when we can use this particular pattern, and its types.

Also, we’ll particularly focus on the chain of responsibility pattern which is a type of behavioral design pattern.

What is Behavioral Design Pattern?

A design pattern in which more emphasis is given to how the objects interact with each other.

Also as the name suggests a chain of responsibility i.e., objects have their responsibilities/tasks to perform.

In behavioral design patterns, there should be minimum hard coding, the objects should be loosely coupled and coherence should be more.

Now, let us understand the terms coupling and coherence. These are significant terms in design patterns and also in software engineering.

Coupling - Coupling means interdependency here in terms of objects. The objects should be less dependent on each other for a particular task, otherwise, due to a failure or delay in one object can lead to a system failure.

Coherence - Coherence means that the objects/modules should work together so that they complement each other rather than creating a burden on the system.



Types of Behavioral Design Pattern:

  1. Interpreter Pattern: As the name suggests it is used to interpret a pattern in a particular way.
  2. Mediator Pattern: This design pattern acts as a mediator between different classes so that they can communicate properly and loosely coupled manner.
  3. Command Pattern: Here the request by the client is wrapped and converted into a command. It is also known as a data-driven pattern.
  4. State Pattern: As the name suggests, it is related to the state of an object in different classes.
  5. Chain of Responsibility Pattern: Used when we want a chain of objects.
  6. Memento Pattern: This design pattern is used when we want to change the state of an object to the previous form.
  7. Template Pattern: In template pattern, there is an abstract class which set the implementation so other class can use them according to it.
  8. Visitor Pattern: In this method, there is a visitor class that will change the algorithm of the particular element.
  9. Null Object: We use this pattern when we have null values and do nothing relationship.
  10. Strategy Pattern: We can use this pattern when we want to change the algorithm or behavior of a class at runtime.
  11. Iterator Pattern: This is one of the most common design patterns, it is used when want to access data sequentially from a collection. In java, iterator pattern is used in iterator also.
  12. Observer Pattern: When we want to observe and respond according to change in an object we use an observer pattern. When an object changes its relationship with other objects which are in entity-relationship also changes so, we need observer pattern.

What is a Chain of Responsibility Pattern?

A design pattern in which objects show a chain behavior is a chain of responsibility pattern.

Whenever a user sends a request it passes through a chain of an object and any object can send a response accordingly. So there is the least coupling between the objects.

Advantages of using Chain of Responsibility Pattern:

  1. Less coupling in the system, so less interdependency.
  2. The structure becomes more flexible due to less coupling and more coherence.

Searching for Reliable Java Development Company ?

Your Search ends here.


Example for Chain of Responsibility Pattern:

Now, let us understand the chain of responsibility pattern with an example. The given below UML diagram will make you easily understand.

UML_Diagram

[Source]


So here we have three different logger class which extends the main abstract logger class and a client class.

Each class has its attributes and methods like Logger has output info, error info, debug info as attributes, and set Logger, Message, display Log Info as methods.

Console Based Logger class has a console-based logger and displays Log Info as methods but no attributes of its own. Debug Based Logger has Debug Based Logger(int levels) and displays Log Info as methods but no attributes of it

The client class which is Chain of Responsibility Client has de chain() and main methods.

Main logger class
public abstract class Logger {  
    public static int OUTPUTINFO = 1;  
    public static int ERRORINFO = 2;  
    public static int DEBUGINFO = 3;  
    protected int levels;  
    protected Logger nextLevelLogger;  
public void setNextLevelLogger( Logger nextLevelLogger ) 
{  
        this.nextLevelLogger = nextLevelLogger;  
    }  
        public void logMessage( int levels, String msg )
{  
        if(this.levels <= levels){  
            displayLogInfo(msg);  
        }  
        if (nextLevelLogger != null) {  
            nextLevelLogger.logMessage(levels, msg);  
        }  
    }  
    protected abstract void displayLogInfo(String msg);  
}  
Console based logger class
public class DebugBasedLogger extends Logger {  
    public DebugBasedLogger(int levels) {  
        this.levels = levels;  
    }  
    @Override  
    protected void displayLogInfo(String msg) {  
        System.out.println("DEBUG LOGGER INFO: " + msg);  
    }  
}
Error based logger class
public class ErrorBasedLogger extends Logger {  
    public ErrorBasedLogger(int levels) {  
        this.levels=levels;  
    }  
    @Override  
    protected void displayLogInfo(String msg) {  
        System.out.println("ERROR LOGGER INFO: " + msg);  
    }  
}
Client class / main class
public class ChainofResponsibilityClient {  
    private static Logger doChaining(){  
          Logger consoleLogger = new ConsoleBasedLogger(Logger.OUTPUTINFO);  
            
          Logger errorLogger = new ErrorBasedLogger(Logger.ERRORINFO);  
          consoleLogger.setNextLevelLogger(errorLogger);  
            
          Logger debugLogger = new DebugBasedLogger(Logger.DEBUGINFO);  
          errorLogger.setNextLevelLogger(debugLogger);  
            
          return consoleLogger;   
          }  
          public static void main(String args[]){  
          Logger chainLogger = doChaining();  
  
              chainLogger.logMessage(Logger.OUTPUTINFO, "Enter the sequence of values ");  
              chainLogger.logMessage(Logger.ERRORINFO, "An error has occurred now");  
              chainLogger.logMessage(Logger.DEBUGINFO, "This was the error now debugging is compiled");  
              }  
}  
Output:
  1. AbilityClient  
  2. CONSOLE LOGGER INFO: Enter the sequence of values  
  3. CONSOLE LOGGER INFO: An error has occurred now  
  4. ERROR LOGGER INFO: An error has occurred now  
  5. CONSOLE LOGGER INFO: This was the error now debugging is compiled  
  6. ERROR LOGGER INFO: This was the error now debugging is compiled  
  7. DEBUG LOGGER INFO: This was the error now debugging is compiled

Conclusion

So in this blog, we focused learning on the chain of responsibility pattern which is one of the behavioral design patterns, its uses, advantages, and also when to use these design patterns, etc. along with an example.

A complete guide on Behavioral Design Pattern and Chain of Responsibility Pattern in Java

A_complete_guide_on_Behavioral_Design_Pattern_and_Chain_of_Responsibility_Pattern_in_Java

Table of Content

In this blog, we’ll learn behavioral design patterns where and when we can use this particular pattern, and its types.

Also, we’ll particularly focus on the chain of responsibility pattern which is a type of behavioral design pattern.

What is Behavioral Design Pattern?

A design pattern in which more emphasis is given to how the objects interact with each other.

Also as the name suggests a chain of responsibility i.e., objects have their responsibilities/tasks to perform.

In behavioral design patterns, there should be minimum hard coding, the objects should be loosely coupled and coherence should be more.

Now, let us understand the terms coupling and coherence. These are significant terms in design patterns and also in software engineering.

Coupling - Coupling means interdependency here in terms of objects. The objects should be less dependent on each other for a particular task, otherwise, due to a failure or delay in one object can lead to a system failure.

Coherence - Coherence means that the objects/modules should work together so that they complement each other rather than creating a burden on the system.



Types of Behavioral Design Pattern:

  1. Interpreter Pattern: As the name suggests it is used to interpret a pattern in a particular way.
  2. Mediator Pattern: This design pattern acts as a mediator between different classes so that they can communicate properly and loosely coupled manner.
  3. Command Pattern: Here the request by the client is wrapped and converted into a command. It is also known as a data-driven pattern.
  4. State Pattern: As the name suggests, it is related to the state of an object in different classes.
  5. Chain of Responsibility Pattern: Used when we want a chain of objects.
  6. Memento Pattern: This design pattern is used when we want to change the state of an object to the previous form.
  7. Template Pattern: In template pattern, there is an abstract class which set the implementation so other class can use them according to it.
  8. Visitor Pattern: In this method, there is a visitor class that will change the algorithm of the particular element.
  9. Null Object: We use this pattern when we have null values and do nothing relationship.
  10. Strategy Pattern: We can use this pattern when we want to change the algorithm or behavior of a class at runtime.
  11. Iterator Pattern: This is one of the most common design patterns, it is used when want to access data sequentially from a collection. In java, iterator pattern is used in iterator also.
  12. Observer Pattern: When we want to observe and respond according to change in an object we use an observer pattern. When an object changes its relationship with other objects which are in entity-relationship also changes so, we need observer pattern.

What is a Chain of Responsibility Pattern?

A design pattern in which objects show a chain behavior is a chain of responsibility pattern.

Whenever a user sends a request it passes through a chain of an object and any object can send a response accordingly. So there is the least coupling between the objects.

Advantages of using Chain of Responsibility Pattern:

  1. Less coupling in the system, so less interdependency.
  2. The structure becomes more flexible due to less coupling and more coherence.

Searching for Reliable Java Development Company ?

Your Search ends here.


Example for Chain of Responsibility Pattern:

Now, let us understand the chain of responsibility pattern with an example. The given below UML diagram will make you easily understand.

UML_Diagram

[Source]


So here we have three different logger class which extends the main abstract logger class and a client class.

Each class has its attributes and methods like Logger has output info, error info, debug info as attributes, and set Logger, Message, display Log Info as methods.

Console Based Logger class has a console-based logger and displays Log Info as methods but no attributes of its own. Debug Based Logger has Debug Based Logger(int levels) and displays Log Info as methods but no attributes of it

The client class which is Chain of Responsibility Client has de chain() and main methods.

Main logger class
public abstract class Logger {  
    public static int OUTPUTINFO = 1;  
    public static int ERRORINFO = 2;  
    public static int DEBUGINFO = 3;  
    protected int levels;  
    protected Logger nextLevelLogger;  
public void setNextLevelLogger( Logger nextLevelLogger ) 
{  
        this.nextLevelLogger = nextLevelLogger;  
    }  
        public void logMessage( int levels, String msg )
{  
        if(this.levels <= levels){  
            displayLogInfo(msg);  
        }  
        if (nextLevelLogger != null) {  
            nextLevelLogger.logMessage(levels, msg);  
        }  
    }  
    protected abstract void displayLogInfo(String msg);  
}  
Console based logger class
public class DebugBasedLogger extends Logger {  
    public DebugBasedLogger(int levels) {  
        this.levels = levels;  
    }  
    @Override  
    protected void displayLogInfo(String msg) {  
        System.out.println("DEBUG LOGGER INFO: " + msg);  
    }  
}
Error based logger class
public class ErrorBasedLogger extends Logger {  
    public ErrorBasedLogger(int levels) {  
        this.levels=levels;  
    }  
    @Override  
    protected void displayLogInfo(String msg) {  
        System.out.println("ERROR LOGGER INFO: " + msg);  
    }  
}
Client class / main class
public class ChainofResponsibilityClient {  
    private static Logger doChaining(){  
          Logger consoleLogger = new ConsoleBasedLogger(Logger.OUTPUTINFO);  
            
          Logger errorLogger = new ErrorBasedLogger(Logger.ERRORINFO);  
          consoleLogger.setNextLevelLogger(errorLogger);  
            
          Logger debugLogger = new DebugBasedLogger(Logger.DEBUGINFO);  
          errorLogger.setNextLevelLogger(debugLogger);  
            
          return consoleLogger;   
          }  
          public static void main(String args[]){  
          Logger chainLogger = doChaining();  
  
              chainLogger.logMessage(Logger.OUTPUTINFO, "Enter the sequence of values ");  
              chainLogger.logMessage(Logger.ERRORINFO, "An error has occurred now");  
              chainLogger.logMessage(Logger.DEBUGINFO, "This was the error now debugging is compiled");  
              }  
}  
Output:
  1. AbilityClient  
  2. CONSOLE LOGGER INFO: Enter the sequence of values  
  3. CONSOLE LOGGER INFO: An error has occurred now  
  4. ERROR LOGGER INFO: An error has occurred now  
  5. CONSOLE LOGGER INFO: This was the error now debugging is compiled  
  6. ERROR LOGGER INFO: This was the error now debugging is compiled  
  7. DEBUG LOGGER INFO: This was the error now debugging is compiled

Conclusion

So in this blog, we focused learning on the chain of responsibility pattern which is one of the behavioral design patterns, its uses, advantages, and also when to use these design patterns, etc. along with an example.

Author Bio

Author

Author Name: Yash Shukla

 

Yash Shukla is an adroit Java professional. With technical acumen and a sharp mindset, he always focuses on learning opportunities to tune his growth. He believes in self-development and periodically shares his insights on various platforms.