我有一个必须建模household的系统,该系统具有TVSubscription。可以是digital 1,也可以是analog 1。
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方法调用才能最终使用它。这里需要双重调度,因为我有ChildAdult,他们可以租用AdultMovieChildrensMovie。因此,我使用双重调度而不是类型检查(要求)。因此,我想到了以下解决方案:

我可以在currentlyLoggedIn上存储aSTB,并在loggedInOn上存储aSTB。但是,这使对象指向彼此。

我的直觉告诉我这是难闻的气味。我不太确定如何解决它。

理想情况下,我想做这样的事情:
aUser rent: aMovie.

最佳答案

我不是专家,只是脑海中的另一种选择。

STB>>initialize
    aUser := UserNotLoggedIn new.

STB>>rentMovie: aMovie by: aUser
    (aMovie okayFor: aUser)
        ifTrue:  [ --Do stuff to charge the movie]

AdultMovie>>okayFor: aUser
    ^aUser canRentAdultMovie

ChildrensMovie>>okayFor: aUser
    ^aUser canRentChildMovie

User>>canRentChildMovie
    ^true

User>>canRentAdultMovie
    self displayErrorCannotRentAdultMovie
    ^false

Adult>>canRentAdultMovie
    ^true

UserNotLoggedIn>>canRentChildMovie
    self displayErrorUserNotLoggedOn
    ^false

UserNotLoggedIn>>canRentAdultMovie
    self displayErrorUserNotLoggedOn
    ^false

Child "just the same as User"

User>rent: aMovie.
    aSetTopBox rentMovie: aMovie by: self.

aUser租金:aMovie。

关于oop - OO设计问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20615352/

10-10 18:05