Care from the novice

As the weather is getting hotter and hotter, everyone is not already on the “left hand small Popsicle, the right hand big watermelon, blowing a small air conditioner, laugh” of the day? But still want to remind everyone here, cold drinks, air conditioning although good, but must be moderate ah!

This is not the rookie because of excessive joy, extreme joy of sorrow, acute gastroenteritis to find. Loose bowels, pull to doubt life, go to the toilet, scalp pins and needles, the whole person has collapsed. So we must pay more attention, do not eat bad stomach like the rookie.

Although physically uncomfortable, but the newbie still insisted on writing the article. Remember to like and follow the newbie for working so hard!

The basic concept

Professional explanation:

1. A client should not rely on interfaces it does not need. 2. The dependency of one class on another should be based on the smallest interface.

Is it like rain and fog? That’s right! Professional only if it’s hard to understand!

All right, all right, just kidding! Let the newbie explain.

An interface should be assigned only one responsibility (role), and different responsibilities cannot be assigned to the same interface.

Benefits of the interface isolation principle

1. Simplify the responsibilities of interfaces so as to effectively avoid interface pollution. 2. When an interface has too many methods, it will often cause some idle methods in the class using the interface, resulting in code redundancy. This phenomenon can be effectively avoided by subdividing the interface. 3. The flexibility of the code can be improved. Just like building blocks, we can split a large interface into several smaller interfaces, and different small interfaces can have various combinations. 4, make the program high cohesion, low coupling.

Note: although it is good to refine interfaces, it is not necessary to blindly split them, otherwise the number of interfaces will increase and the maintenance cost of the system will increase.

Code sample

Let’s start with code that doesn’t follow interface isolation

public interface School {

  /** ** class */
  void attendClass(a);

  /** * class is over */
  void afterClass(a);

  /** * learn */
  void learn(a);

  /**
   * 讲课
   */
  void lecture(a);
}
Copy the code

In the code above, suppose you have a class called People, whose role is that of a student, implementing the School interface. It will be forced to implement the “lecture” method, which it does not need. This leads to redundancy and makes our code bloated.

According to the interface isolation principle, we can divide the large interface into three smaller interfaces.

School Interface:

public interface School {

  /** ** class */
  void attendClass(a);

  /** * class is over */
  void afterClass(a);

}
Copy the code

Teacher interface:

public interface Teacher {

  /**
   * 讲课
   */
  void lecture(a);
}
Copy the code

Student interface:

public interface Student {

  /** * learn */
  void learn(a);
}
Copy the code

By doing this, we can avoid redundant code and make our code more flexible.

The above rookie on the interface isolation principle of a point of their own views, if the article there to write unreasonable hope we can put forward. Another thing to remind you is: everything has pros and cons, we need to learn to balance the pros and cons, find the best solution, make the program better. Ok, today’s share is over here, feel the article is written well remember to give a thumbs up and follow you, finally WISH everyone a happy weekend!