软件构造(二)spec 规约

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越强.(前者用户所需遵守规则少,后者用户得到的信息更详细)
因为前置条件必须被满足,所以后置条件的比较是在前置假设成立的条件下进行的,在很多情况下,前置条件的限制使得后置条件从不同变为相同.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值