不变因素:方便查询
本节的不变因素所说的查询可以从其它查询中得出,但是它方便定义模型的其它部分。这里是一个例子。
最早的预订
一部影片可能有多人预订,每个预订包括一个日期和时间,一些预订可能还在排队,其中肯定有一个最早的。
我们可以通过下面的不变因素获得这个信息。
-- In the context of a title, t,
t : Title
-- if the set of reservations that are still pending is not empty then 如果预订等待队列不为空,则
t.reservations -> select( pending ) -> notEmpty implies
-- there is an oldest pending reservation 有一个最早的预订
t.oldestPending -> notEmpty
-- which is one of the title’s pending reservations 找出该片名的预订
and t.reservations -> select( pending) -> includes( t.oldestPending )
-- and which was made at the same time as, or before, all of the title’s pending reservations 找出最早的预订
and t.reservations -> select( pending ) -> forall( v |
t.oldestPending.whenMade.atOrBefore( v.whenMade ) )
不变因素:约束
本节的不变因素排除某些粗糙类型模型的印象。我们可以非常松散地,通过将对象限制在组内或是按照对象间必然的联系将约束分组(这是一个松散的分组,因为限制在一组内的对象可能互相关联)。
组(Sets)
类型模型允许访问一组对象的两种方法均可访问对象。
录像店的库房是按照影片名称分类的拷贝:
-- In the context of a video store ,
vs : VideoStore
-- the set of copies that form the store’s inventory 拷贝集合构成库存
vs.inventory =-- is the set of copies of the titles in the catalog 按照片名分类的一组拷贝
vs.catalog.copies
录像店有一组出租的录像带
-- In the context of a video store ,
vs : VideoStore
-- the set of rentals held by members 一组会员租借的录像带
vs.members.rentals =
-- is the same as the set of rentals formed from the copies’ current and past rentals 与被租借的拷贝是完全相同的组
vs.copies.currentRental -> union( vs.copies. pastRentals)