Design advice: lists and containment

Jim Menard jim.menard at gmail.com
Sat Nov 5 15:05:39 UTC 2005


I need some design advice. This problem is also applicable to other
languages, but for some reason I seem to agonize over it more when I'm
trying to "think in Smalltalk".

A user has a collection of bookmarks. Each bookmark is therefore owned
by a user. (In my model, a bookmark may also be owned by a group of
users instead of an individual user, but let's not confusing things
for now.)

So, a user has a bookmarks instance variable that is a collection. It
would also simplify my code to let each bookmark have a user instance
variable that "points" back to the user.

1) Is that kosher? Is it good practice to have a bookmark hold on to its user?

2) To add a bookmark to a user, it now takes two steps:

    user bookmarks add: bookmark.
    bookmark user: user.

Is there a better way? I could easily put that code into, say, a user
method. Is that how it is commonly done?

3) When I remove a bookmark from a user's collection, will the
bookmark be GC'd? I think so, but I want to be sure.

4) The accessor method on user that returns the bookmarks means that
callers can manipulate the list. My first instinct is to say, "Don't
worry about it. I'm not developing a library, so I will know how to
use the collection properly." Should I worry? Should the accessor
method return a copy of the bookmarks, so manipulation of the contents
won't affect the user's bookmark list? If so, then I have to provide
addBookmark: and removeBookmark: methods, right?

Thanks in advance for your advice.

Jim
--
Jim Menard, jim.menard at gmail.com, jimm at io.com
http://www.io.com/~jimm



More information about the Squeak-dev mailing list