Blog for Software and Technology
  • Blog
  • About
  • Contact

TUTORIAL ALERT: CHAIN OF RESPONSIBILITY PATTERN WITH JAVA

1/20/2022

1 Comment

 
Picture
​In a java outsourcing company, developers design their code efficiently to make it more reliable with the help of design methodologies. In this article, you will learn about the chain of responsibility pattern used by professionals to make the code more reliable.

Introduction
​
In Java, to make the code more reliable we should design our code efficiently which can be done by applying design methodologies such as OOPs, OOAD, design principles, and patterns into applications.
 
Chain of Responsibility pattern is a behavioral design pattern that actually makes the code lose coupling. It decouples the sender of the request to the receiver, where the chain of objects are there to process the request and the request used to starts or process with the first object in the chain, it proceeds if that object has the solution otherwise the request is forwarded to next object in the chain in order to handle or process that request. This chaining will happen until finding the solution object is in the chain.
 
GOF states that – it “Gives more than one object an opportunity to handle a request by linking receiving objects together.”
Picture

Handler – interface to handle the requests
 
Concrete Handler – A request handler that handles the request that they are responsible for, otherwise it sends the request to its successor.
 
Client – send the request to the handler to process the request. The first handler object in the chain would be invoked by the client.
 
Fore.g.
 
java.util.logging.Logger, javax.servlet.Filter are the examples that are followed by a chain of responsibility design pattern.
Let us take an example of Gmail, where we could see the mails are categorized as Primary, Social, and Promotion.
 
Social – Emails from Facebook, Twitter, Google plus are in the social category

Promotion – Emails from Paytm, Flipkart, Amazon offers are in the promotion category

Primary – Other emails from people and friends are companies are in the primary category

We can also have our own folder or category or rule for the basis of the incoming email on the subject, from, to, and all.
So, how could we do this in code with good design?

// Handler interface
 
public interface GmailHandler {
 
   public void setNext(GmailHandler gmailHandler); //to set next handler
 
   public void handleRequest(Email email); //to handle or process the emails
 
}
// Request object
 
public class Email {  
 
   private boolean is Social;
 
   private boolean is Promotion;
 
   private boolean is Primary;
 
   private Boolean is Spam;
 
   public boolean is Social() {
 
         return is Social;  // returns true when emails from facebook, twiiter, google plus
 
    }
 
  public boolean is Promotion() {
 
         return is Promotion; // returns true when emails from paytm, flipkart, amazon offers     
 
    }
 
  public boolean is Primary () {
 
        return is Primary; // returns true when emails not from social and shopping sites      
 
    }
 
   public boolean is Spam() {
 
         return is Spam;      
 
    }
 
   // more members and methods on content of email and other details
 
}
// Concrete Handler or Request Handler
 
public interface SocialEmaiHandler {
         
        private GmailHandler handler;

public void setNext(GmailHandler handler) {
 
        this.handler = handler;
 
   }
 
   public void handleRequest(Email email) {
 
         if(email.isSocial()) {
 
                    // proceeds or handles social email contents, keeps in social category
 
          } else {
 
                   handler.handleRequest(); // proceeding with next handler in the chain
 
          }
 
   }
 
}
// Concrete Handler or Request Handler
 
public interface PromotionEmailHandler {
 
   private GmailHandler handler;
 
   public void setNext(GmailHandler handler) {
 
        this.handler = handler;
 
   }
 
  public void handleRequest(Email email) {
 
         if(email.isPromotion()) {
 
                    // proceeds or handles promotion email contents, keeps in promotion category
 
          } else {
 
                   handler.handleRequest(); // proceeding with next handler in the chain
 
          }
 
   }
 
}
// Concrete Handler or Request Handler
 
public interface PrimaryEmailHandler {
 
   private GmailHandler handler;
 
   public void setNext(GmailHandler handler) {
 
        this.handler = handler;
 
   }
 
   public void handleRequest(Email email) {
 
         if(email.isPrimary()) {
 
                    // proceeds or handles primary email contents, keeps in primary category
 
          } else {
 
                   handler.handleRequest(); // proceeding with next handler in the chain
 
          }
 
   }
 
}
// Client
 
public class EmailClient {
 
  private GmailHandler handler;
 
  public EmailClient() {
 
        createProcessChain();
 
  }
 
  public void addRule(GmailHandler handler){
 
       if(this.handler == null)
 
             this.handler = handler;
 
       else
 
                this.handler.setNext(handler);
 
  }
 
  public void sendRequest(Email email){
 
       this.handler.handleRequest(email);
 
  }
 
  public void createProcessChain() {
 
       addRule(new SocialEmailHandler());
 
       addRule(new PromotionEmailHandler());
 
       addRule(new PrimaryEmailHandler());      
 
  }
 
  public static void main(String[] aa){
 
      EmailClient client = new EmailClient();
 
      Email email = new Email();
 
      email.setIsPromotion(true);
 
      client.sendRequest(email);
 
      Email email = new Email();
 
      email.setIsPrimary(true);
 
      client.sendRequest(email);
 
      Email email = new Email();
 
      email.setIsSocial(true);
 
      client.sendRequest(email);
 
  }
 
}

Conclusion: 
​
Just think that, if we didn’t follow this chain of responsibility design pattern how would we achieve this solution? We would write if and else if block on each case or category!!?? If we do so, it creates an n-path complexity problem to our code and it is very difficult to change the code to adapt a new feature i.e. each time we have to rework with the implementation.

So, the Chain of responsibility pattern helps us to make the code loose coupling and easy to adopt a new feature i.e. if we want to add a new rule or category like spam to email simply we can add a new concrete handler called SpamEmailHandler that implements Handler interface and most importantly we are adding the feature to our code without touching the existing implementation.
 
As you now know that chain of responsibility is a behavioral design pattern used by Java outsourcing company professionals to design the code. Any point you did not get can be asked in comments.

Related Articles:
How to Use Spring Batch and Connect It to Database in Java Development?How To Benefit Out Of Mobile Responsive Java Application Development And Make It Big?
1 Comment

Why You Should Outsource Your Java Development Work

5/28/2015

6 Comments

 
The corporate world is getting more dependent on IT- the outsourcing software development practices have made an intense contribution to IT development and growth. More businesses have started considering offshore software development companies to outsource their dream project. Why? Do they get any benefit from it? Maybe, but their business surely gets on high. Outsourcing to offshore vendors is pretty inexpensive and time-saving. You could save much time and use it in the promotion and marketing of your services and products
Outsourcing your Java development work eliminates the frustration and time related to finding and analyzing candidates’ skills. The development team starts working on your project with a single phone call. You don’t have to worry when you hire experienced professionals because they know, what are your desires and requirements. They understand your business and accordingly make a design for the application. Associating with a premium partner ensures that each new resource has been tested and scrutinized by the team.

Outsource Java Development Company allows, the development team to rapidly determine the effectiveness of new employee and quickly on-board employees. If everything is under control, you can get new resource reporting with a single call.
Picture
Why Did Most Organizations believe In Outsourcing?

These are the three key reasons that, outsourcing decides:

1.       Lack of experienced staff to handle business process

2.       Lesser availability of cheaper labor

3.       Feasibility and ability to focus on other business processes that are extremely crucial for business growth.


Due to all of these three factors, these organizations generally outsource to offshore development partners and get their work done. Technical expertise, excellent communication capabilities, and suitable packages are major benefits you can avail while outsourcing Java development work to India.

Read more - JEP 359: The Best New Thing for a Long Time for JAVA

What Benefits You Can Avail?​
1. Expertise and swiftness
Most of the time companies outsource their tasks to vendors who are experienced enough to deliver the exact output. The outsourced vendor possesses specialized skills and tools set that assist the developers during development and completion of tasks.

2. Core process handling
By outsourcing Java services, companies can have more time to work on their core processes and strengthen them simultaneously.

3. Risk Sharing
Risk sharing is a crucial factor because outsourcing helps companies to shift responsibilities completely or partially to the outsourced vendor.

4. Low operation cost
Not all organizations need, in-house IT development team and thus they outsource. Outsourcing development services elude the requirement to hire experts, in-house and in this way, you are one step ahead to low operation cost.​

5. You can get a fresh perspective
IT consultants can bring you a fresh perspective when you outsource to them. The Java web development team can provide unique ideas, intuitive approaches, and brilliant results that one can ​never expect from a novice or fresher.

See more at - 
  • Spring Boot for Apache Geode and Pivotal GemFire Released (Ver. 1.2.0)
  • Introduction JAX-RS Security for Restful Services Using Java 8

6 Comments

    RSS Feed

    SoftWeb Solution

    Aegis Soft Tech is a well-known name in IT industry. We have stable team of resources working on various technologies for the last 10 years. We are also prominent as 'Future of IT ' in India. Today, we are giving quality IT outsourcing services to our clients with wonderful exposure to different IT domains.

    Categories

    All
    3tier-app
    Angular 2
    Angular JS 2
    Aspnet Development
    Aspnet-development
    Big Data
    Java
    Java Developers
    Java Developers India
    Java Development
    Java Development News
    Java Development Team
    Java EE Development
    Java Outsourcing
    Java Programmers
    JavaScript
    Java Update
    Java Web Development
    .Net Development
    Outsource Your Java Development
    Python Development
    ServiceNow
    Software Development

Powered by Create your own unique website with customizable templates.