It's harder than I though to stick with OO

Discussion in 'Java' started by Marc Twain, Nov 21, 2003.

  1. Marc Twain

    Marc Twain Guest

    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
    Marc Twain, Nov 21, 2003
    #1
    1. Advertising

  2. Marc Twain

    Anton Spaans Guest

    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" <> wrote in message
    news:...
    > 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
    Anton Spaans, Nov 21, 2003
    #2
    1. Advertising

  3. "Marc Twain" <> wrote in message
    news:...
    > 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.
    David Robbins, Nov 21, 2003
    #3
  4. 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/corej2eepatterns/Patterns/DataAccessObject.html

    --
    Regards,
    Christophe Vanfleteren
    Christophe Vanfleteren, Nov 21, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. EightNineThree

    Re: Making pictures harder to steal...

    EightNineThree, Aug 9, 2003, in forum: HTML
    Replies:
    0
    Views:
    394
    EightNineThree
    Aug 9, 2003
  2. Talc Ta Matt

    Re: Making pictures harder to steal...

    Talc Ta Matt, Aug 9, 2003, in forum: HTML
    Replies:
    1
    Views:
    326
    William Tasso
    Aug 9, 2003
  3. Peter Ammon

    Preprocessor harder to write than parser?

    Peter Ammon, Jul 14, 2007, in forum: C Programming
    Replies:
    1
    Views:
    528
    Jean-MArc Lienher
    Jul 14, 2007
  4. bussiere maillist
    Replies:
    0
    Views:
    479
    bussiere maillist
    Dec 7, 2007
  5. Tim Chase
    Replies:
    0
    Views:
    570
    Tim Chase
    Dec 7, 2007
Loading...

Share This Page