You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Robert Taylor <rt...@mulework.com> on 2003/03/13 23:44:18 UTC

[OT]RE: when should classes contain other objects?

Heather,

Let me first say that this is considered off-topic and
the subject of your email should indicate it as such if
you post to this mailing list. Rick Reumann has set up
a Yahoo Group http://groups.yahoo.com/group/model_struts/,
for this topic. You can post to model_struts@yahoogroups.com

That being said....


I had similar questions when designing my model and decided to
design a set of objects to mirror my tables. Since my database
is RDBMS and not OO, this means that each of those objects contained
an id as a foreign key reference instead of an object reference.
I call these objects DTO (Data Transfer Object). They are simple
structures with little to no functionality apart from providing
accessors and mutators so they conform to the bean spec.

I then designed a set of business components by using composition
instead of inheritence. For me, business components encapsulated
the business logic for interacting with the database. Each is
composed of 1 to many DTO objects and various helpers such as
DAO (Data Access Object) to which the underlying query and
persistence logic is delegated.

Modifying your schema to make the address field in student
a foreign key to an address, part of my model would look something like
the following:

public class StudentDTO {
 private int id;
 private String name;
 private Date enrollment;
 private int addressId;

 public StudentDTO() {};

 // accessors and mutators for each member

}

public class Student {

  private StudentDTO studentDetail;
  private AddressDTO addressDetail;

  public static Student findByStudentId(int id) throws StudentException {

   // find and return a student or return null


  }

  public Student(StudentDTO detail)  {

    this.studentDetail = detail;

  }

  public StudentDTO getStudentDetail() {

   return this.studentDetail;

  }

  public AddressDTO getAddressDetail() throws StudentException {

    // lazily load address detail
    if (this.addressDetail == null) {

      this.addressDetail = // get address detail from database

    }

    return this.addressDetail;
 }

  public void create() throws StudentException {

    try {

      StudentDAO dao = DAOFactory.getDAO(StudentDAO.DAO_NAME);
      dao.createStudent(this.studentDetail);

    } catch (Exception e) {
       throw new StudentException(e);
    }


  }

  public void update() throws StudentException {

    // do update stuff
  }

  // the rest of CRUD (create, retrieve, update, delete) methods

  // additional business logic methods
}



Student can only be created with valid student details or it
can be found using a student identity. Finding the student only
involves retrieving the primary student information. That is,
the address details are not retrieved when Student.findByStudentId()
is invoked, but rather when Student.getAddressDetail() is invoked.
The address details are lazily loaded and referenced there after.

Let me say that there are a multitude of approaches to this problem
and my choice was based on application requirements and schema complexity.
There may be, and most probably are, better ways to build this architecture.


You may want to visit theserverside.com and take a look at some of the
design
patterns documented by Sun and/or Martin Fowler.

robert


> -----Original Message-----
> From: Heather Buch [mailto:hbuch@www.thebuchs.dk]
> Sent: Thursday, March 13, 2003 4:43 PM
> To: struts-user@jakarta.apache.org
> Subject: when should classes contain other objects?
>
>
> 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
>


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