views:

60

answers:

8

For example if i have an Author class and a book class independently. We all know an author writes a book.

What i would love to know when it's best to include the book as a reference object in the Author class or just include the book name?

The reason for this question ties mainly to flexibility and easy maintenance.

Update:

What design pattern should i read up that relate to this type of issue?

A: 

It depends on what you want to do with the information. Sometimes it might be easier to have your list of book names stored in the author, other times you might need the full book information (ISBN, publisher etc.)

In a database you would have an Author table with all of their details, and a book table with all details of the book, then probably a many-to-many relationship table to tie books to authors and vice-versa.


Oh, and properties / object references aren't really mutually exclusive. A property CAN be an object reference. It sounds more like you are asking if you should store the full obejct information or just the piece you might need immediately.

ck
The last paragraph explain what i am talking about.
Colour Blend
A: 

Option B. Avoid having such references - it will quickly render your program into a mess of dependencies. Keep the two concepts independent, and if you need, some higher-order abstraction to link between the two.

Your question is a part of a complex saga known as "object-relational mappings", there's a lot of material on design patterns for that on the web.

Pavel Radzivilovsky
-1: The question contains no hints whatsoever that OR mapping is involvend, and even then: pretty much all widely used OR mappers model foreign keys as actual object references (with some lazy loading magic to avoid fetching data needlessly)
Michael Borgwardt
+1  A: 

The name would be a very bad idea; it's quite possible for two different books to have the same title.

I'd use an object reference (inside some sort of collection, of course, since an author can write more than one book) - that's what they're for. This is certainly flexible and maintainable.

There may be exceptional circumstances where this causes problems, and only then would I consider keeping some sort of unique ID (in the case of books, the ISBN would be the prime candidate) instead of a reference.

Michael Borgwardt
+2  A: 

I would generally store the reference to the book, and the book object is therefore readily accessible from the author. If you store the property (name) in this scenario, then some questions are:

  1. is the name unique ?
  2. is it costly to retrieve the book from the author (e.g. do you have to go to the database) ?

If you don't want the whole book object in memory (perhaps storing all authors with all their books consumes huge amounts of resource), then perhaps you want a book placeholder object referenced from the author class. That placeholder would store the book's unique key and can retrieve the book upon demand. It may implement a book interface and thus be indistinguishable from the real book. The downside is that the book still has to be populated upon, or prior to, a request for info.

Brian Agnew
+1  A: 

It really depends if author is ever going to need access to book information beyond that of just the title. If really never, then ok maybe you only need the title as a String. However, if additional book info is likely to be needed, you need to think about how you are going to access that. In this case it probably makes sense to use a book class.

Richard
+1  A: 

Having the book name inside the author object will create duplication if a book has many authors. If somehow you need to modify the book name then you will have to go through all the authors collection in order to determine where you need to change.

Having a single book object referenced by all its authors is much simple. Just change the book name in one place and that's all.

In don't think is it easy maintainable or flexible a structure with unneeded duplicates.

EDIT: With duplication of the name you can also get to inconsistencies. Just imagine what happens if a book's titles (with two authors) gets its name modified just for one author.

Victor Hurdugaci
A: 

Object-oriented when each object has references to its best friends – not to their names. There are certain downsides to it, like: it is notoriously hard to keep track of back-references.

However, the problem calls for a more general solution than a best-practice pattern.

As long as you're using OO to design your application at all, I'd opt to be consistent and keep direct references to your objects.

nes1983
A: 

It really depends on your domain. Most book store databases have some discrepancies between names of authors between books, and no information whether a given name is shared by more than one author. So in that a book would have a list of author names, and there would be no object identity mapped to that data.

On the other hand, if your domain is a publishing house, you have a very good idea which of the authors John Smith ( client number 19024982 ), J. Henry Smith ( client number 19024982 ) and John Smith ( client number 773829 ) are the same or different authors, and which books those two authors have created, and so using object references for author and book identity would be a good mapping of the domain.

Pete Kirkham