介绍代码:松耦合例如
public interface Course {
/**
* returns the number of units (hours) a specific course is
*/
public int units();
/**
* returns the number of students signed up for the course
*/
public int numOfStudents();
/**
* returns the maximum number of students the course can have signed up to it
*/
public int maxNumStudents();
/**
* returns whether or not the student is enrolled in the course
*/
public boolean registered(Student s);
/**
* enrolls s in the course
*
* @pre this.registered(s) == false
* @pre this.numOfStudents() < this.maxNumStudents()
*
* @post this.registered(s) == true
* @post this.numOfStudents() == $prev(this.numOfStudents()) + 1
*/
public void register(Student s);
}
public interface Student {
/**
* enrolls the student in course c
*
* @pre c.registered(this) == false
* @pre c.numOfStudents() < c.maxNumStudents()
* @pre this.totalUnits() + c.units() <= 10
*
* @post c.registered(s) == true
* @post this.totalUnits() == $prev(this.totalUnits()) + c.units()
*/
public void register(Course c);
/**
* return the total number of units this student currently is enrolled in
*
* @pre true
* @post 0 <= $ret <= 10
*/
public int totalUnits();
}
在示例代码IM
试图描述两个单独的实体(接口/类/无论),其在一方面应为(我想,至少)松散地耦合,但另一方面确实取决于彼此并且需要彼此的某种知识。
在上面的场景中,我需要第三个类,它们实际上将它们组合到一个工作系统中。它的丑陋,因为从现在起,上面的定义是松散耦合的 - student.register(c)只改变学生对象,而course.register(s)只改变课程对象。所以统一类将不得不运行s.register(c)和c.register(s)。
虽然如果我重新记录所有的register()逻辑到一个类然后我紧紧地把它们联系起来。
有没有更清晰的设计方法?