Single responsibility of the seven design principles of java, the principle of interface isolation (combination of text and code to understand)

Whenever you do, it is best to pursue only one goal that you care about the most, and you can give in to the rest, so that the chances of achieving the goal are high. For example: To do this, the most important thing is to learn experience, then don't care about money; to do that, the most important thing is money, then don't care about face. And so on. If you do something and want to learn experience, you have to make a lot of money and have face. It's such a beautiful thing, you have to wait.

An efficient app comes from the programmer's handling of details, as well as the specification of code, readability, etc.It is often heard that design patterns are used. Design patterns have seven design principles, and all design patterns must comply with these seven Let's start with the big design principles , and today I will take you to understand the seven design principles.

What are the seven design principles?

  • Single responsibility principle
  • Interface isolation principle
  • Reversion principle
  • Richter substitution principle
  • Principle of opening and closing
  • Dimit's Law
  • Synthetic reuse principle

Usually everyone understands the first six, and there is no synthetic reuse principle

Why use the seven design principles?

  • Code reusability (the same code does not need to be written multiple times);
  • Readability (standardization of programming, easy for other programmers to read and understand);
  • Extensibility (when new functions need to be added, it is very convenient, also called maintainability);
  • Reliability (when we add new functions, it will not affect the original functions);
  • Make the program present the characteristics of high cohesion and low coupling;

Single responsibility principle

There is only android studio on my computer, so the android project is running, and eclipse is not installed, so there is no java project

Ordinary code:

SingleBean singleBean = new SingleBean();
singleBean.run("小明");
singleBean.run("钢铁侠");

public class SingleBean {
    
    
    public void run(String title){
    
    
        Log.i("SingleBean",title+"在看电视..");
    }
}

效果图(1.1):
Insert picture description here

Problems:

  • Although there is no problem with the code, and no error is reported, if it is written like this, the SingleBean class has two operation logics.
    • Xiao Ming is watching TV...
    • Iron Man is watching TV...

If I want to change the logic of the code now, is Xiao Ming doing his homework?

SingleBean singleBean = new SingleBean();
singleBean.run("小明");
singleBean.run("钢铁侠");
        
public class SingleBean {
    
    
    public void run(String title){
    
    
        Log.i("SingleBean",title+"在写作业..");
    }
}

效果图(1.2): As
Insert picture description here
you can see, if you write this way, the code will have a problem

What I want to change is: Xiao Ming is doing homework, Iron Man is watching TV

Modify the code to comply with the single responsibility principle:

Option One:

SingleBean singleBean = new SingleBean();
singleBean.run("小明");
singleBean.run2("钢铁侠");

public class SingleBean {
    
    
    public void run(String title){
    
    
        Log.i("SingleBean",title+"在写作业..");
    }
    public void run2(String title){
    
    
        Log.i("SingleBean",title+"在看电视..");
    }
}

效果图(1.3):
Insert picture description here
Analysis:

  • Although the single responsibility principle is not observed here, the single responsibility principle is observed in the method.

Option II:

SingleBean singleBean = new SingleBean();
singleBean.run("小明");
        
SingleBean2 singleBean2 = new SingleBean2();
singleBean2.run("钢铁侠");

public class SingleBean {
    
    
    public void run(String title){
    
    
        Log.i("SingleBean",title+"在写作业..");
    }
}

public class SingleBean2 {
    
    
    public void run(String title){
    
    
        Log.i("SingleBean",title+"在看电视..");
    }
}

analysis:

  • The class is disassembled, increasing the amount of code, benefits: the code is more readable
    效果图(1.4):
    Insert picture description here

to sum up:

  • If the single responsibility principle is not strictly followed in the category, then the single responsibility principle must be followed in the method.
  • Reduce the complexity of the class, one class is responsible for one responsibility
  • Improve the readability and maintainability of the class
  • The code is simple enough or highly repetitive, so that it can go beyond the class and follow the single responsibility principle in the method.

Simple and crude understanding:
one class is responsible for one responsibility, not one class is responsible for one responsibility. For
example, I have to eat now, including shopping, cooking, eating, washing dishes, etc. (a class is responsible for one responsibility)
instead of me having to eat, Just simply eat, (one class is responsible for one responsibility)

Interface isolation principle

Let's take a look at the completed case diagram first;

事例图(2.1):

Insert picture description here

  • Red: Class A, which implements interface method1(), method4(), method5() methods
  • Yellow: Type B, implement interface
  • Green: Class C, implement interface method2(), method3(), method4() methods
  • Purple: Class D, implement interface
  • Blue: Iquarantine, the interface isolation implementation class, there are 5 methods method1() to method5()
  • Black arrow: indicates that the corresponding method is implemented through the implementation class
  • Gray arrow: the logo directly implements the interface Iquarantine

Coming code:

Interface class:

public interface Iquarantine {
    
    
    void  method1();
    void  method2();
    void  method3();
    void  method4();
    void  method5();
}

QuarantineA class:

public class QuarantineA {
    
    
  public  void  run(Iquarantine iquarantine){
    
    
      iquarantine.method1();
      iquarantine.method4();
      iquarantine.method5();
  }
}

Quarantine Class B:

public class QuarantineB implements Iquarantine{
    
    
    @Override
    public void method1() {
    
    
        Log.i("Quarantine","QuarantineB类 method1()实现");
    }

    @Override
    public void method2() {
    
    
        Log.i("Quarantine","QuarantineB类 method2()实现");
    }

    @Override
    public void method3() {
    
    
        Log.i("Quarantine","QuarantineB类 method3()实现");
    }

    @Override
    public void method4() {
    
    
        Log.i("Quarantine","QuarantineB类 method4()实现");
    }

    @Override
    public void method5() {
    
    
        Log.i("Quarantine","QuarantineB类 method5()实现");
    }
}

QuarantineC class:

public class QuarantineC {
    
    
    public void run(Iquarantine iquarantine){
    
    
        iquarantine.method2();
        iquarantine.method3();
        iquarantine.method4();
    }
}

QuarantineD class:

public class QuarantineD implements Iquarantine{
    
    
    @Override
    public void method1() {
    
    
        Log.i("Quarantine","QuarantineD类 method1()实现");
    }

    @Override
    public void method2() {
    
    
        Log.i("Quarantine","QuarantineD类 method2()实现");
    }

    @Override
    public void method3() {
    
    
        Log.i("Quarantine","QuarantineD类 method3()实现");
    }

    @Override
    public void method4() {
    
    
        Log.i("Quarantine","QuarantineD类 method4()实现");
    }

    @Override
    public void method5() {
    
    
        Log.i("Quarantine","QuarantineD类 method5()实现");
    }
}

Implementation:

//接口隔离原则
QuarantineA quarantineA = new QuarantineA();
quarantineA.run(new QuarantineB());

QuarantineC quarantineC = new QuarantineC();
quarantineC.run(new QuarantineD());

The run() method of QuarantineA needs to pass an Iquarantine interface, and QuarantineB implements the Iquarantine interface, so just pass QuarantineB() when running directly.

Let's see the result:

效果图(1.5):
Insert picture description here
The problem with this code is obvious:

Failure to observe the principle of interface isolation:

When the QuarantineB class is implemented, because the QuarantineA class only needs
method1(), method4(), method5() methods, method2() and method3() are redundant!

When the QuarantineD class is implemented, method1() and method5() are redundant

Definition of interface isolation principle:

  • The client should not rely on interfaces it does not need
  • The dependencies between classes should be established on the smallest interface

Definition and explanation of interface isolation principle:

  • The principle of interface isolation is a principle that restricts the use of interfaces. It tells us that if we want to use interfaces, the key is isolation. Isolation means breaking off contact and communication.
  • So when we use the interface, what should we isolate? "The client should not-rely on the interface it does not need", the isolation here refers to the isolation of the client from the interface it does not need, that is, the client should not use the interface it does not need
  • "The dependency between classes should be established on the smallest interface", it requires the "minimal interface", that is, there are no redundant methods in the interface, so the isolation here refers to isolation from redundant methods .

The interface isolation principle tells us not to pack a lot of methods into an interface, which will cause the interface to become extremely bloated . Should be based on actual needs, so that only useful methods in the interface , that is to say we must refine our interface .

Use interface to isolate code:

Divided into three interfaces:

interface IquarantineA {
    
    
    void  method1();
    void  method5();
}

interface IquarantineB {
    
    
    void  method4();
}

 interface IquarantineC {
    
    
    void  method2();
    void  method3();
}

QuarantineA class:

public class QuarantineA {
    
    
  public  void  run(QuarantineB quarantineB){
    
    
      quarantineB.method1();
      quarantineB.method4();
      quarantineB.method5();
  }
}

QuarantineB class, implement IquarantineA, IquarantineB:

public class QuarantineB implements IquarantineA,IquarantineB{
    
    
    @Override
    public void method1() {
    
    
        Log.i("Quarantine","QuarantineB类 method1()实现");
    }

    @Override
    public void method4() {
    
    
        Log.i("Quarantine","QuarantineB类 method4()实现");
    }

    @Override
    public void method5() {
    
    
        Log.i("Quarantine","QuarantineB类 method5()实现");
    }
}

QuarantineC class:

public class QuarantineC {
    
    
    public void run(QuarantineD quarantineD){
    
    
        quarantineD.method2();
        quarantineD.method3();
        quarantineD.method4();
    }
}

QuarantineD class, which implements IquarantineB, IquarantineC interfaces:

public class QuarantineD implements IquarantineB,IquarantineC{
    
    

    @Override
    public void method2() {
    
    
        Log.i("Quarantine","QuarantineD类 method2()实现");
    }

    @Override
    public void method3() {
    
    
        Log.i("Quarantine","QuarantineD类 method3()实现");
    }

    @Override
    public void method4() {
    
    
        Log.i("Quarantine","QuarantineD类 method4()实现");
    }
}

In this way, the corresponding class implements the corresponding method, the interface is not bloated, and the interface isolation principle is complied with!

效果图(1.6):
Insert picture description here

to sum up:

  • Avoid data pollution caused by the interface,
  • Improve the flexibility of the code. One class implements multiple interfaces, so a bloated interface is divided into several small interfaces, and more needs can be met through different combinations of small interfaces
  • High cohesion, high cohesion is to improve the processing capabilities of interfaces, classes, and modules, and reduce external interaction

Single responsibility principle code

Interface isolation principle code

you may also like:

Reliance inversion of the seven design principles of java, the principle of Liskov substitution (combination of text code to understand)

The Dimit principle, the principle of opening and closing, and the principle of composite reuse (combined understanding of text codes) of the seven design principles of java

Android studio UML modeling

Go to the design pattern/design principle catalog page

Originality is not easy, your likes are your greatest support for me, please like to support it~

Guess you like

Origin blog.csdn.net/weixin_44819566/article/details/112178083