Simple OO smaltalk question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Simple OO smaltalk question

Tony Giaccone-2


I'm new to Smalltalk and come from the java world and am looking for a bit of help on how something should be implemented.


Assume I have a Person class

and that there are three subclasses of that class Admin, Teacher, Student.

I also have a class called UserManager

I want to implement a method on UserManger>> class  firstPageForUser: user

This method looks at the class of the user and returns an initial page for each user after log in.

I have this method implmented:
Person>>class firstPageForUser

        ^nil


Now on the UserManager clas>> firstPageForUser: user

        (user isKindOf: Person ) ifTrue: [returnPage := ( (user class) firstPageForUser) ].


Which as I'm sure most of you realize doesn't work.  I'm not sure how to structure the call so
that the right message gets sent to the class.

My understanding is that even though the subclasses don't implement this method yet,
that the method should be passed up the class tree till it finds the method in Person.

However, I also know that the syntax for the method I've written isn't correct.

What I want to do is make the call on the class for each subclass.

I could do the same thing this way, right, but this seems more verbose and ugly then it has to be.



        (user isKindOf: RSPerson ) ifTrue: [returnPage := (RSPerson firstPageForUserClass) ].
        (user isKindOf: RSAdmin ) ifTrue: [returnPage := (RSAdmin firstPageForUserClass) ].
        (user isKindOf: RSTeacher ) ifTrue: [returnPage := (RSTeacher firstPageForUserClass) ].
        (user isKindOf: RSStudent ) ifTrue: [returnPage := (RSStudent firstPageForUserClass) ].

        ^returnPage



_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users
Reply | Threaded
Open this post in threaded view
|

Re: Simple OO smaltalk question

Stéphane Ducasse
OOP in Smalltalk is nearly the same as in Java!

No typecheck

> I could do the same thing this way, right, but this seems more verbose and ugly then it has to be.
>
>
>
> (user isKindOf: RSPerson ) ifTrue: [returnPage := (RSPerson firstPageForUserClass) ].
> (user isKindOf: RSAdmin ) ifTrue: [returnPage := (RSAdmin firstPageForUserClass) ].
> (user isKindOf: RSTeacher ) ifTrue: [returnPage := (RSTeacher firstPageForUserClass) ].
> (user isKindOf: RSStudent ) ifTrue: [returnPage := (RSStudent firstPageForUserClass) ].
>
> ^returnPage

why don't you either declare a factory or simply define methods on the RS-Object  

> RSPerson >> firstPageForUserClass
        ^ cccc

> RSAdmin >> firstPageForUserClass
> RSTeacher >> firstPageForUserClass

Stef
_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users
Reply | Threaded
Open this post in threaded view
|

Re: Simple OO smaltalk question

Lukas Renggli
Why don't you just write

    user class firstPageForUserClass

?

Lukas

On 11 October 2010 09:16, Stéphane Ducasse <[hidden email]> wrote:

> OOP in Smalltalk is nearly the same as in Java!
>
> No typecheck
>
>> I could do the same thing this way, right, but this seems more verbose and ugly then it has to be.
>>
>>
>>
>>       (user isKindOf: RSPerson ) ifTrue: [returnPage := (RSPerson firstPageForUserClass) ].
>>       (user isKindOf: RSAdmin ) ifTrue: [returnPage := (RSAdmin firstPageForUserClass) ].
>>       (user isKindOf: RSTeacher ) ifTrue: [returnPage := (RSTeacher firstPageForUserClass) ].
>>       (user isKindOf: RSStudent ) ifTrue: [returnPage := (RSStudent firstPageForUserClass) ].
>>
>>       ^returnPage
>
> why don't you either declare a factory or simply define methods on the RS-Object
>
>> RSPerson >> firstPageForUserClass
>        ^ cccc
>
>> RSAdmin >> firstPageForUserClass
>> RSTeacher >> firstPageForUserClass
>
> Stef
> _______________________________________________
> Pharo-users mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users
>



--
Lukas Renggli
www.lukas-renggli.ch

_______________________________________________
Pharo-users mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-users