spec 规约
是使用者(client)和实现者(implementor)之间的妥协与契约,即是一种在使用时必须要遵循的要求的说明,也是一种在在实现时需要遵循的原则.
一般书写在每一个类和每一方法前面.
由@param、@throws、@return组成.
/**
* Insert a interval with its start, end, and label
* Labeled intervals conflict if: they have the same label with different
* intervals, or they have different labels with overlapping intervals.
*
* For example, if this set is { "A"=[0,10), "B"=[20,30) },
* insert("A"=[0,10)) has no effect
* insert("A"=[10,20)) throws IntervalConflictException
* insert("B"=[10,20)) throws IntervalConflictException
* insert("C"=[20,30)) throws IntervalConflictException
* insert("D"=[30,40)) adds "D"=[30,40)
*
* @param start the start of the interval(the low end of the interval)
* @param end the end of the interval(the high end of the interval)
* @param label the label to add
* @throws IntervalConflictException -if the label is already existed,
* and the start and end are not the same with the new ones;
* or there's a existed interval whose range conflict with the new one.
*/
约定俗成的规矩是使用者需要在满足前置条件的情况下,才能得到正确的结果.
行为等价性: 对于用户来讲, 提供的同一个spec,若都被满足了,那么就算做等价.
所以重点就是要站在用户端看待问题,只要同一个spec被满足,则说明实现了相同的功能,即同一个spec所有实现是等价的.
不能单看代码结果不一样,就说两个实现不等价.
spec的强弱判断和比较方法: 前置条件越弱, 后置条件越强, spec越强.(前者用户所需遵守规则少,后者用户得到的信息更详细)
因为前置条件必须被满足,所以后置条件的比较是在前置假设成立的条件下进行的,在很多情况下,前置条件的限制使得后置条件从不同变为相同.