我有一个系统,在那里我有一个household
,其中有一个TVSubscription
建模。这可以是一个digital
,也可以是analog
。OO设计问题
甲user
登录到SetTopBox
。然后他可以租用Movies
。
因此目前的方案如下:
//Existing instantiated variables in scope
aMovie
aUser
aSetTopBox
//End
--> execute this command:
aUser rent: aVideo on: aSTB
Code:
User>>rent: aVideo on: aSTB
aVideo rentBy: self on: aSTB
Video>>rentBy: aUser on: aSTB
aUser rentActionMovie: self on: aSTB
User>> rentActionMovie: aMovie on: aSTB
aSTB rentActionMovie: aMovie by: self
STB>>rentActionMovie: aMovie by: aUser
(loggedInUser isNil)
ifTrue: [ loggedInUser := aUser.
--Do stuff to charge the movie]
ifFalse: [ -- Show error that user is not logged in]
从技术上讲,这是正确的。但我有(对不起,是肛门)这个问题:
我必须要通过aSTB
2种方法调用到最终使用它。需要在此双重派遣,因为我有Child
和Adult
,他们可以租AdultMovie
和ChildrensMovie
。因此我使用双重调度来代替类型检查(需求)。因此,我想到了以下解决方案:
我可以在aSTB
上存储currentlyLoggedIn
,并在aSTB
上存储loggedInOn
。然而,这使得对象指向彼此。
我的直觉告诉我,这是一个不好的气味。我不确定如何修复它。
理想我愿做这样的事情:
aUser rent: aMovie.
好了,这是一个过程,我采取的分配。我也有过度的工程感觉..我可能会尝试使用中介来建模关系。 –