Big brother beep beep beep

The chain of responsibility design pattern is very common and important in daily work. Many open source frameworks adopt the chain of responsibility design pattern, such as; Filter in servlet, Filter in Dubbo, Plugin in Mybatis, HandlerExecutionChain in Spring MVC, etc., all adopt the responsibility chain design pattern.

If this article is helpful to you, please help the elder brother to click “watching” and “like”, if you can share with your friends, that would be even better, hehe

Procedural thinking triggered by having a baby



What is the chain of responsibility

We can understand it as: what to do first, what to do later, several things to form an ordered linked list.

Just like the above scene of giving birth to a baby, to get married first – > bridal chamber – > pregnancy – > giving birth to a baby.

Implement the chain of responsibility design pattern

Raw code logic

public static void main(String[] args) {
    String status = "Marriage Certificate";
    if(status.equals(married)) {        status = "Bridal chamber";
    }
 if(status.equals(bridal chamber)) { status = "Pregnant";  }  if(status.equals(pregnancy)) { status = "We have a baby.";  }  System.out.println(status); } Copy the code

Transformation with responsibility chain model

  • Construct each node in the chain of responsibility
/ / total interface
public interface Filter {
    String doFilter(String status);
}

// Create each process node in the chain of responsibility  classTo get marriedFilter implements Filter {  @Override  public String doFilter(String data) {  return "The wedding is over. It's time for the wedding.";  } }  classBridal chamberFilter implements Filter {  @Override  public String doFilter(String data) {  return "Wedding is over. It's time to get pregnant.";  } }  classpregnancyFilter implements Filter {  @Override  public String doFilter(String data) {  return "Pregnancy over, time to have a baby.";  } } classGave birth to the babyFilter implements Filter {  @Override  public String doFilter(String data) {  return "The baby is here.";  } } Copy the code
  • The chain that constructs the chain of responsibility
public class FilterChain {

    / / chain
    private List<Filter> filterList = Lists.newLinkedList();

 // Add execution nodes to the chain  public FilterChain(a) {  filterList.add(newMarried the Filter ()); filterList.add(newBridal chamber Filter ()); filterList.add(newPregnancy Filter ()); filterList.add(newBaby the Filter ()); }   // Execute the node on the chain  public String processData(String status) {  String str = null;  for (Filter filter : filterList) {  str = filter.doFilter(status);  }  return str;  } } Copy the code
  • Start having babies
public static void main(String[] args) {
    String str = new FilterChain().processData("Marriage Certificate");
    System.out.println(str);
}
Copy the code

You may think that if/else can be done, why do we have to create so many classes, the above is a simulation scenario, the real business scenario is far more complex.

This design pattern can decouple programs, clarify responsibilities, improve readability and maintainability of programs. This is an essential thought for advanced programmers.

Speech in life

It is so easy to give birth to a baby. Haha, a responsibility chain design mode is done. If you are also a responsible man, you will not be ashamed of the responsibility chain design mode.

The above story is pure fiction, not responsible for the birth of children

IT brother

A big factory to do advanced Java development program ape

Follow wechat public account: IT elder brother

Java foundation, Java Web, JavaEE all tutorials, including Spring Boot, etc

Reply: Resume template, you can get 100 beautiful resumes

Reply: Java learning route, you can get the latest and most complete a learning roadmap

Re: Java ebooks, get 13 must-read books for top programmers