One article lets you read "Seven Principles of Design Mode - Single Responsibility Principle"

Posted by Edward on Wed, 10 Jun 2020 04:10:46 +0200

Just a week before the end of the last round of overtime work, the leader came to tell me: "Birds, weekends and overtime work are better match!"Poor children have new urgent tasks, and the good overtime life is starting again.

NoThis is not the life that newbies want. Newbies want to resist. Newbies decide to resign.This does not stealthily take out the mobile phone to log in to various recruitment websites for browsing, suddenly find how to recruit also requires design mode?To do this, quickly take out the already dusty data to make up for it.Ouch!Help me up to work overtime before that.

The above content is pure fiction (except overtime). If the same, please see below.

Today let's take a look at one of the seven principles of design mode - the single responsibility principle.

Basic concepts

For java classes, the single responsibility principle is that a class is responsible for only one responsibility, or a function.

Benefits of a single responsibility

1. A class is responsible for only one responsibility. The most direct benefit is to reduce the complexity of the class.
2. The single responsibility will improve the readability of classes, improve the maintainability of programs, and reduce the later maintenance costs.
3. Because it is responsible for only one function, it will not affect other functions when code changes, thus greatly reducing the risk of changes.

That's all there is to theoretical knowledge. Let's take a look at a sample code to deepen our understanding of the single responsibility principle.

Code Time

Sample code that violates the single responsibility principle:

public class SingleResponsibilityDemo1 {

  public static void main(String[] args) {
    Eat eat = new Eat();
    eat.eat("Tiger");
    eat.eat("Goat");
  }
}

/**
 * Dining
 */
class Eat {
  
  public void eat(String animalName) {
    if ("Tiger".equals(animalName)) {
      System.out.println(animalName + ",In gobbling meat!");
    } else if ("Goat".equals(animalName)) {
      System.out.println(animalName + ",Chewing your slowly grazing!");
    }
  }
}

Note: Meat eating and grass eating are two kinds of behaviors, which are written together and typically violate the principle of single duty.This design adds an else if if if if to add the ability to eat other foods later. When the function increases and the logic is complex, the code becomes very bulky and difficult to understand and maintain.

Sample code for class level compliance with single responsibility principle:

public class SingleResponsibilityDemo2 {

  public static void main(String[] args) {
    EatMeat eatMeat = new EatMeat();
    eatMeat.eatMeat("Tiger");
    EatGrass eatGrass = new EatGrass();
    eatGrass.eatGrass("Goat");
  }
}

// Eating meat
class EatMeat {

  public void eatMeat(String animalName) {
    System.out.println(animalName + ",In gobbling meat!");
  }
}

// Grazing
class EatGrass {

  public void eatGrass(String animalName) {
    System.out.println(animalName + ",Chewing your slowly grazing!");
  }
}

Sample code that adheres to the single responsibility principle at the method level:

public class SingleResponsibilityDemo3 {

  public static void main(String[] args) {
    Eat eat = new Eat();
    eat.eatMeat("Tiger");
    eat.eatGrass("Goat");
  }

}

class Eat {

  public void eatMeat(String animalName) {
    System.out.println(animalName + ",In gobbling meat!");
  }

  public void eatGrass(String animalName) {
    System.out.println(animalName + ",Chewing your slowly grazing!");
  }
}

individual opinion

1. Although a single responsibility is good, it cannot be carried over by itself, or it will cause an increase in classes and add additional maintenance costs.
2. When the logic of the code is simple enough, we can violate the single responsibility principle at the code level;
3. When the number of methods in a class is small and the business logic is not particularly complex, you can violate the principle of single responsibility at the class level and simply sink down to the method level.
4. We should use the principle of single responsibility flexibly according to needs and actual conditions.

Overall, the principle of single responsibility needs to be applied flexibly, considering whether it is observed at the class level or at the method level in light of the actual situation.

That's all for today's sharing. I feel like the article is well written. Remember to pay attention to it!

Topics: Mobile Java