Article Directory
-
- 1. Common naming conventions
- 2. Through interface-oriented programming (to achieve module decoupling)
-
- 3.1 Normal call, the controller calls the implementation class of the service
- 3.2 Optimization 1, Declare objects through the interface ʻIPersonService` to achieve decoupling on the left side of the equal sign
- 3.3 Optimization 2, to achieve decoupling on the right side of the equal sign through Spring container injection and automatic loading
1. Common naming conventions
Interface definition ( IXxx , add before the interface I
to 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 Impl
to distinguish, put it in the corresponding impl
package)
- 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.
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 PersonServiceImpl
commented out the class, just these few lines of code, the coupling degree is as high as three, it must not work.
3.2 Optimization 1. IPersonService
Declare 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 PersonServiceImpl
look at the coupling through the comments . At this time, there is only one coupling (coupling on the right side of the equal sign)
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 PersonServiceImpl
to 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.