+1:“带有当前项目的集合”软件设计模式非常普遍。
你的游戏中有几个房间。
The `Game` has a collection of `Rooms`.
然后,Game“管理”或“拥有”Rooms。
当一个对象“管理”或“拥有”其他对象时,负责分配和释放(内存中对象的“创建”和“销毁”)。
但是:
Each `room` has a collection of `Players` and an `Inventory`.
等等。你忘了:
The `Game` has a collection of `Players`.
还有:
Each `room` has a collection of `Players`.
等等,Game 也有相同的 Players 集合。
小心“有”这个词。
在 O.O.P. 中,许多对象可以与其他对象相关联,
但是,只有一个对象可以是另一个对象的“管理者”(同时“相关”)。
Room (s) 和 Game 两者都有一些关系,
或与Player (s) 关联,但只有一个对象,
可以是他们的“经理”。
因为Player 始终是Game 的一部分,但是,
可以留下一个当前的Room...
...那么房间可以引用Players的同一个集合,
比Game,但不“管理”它们。
所以,我们把前面的声明改成:
The `Game` manages a collection of `Players`.
Each `Room` relates to a collection of `Players`.
现在:
Each `room` has (a collection of | ) an `Inventory`.
然后,每个Room“管理”或“拥有”Inventory。
让我们用Items替换Inventory这个词:
Each `room` has a collection of `item`s, called an `Inventory`
所以:
The `Game` manages a collection of `Rooms`.
The `Game` manages a collection of `Players`.
Each `Room` relates to a collection of `Players`.
Each `Player` relates to a single, current `Room`.
问题在于“有”这个词。有时意味着联想,
“管理”/“拥有”对象,有时意味着“关联但不管理”一个对象。
最后:
Each `Room` manages to a collection of `Items`, also called `Inventory`.
但是,如果一个Player可以带着Items,和他/她一起,
并更改Room(s),并且每个Player,可能会删除一个Item,
进入Room,就像放下枪,拿起斧头一样。
然后事情会变得有点混乱。
假设item“可以位于”,而不是room“具有”。
因此,每个player 都可以与Items 的集合相关,并且,
每个room 可以与Items 的集合相关,
并且,Items 由Game“管理”。
Each `Player` can relate to a collection of `Item` (s).
Each `Room` can relate to a collection of `Item` (s).
Each `Item`, maybe related to a `Room`,
can be located in a `Room`, but, not always.
Each `Item`, maybe related to a `Player`, but, not always.
Each `Item` is part of an universe called the `Game`.
So, the `Game` "manages" all the `Item` (s).
干杯。