1, Foreword
- Usually, many people think that the design pattern principle is six, and I have written seven here. Don't think it's a mistake. Don't say anything superfluous. Just look down.
2, Single responsibility principle
2.1 concept
- For classes, that is, a class should be responsible for only one responsibility. If category A is responsible for two different responsibilities: Responsibility 1 and blame 2. When the requirement of responsibility 1 is changed and a is changed, the implementation error of responsibility 2 may be caused. Therefore, class a needs to be decomposed into A1 and A2.
2.2 code demonstration
- The following is an example of the operation of vehicles.
Now there is a class that is responsible for the operation of vehicles. As follows, but assuming that the aircraft is passing in now, the aircraft will run on the ground. However, this situation is obviously wrong.
public class Vehicle{ public void run(String name) { Sysout.out.printfln(name + ": Running on the ground"); } }
- Solution: separate the responsibilities of each class.
- Vehicle interface
public interface Vehicle{ public void run(String name); }
- Flying tools
public class AirVehicle implements Vehicle{ @Override public void run(String name) { System.out.println(name + "Flying in the sky"); } }
- Underwater vehicle
public class WaterVehicle implements Vehicle{ @Override public void run(String name) { Sysout.out.println(name + "Running in the water"); } }
- Land transportation
public class LandVehicle implements Vehicle{ @Override public void run(String name) { Sysout.out.println(name + "Running on land"); } }
- Test class
public class Test{ public static void main(String[] args) { LandVehicle lv = new LandVehicle(); lv.run("automobile"); } }
By separating the functions of each class, we can realize the purity of the class, that is, the principle of single responsibility;
2.2. Single responsibility principle, precautions and details
- Reduce class complexity. A class is responsible for only one responsibility.
- Improve the readability and maintainability of the class.
- Reduce the risk caused by class changes.
- Generally, we should abide by the principle of single responsibility. Only when the logic is simple enough can we violate the principle of single responsibility at the code level. Only if the number of methods in the class is small enough to maintain the principle of single responsibility at the method level.
3, Interface isolation principle
3.1 basic introduction
- The client should not rely on interfaces that it does not need, that is, the dependence of one class on another should be based on the smallest interface.
3.2 problems of traditional methods and improvement of interface isolation principle
- Class a relies on class B through interface, and class C relies on class D through interface. If the interface is not the smallest interface for class A and class C, class B and class D must 2 implement methods they do not need;
- Split the interface into several independent interfaces. Class A and class C establish dependencies with the interfaces they 1 need. That is, the principle of interface isolation is adopted.
3.3 code demonstration
- Before the formal start, give a problematic example to facilitate understanding.
- ClassB depends on ClassA and calls func1(); However, you only need to call func1 () method, but ClassA needs to implement all methods in the interface, so writing in this way will make Class too complex.
public interface Interface{ public void func1(); public void func2(); public void func3(); public void func4(); public void func5(); }
public class ClassA implements Interface{ @Override public void func1() { } @Override public void func2() { } @Override public void func3() { } @Override public void func4() { } @Override public void func5() { } }
public class ClassB{ public void depend1(Interface i) { i.func1(); } }
public class Test { public static void main(String[] args) { ClassB b= new ClassB(); b.depend1(new ClassA()); } }
- Therefore, the principle of interface isolation is proposed. In fact, the so-called interface isolation principle is to decompose an interface and divide methods into different interfaces.