Optimization (decoupling), decoupling is achieved through interface-oriented programming and Spring's @Autowired automatic loading.

1. Common naming conventions

Interface definition ( IXxx , add before the interface Ito distinguish)

  • Service layer interface naming: com.xxx.service.IXxxService
  • Dao layer interface naming: com.xxx.dao.IXxxDao

The definition of the implementation class (XxxImpl, after the name is added Implto distinguish, put it in the corresponding implpackage)

  • Service layer interface naming: com.xxx.service.impl.XxxServiceImpl
  • The Dao layer interface is named: com.xxx.dao.impl. XxxDaoImpl

2. Through interface-oriented programming (to achieve module decoupling)

Judge the coupling relationship between classes: The simplest way is to comment/delete. Comment out all the other classes called in this class and see how many errors there are.
Insert picture description here

Assuming the following relationship

public interface IPersonService{
    
    }//service接口
public class PersonServiceImpl implements IPersonService{
    
    } //service接口的实现类
public class PersonController{
    
    } //controller调用service

3.1 Normal call, the controller calls the implementation class of the service

public class PersonController {
    
    

    //直接调用接口的实现类
    PersonServiceImpl personService= new PersonServiceImpl();
    public int findPersonByName(Person person){
    
    
        return personService.findPersonByName(person);
    }
}

Check the coupling situation, we PersonServiceImplcommented out the class, just these few lines of code, the coupling degree is as high as three, it must not work.
Insert picture description here

3.2 Optimization 1. IPersonServiceDeclare objects through interfaces to achieve decoupling on the left side of the equal sign

public class PersonController {
    
    

    //通过调用接口来实现等号左边解耦
    IPersonService personService= new PersonServiceImpl();

    public int findPersonByName(Person person) throws InterruptedException {
    
    
        return personService.findPersonByName(person);
    }
}

Let's PersonServiceImpllook at the coupling through the comments . At this time, there is only one coupling (coupling on the right side of the equal sign)
Insert picture description here

3.3 Optimization 2, to achieve decoupling on the right side of the equal sign through Spring container injection and automatic loading

public class PersonController {
    
    

    //通过@Autowired自动装载,它会从容器中找对应类型的bean将其注入赋值给personService
    @Autowired
    IPersonService personService;
    //就相当于执行了 IPersonService personService= new PersonServiceImpl();

    public int findPersonByName(Person person) throws InterruptedException {
    
    
        return personService.findPersonByName(person);
    }
}

Continue PersonServiceImplto check whether there is a coupling situation through the annotations , there is no error, so there is no coupling between the two classes, and the current PersonController and PersonService are not coupled.

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40542534/article/details/108977009