Java中的低耦合高内聚法则

java框架模式_低耦合高内聚法则

定义:一个对象应该对其他对象保持最少的了解。
问题由来:类与类之间的关系越来越密切,耦合度越来越大,当一个类发生改变时,对另外一个类的影响也越大.
解决方案:尽量降低类与类之间的耦合.

案例:在一所学校,里面有老师若干名,依次编号。下面有学生若干名,一次编号。现在要求打印出所有的老师和学生的ID.

(1)违反低耦合高内聚原则的代码,如下:

import java.util.ArrayList;
import java.util.List;

class Teacher{
   privateString id;
   publicvoidsetId(String id)
   {
     this.id=id;
   }
   publicString getId()
  {
     return id;
  }
}
class Student{
   private String id;
   public void setId(String id)
   {
      this.id=id;
   }
   public String getId()
   {
      return id;
   }
}
class StudentManage{
   publicList  getAllStudent()
   {   List list=newArrayList();
       for(int i=0;i<100;i++)
       { Student student=new Student();
         student.setId("学生学号是"+i);  
         list.add(student);  
       }

      return list;
   }

}
class TeacherManage{ 
   publicList getAllTeacher()
   {
    List list=newArrayList(); 
    for(inti=0;i<100;i++)
    { Teacher teacher =new Teacher();
      teacher.setId("老师编号"+i); 
       list.add(teacher);
    }

   return list;
   }
   public void printAllPerson(StudentManagestudentmanager)
   {
      Listlist1=studentmanager.getAllStudent();
      for(Student s:list1)
      {
        System.out.println(s.getId());
      }
      Listlist2=this.getAllTeacher();
      for(Teacher t:list2)
      {
        System.out.println(t.getId());
      } 
   }
}

public classClient {
 publicstaticvoidmain(String[] args) {
   TeacherManagetm=newTeacherManage();
   tm.printAllPerson(new StudentManage());  

}
}

现在这个设计的主要问题出现在TeacherManage类中,根据低耦合高内聚法则,Student类与TeacherManage类发生耦合。

(2)修改之后的代码如下:

import java.util.ArrayList;
import java.util.List;

class Teacher{
   privateString id;
   publicvoidsetId(String id)
   {
     this.id=id;
   }
   publicString getId()
  {
     return id;
  }
}
class Student{
   private String id;
   public void setId(String id)
   {
      this.id=id;
   }
   public String getId()
   {
      return id;
   }
}
class StudentManage{
   publicList  getAllStudent()
   {   List list=newArrayList();
       for(int i=0;i<100;i++)
       { Student student=new Student();
         student.setId("学生学号是"+i);  
         list.add(student);  
       }

      return list;
   }
   public void printAllStudent()
   {
      Listlist1=this.getAllStudent();
      for(Student s:list1)
      {
        System.out.println(s.getId());
      }
   }

}
class TeacherManage
{ publicList getAllTeacher()
   {
    List list=newArrayList(); 
    for(inti=0;i<100;i++)
    { Teacher teacher =new Teacher();
      teacher.setId("老师编号"+i); 
       list.add(teacher);
    }

   return list;
   }
   publicvoidprintAllTeacher()
   {
     List list2=this.getAllTeacher();
     for(Teacher t:list2)
     {
        System.out.println(t.getId());
     }
   }


}

public classClient {
 publicstaticvoidmain(String[] args) {
   TeacherManagetm=newTeacherManage();
   tm.printAllTeacher();
   StudentManagesm=newStudentManage();
   sm.printAllStudent();
}
}

修改后,学生新增加了学生ID的方法,老师直接来调用即可。从而避免了与学生发生耦合。低耦合高内聚原则的初衷是降低了类之间的耦合,由于每个类减少了不必要的依赖,因此的确可以降低耦合关系。采用此法则可以做到结构清晰,高内聚低耦合、

你可能感兴趣的:(Java)