2013-12-16 105 views
5

我有一个系统,在那里我有一个household,其中有一个TVSubscription建模。这可以是一个digital,也可以是analogOO设计问题

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。因此我使用双重调度来代替类型检查(需求)。因此,我想到了以下解决方案:

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

我的直觉告诉我,这是一个不好的气味。我不确定如何修复它。

理想我愿做这样的事情:

aUser rent: aMovie. 

回答

1

我不是专家,只是替代了我的头顶部...

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。

1

思想

这么多的两个对象之间的关系, 那么它可能是你想将关系建模为对象

一个目的是anything that is visible or tangible and is relatively stable in form.

实现思路

您创建一个临时的对象,只要它仅作为生活在两个物体之间的关系。如果需要,该对象可以充当中介或方法对象。

我的感觉是,它可能是过度设计,而不是马上由谁读码的人了解情况。

+0

好了,这是一个过程,我采取的分配。我也有过度的工程感觉..我可能会尝试使用中介来建模关系。 –

2

第二个选择...

STB>>initialize 
    aUser := UserNotLoggedIn new. 

STB>>login 
    aUser := self getUserFromAuthorisationCheck 

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

UserNotLoggedIn>>canRent: aMovie 
    self displayErrorUserNotLoggedOn 
    ^false 

User>>canRent: aMovie 
    aMovie ratingAge <= self ratingAge. 

AdultMovie>>ratingAge 
    ^18 

Adult>>ratingAge 
    ^18 

ChildrensMovie>>ratingAge 
    ^10 

Child>>ratingAge 
    ^10 

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