七大设计原则之单一职责原则
介绍
一个类只负责一项职责
比如 dao 层
案例
1、类的级别上实现单一职责
public class SingleResponsibilityPrincipleDemo {
public static void main(String[] args) {
UserDao userDao = new UserDao();
userDao.getUserInfo();
VisitDao visitDao = new VisitDao();
visitDao.getVisitInfo();
}
}
class UserDao{
public void getUserInfo(){
System.out.println("获取用户信息");
}
}
class VisitDao{
public void getVisitInfo(){
System.out.println("获取访问信息");
}
}
2、方法的级别上实现单一职责
public class SingleResponsibilityPrincipleDemo1 {
public static void main(String[] args) {
UserInfo userInfo = new UserInfo();
userInfo.getUserBaseInfo();
userInfo.getUserDetailInfo();
}
}
class UserInfo{
public void getUserBaseInfo(){
System.out.println("获取用户基础信息");
}
public void getUserDetailInfo(){
System.out.println("获取用户详细信息");
}
}