struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Aaron O'Hara" <a...@globalreachsoftware.com>
Subject RE: when should classes contain other objects?
Date Fri, 14 Mar 2003 00:09:20 GMT
Heather,

It's hard to say if there is a right or wrong approach, but I'll let you
know what I do and why.  When deciding on an implementation, keep in
mind the following factors and what priority they have: readability,
maintainability, development time, operational speed, memory efficiency.

For every entity in my database that I will use in my application, I
create a corresponding class which maps directly to the able; I do have
classes contain other objects.  In addition to this, I also make a class
for every unique result set that will be returned to the application.  

This has the advantage of being very efficient on memory because only
fields that are used in the query will be in the object.  For example,
if I wanted a list of students and their courses, I would create a
result set object with the studentname, and coursename.  The alternative
is to fully instantiate your "entity" objects and have some fields not
initialized or all fields initialized even though you're not using them.

In the case you provided, I probably would not create a
"student_course_section" class because that class would never be used
directly in the application.

On the negative side, it requires more code to be written.  If your
schema changes, it will involve more changes as well.  However, in my
humble opinion, this is a highly efficient way dealing with database
entities result sets.

If you have any questions, please let me know.

Aaron

-----Original Message-----
 >From: "Heather Buch" <hbuch@www.thebuchs.dk>
>Reply-To: "Struts Users Mailing List" <struts-user@jakarta.apache.org>
>To: struts-user@jakarta.apache.org
>Subject: when should classes contain other objects?
>Date: Thu, 13 Mar 2003 22:42:32 +0100
>
>I'm thinking about how to build my java objects, based upon my
relational
>database.
>
>In my database I have:
>
>student table
>-id
>-name
>-enrollment date
>-address
>
>course table
>-id
>-name
>
>course_section table
>-course_id
>-date
>
>student_course_section
>-course_section_id
>-student_id
>
>My problem is - how to map this to java objects. Specifically, when
should 
>a
>class contain another whole object, and when can it just contain a
field 
>from
>the other object?
>
>For example, is this better:
>
>class Course
>{
>}
>
>class Student
>{
>}
>
>class StudentCourseSection
>{
>protected Course mycourse
>protected Student mystudent
>}
>
>or this?:
>
>class StudentCourseSection
>{
>protected String mycourseid
>protected String mystudentid
>}
>
>I like the idea of objects that contain coplete other objects. If
>StudentCourseSection contained Course and Student objects and it needed
the
>name of the course or the student, they would be immediately available.
On 
>the
>other hand, it could get to be a pain to load many "contained" objects
up 
>from
>the database, each time you load the "containing" object.
>
>I have been looking around a bit for info on this problem. There is a
lot 
>of
>writing on object-relational-mapping, and specifically ORM tools. I'm
not
>interested in a tool, just some basic concepts and rules for how to
group 
>the
>objects together. (I'm not really interested in thinking about object
>inheritance either if I can avoid it). Also, if anyone happens to know
of a
>good article on this, that would be very helpful.
>
>Thanks,
>
>Heather Buch
>
>
>
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org


_________________________________________________________________
MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.  
http://join.msn.com/?page=features/virus


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org





---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Mime
View raw message