Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > It's harder than I though to stick with OO

Reply
Thread Tools

It's harder than I though to stick with OO

 
 
Marc Twain
Guest
Posts: n/a
 
      11-21-2003
This might be modelling 101 but I'll give it a go: I'm trying to have
a JavaBean that maps one or more table in a DB.

So I create a 'User' bean. The no arg Constructor is used to create a
brand new user with default properties. Another constructor takes a
Primary key as an argument to create a new user from the database.

Then I add 'save()' and 'update()' methods. So far so good. (I guess)

The problem is, if I add a delete() method, it means I have to
instantiate a user object before calling delete() on itself. Doesn't
make much sense to me!

And finally all goes to hell when I end up having to list a bunch of
users... I don't even know where to place that listUsers() method. As
a static method of the User class?

Does anyone have faced the same problems or am I just approaching the
issue the wrong way? I have 5 java books on J2EE but none of them
cover those modelling issues.

Any idea?

TIA,

AJ
 
Reply With Quote
 
 
 
 
Anton Spaans
Guest
Posts: n/a
 
      11-21-2003
The 'save()' and 'update()' are operation on the *row*, on the User
*Object*.

What you could do is to make creation/deletion, which are operations on the
*table*, *static* methods.

public static User newUser()
{
return new User();
}

and do this for all the necessary constructors, which you make private or
protected.

Then, to delete a row:
public static void deleteUser(User pUser)
{
....
remove pUser.getPrimKey() from table.
}
-- Anton.

"Marc Twain" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> This might be modelling 101 but I'll give it a go: I'm trying to have
> a JavaBean that maps one or more table in a DB.
>
> So I create a 'User' bean. The no arg Constructor is used to create a
> brand new user with default properties. Another constructor takes a
> Primary key as an argument to create a new user from the database.
>
> Then I add 'save()' and 'update()' methods. So far so good. (I guess)
>
> The problem is, if I add a delete() method, it means I have to
> instantiate a user object before calling delete() on itself. Doesn't
> make much sense to me!
>
> And finally all goes to hell when I end up having to list a bunch of
> users... I don't even know where to place that listUsers() method. As
> a static method of the User class?
>
> Does anyone have faced the same problems or am I just approaching the
> issue the wrong way? I have 5 java books on J2EE but none of them
> cover those modelling issues.
>
> Any idea?
>
> TIA,
>
> AJ



 
Reply With Quote
 
 
 
 
David Robbins
Guest
Posts: n/a
 
      11-21-2003

"Marc Twain" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> This might be modelling 101 but I'll give it a go: I'm trying to have
> a JavaBean that maps one or more table in a DB.
>
> So I create a 'User' bean. The no arg Constructor is used to create a
> brand new user with default properties. Another constructor takes a
> Primary key as an argument to create a new user from the database.
>
> Then I add 'save()' and 'update()' methods. So far so good. (I guess)
>
> The problem is, if I add a delete() method, it means I have to
> instantiate a user object before calling delete() on itself. Doesn't
> make much sense to me!
>
> And finally all goes to hell when I end up having to list a bunch of
> users... I don't even know where to place that listUsers() method. As
> a static method of the User class?
>
> Does anyone have faced the same problems or am I just approaching the
> issue the wrong way? I have 5 java books on J2EE but none of them
> cover those modelling issues.
>
> Any idea?
>
> TIA,
>
> AJ


start with a good book on basic object oriented design and programming.

but basically you decide what information you have to have and the
relationships between the information and the operation that need to be done
on that information. then you divide up the information and operations into
classes such that each class is contained within itself. this operation
frequently requires a couple iterations. warning flags that you have not
properly grouped the information and operations into the right classes are
questions like what you have about how to make a list of users, and why
delete() doesn't make sense.... those point to errors in dividing up the
information and what needs to be done with it.

I would just quickly say that you need at least 3 different classes...
1. a database user class that handles reading, writing, deleting and other
things that go on with the database.
2. a user class that holds the information about a single user. it might
call the database user class to get or save it's internal information, or
that may be done by some other class that creates users.
3. a user list class that contains a list of users. it would provide things
like the count of the number of users, iterators to go through a list, maybe
searches for types of users, ways to add and remove users from the list,
etc...

and outside of all that you would need some other way to create each of
those types of things and make them actually do something. a gui with
controls to create and enter data for users, displays for listing
information, etc... all outside of the individual classes that hold and
operate on the information.

packaging things into a bean is another problem and depends on what you need
to provide as services to all the other beans in the world... a bean could
be something like a 'user management' bean that encapsulates all of the
above including the gui, or it could be just a single class... this type of
design decision would have to be made when you know who wants to use your
bean and what they need to do.


 
Reply With Quote
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      11-21-2003
Marc Twain wrote:

> This might be modelling 101 but I'll give it a go: I'm trying to have
> a JavaBean that maps one or more table in a DB.
>
> So I create a 'User' bean. The no arg Constructor is used to create a
> brand new user with default properties. Another constructor takes a
> Primary key as an argument to create a new user from the database.
>
> Then I add 'save()' and 'update()' methods. So far so good. (I guess)
>
> The problem is, if I add a delete() method, it means I have to
> instantiate a user object before calling delete() on itself. Doesn't
> make much sense to me!
>
> And finally all goes to hell when I end up having to list a bunch of
> users... I don't even know where to place that listUsers() method. As
> a static method of the User class?
>
> Does anyone have faced the same problems or am I just approaching the
> issue the wrong way? I have 5 java books on J2EE but none of them
> cover those modelling issues.
>
> Any idea?
>
> TIA,
>
> AJ


You might want to look into the DAO pattern.
http://java.sun.com/blueprints/corej...essObject.html

--
Regards,
Christophe Vanfleteren
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preprocessor harder to write than parser? Peter Ammon C Programming 1 07-14-2007 10:49 AM
70-291 harder than 70-291 FinalHokage MCSA 8 04-11-2006 10:35 AM
DSLR harder to handhold than P&S? Derek Fountain Digital Photography 31 04-19-2005 06:29 PM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
Memory stick pro is cheaper than normal memory stick fanfaron Digital Photography 2 06-07-2004 03:27 PM



Advertisments