Menu

How do I?

Reflection
2002-04-15
2002-04-22
  • Reflection

    Reflection - 2002-04-15

    I am trying to do something and can't quite get it right.  I was wondering if anybody had any suggestion on how to look at the problem.  I have thought of an example that is much more basic than my problem, but very simmular.

    Here is my fictional problem:

    - I have 3 classes.  BookCase, Shelf, and Book.
    - I only have one BookCase, but can add shelves, and books.
    - A shelf is a Multi-Aggregation of a bookcase.  (Many shelves, requires a book case)
    - A book can be a Multi-Aggregation of a shelf.  (Many books, requires a shelf)

    Now I want to be able to search the whole book shelf for a book.  I don't want to have to search each shelf individually.

    So to do this I create a Multi-Association of a BookCase to a Book.

    Now I think here's where the problem comes in:
    - Adding and Removing Books.
    - Having more than one copy of a book.

    If I have two copies of the same book on two shelves, I don't want to make two objects for that one book.  But when I remove all references of that book, I want the destructor to destroy the book.

    One Idea I had was to use a use counter.  Everytime I and another copy of a book to the bookcase I increase the counter.  Every time I remove a book, I decrease the counter.  And free the object if the counter is equal to zero.  However I don't like this because it doesn't ensure that when the books not being used anymore that it will be released.  I have to manually try to make sure.

    Does this make any sence?  Any suggestions?  Any alternative ideas?

     
    • Jimmy Venema

      Jimmy Venema - 2002-04-17

      You would have two physical books in reality, however you want to have only one book description. So given your requirements you should modify your model to suit this requirements. So you would have a bookcase with multiple book descriptions and with multiple selves (you said that in your case there is only one bookcase, otherwise you would need to introduce a library class). A physical book would be both be an aggregate of the book description and teh Selve. So the model I would come to in the two minutes spend on it would be:
      BookCase <>--->> Selve
      BookCase <>--->> BookDescription
      BookDescription <>--->> Book
      Selve <>--->> Book

      In case of a library with multiple bookcases:
      Library <>--->> BookDescription
      Library <>--->> BookCase
      BookCase <>--->> Selve
      BookDescription <>--->> Book
      Selve <>--->> Book

      You could extent the model with authors, so you could easily find the different books of the same author.
      Library <>--->> Author
      Library <>--->> BookDescription
      Author <>--->> BookDescription
      Library <>--->> BookCase
      BookCase <>--->> Selve
      BookDescription <>--->> Book
      Selve <>--->> Book

      Jimmy Venema

       
      • Reflection

        Reflection - 2002-04-18

        Thanks for the suggestions..  I think you are on the right track.  However there are some differences between my goal and your suggestions.

        Main difference is I don't want to duplicate the book data.  In real life we would, but in a computer system this is a mistake due to wasted memory.

        I think to fix this variation, I think this may work better:

        BookCase <>--->> Shelf
        BookCase <>--->> Book
        Shelf <>--->> BookDescription
        Book <>---> BookDescription

        Of course I need to try this out.

         
    • Jimmy Venema

      Jimmy Venema - 2002-04-19

      In my model there was no duplication of book data! The book data resides in the BookDescription, the Book class only represents the physical location of one copy of it on a Selve in a BookCase. It seems we have swapped the meaning of book and Book description. So better read BookData instead of BookDescription and BookReference instead of Book. The model now becomes:
      BookCase <>--->> Selve
      BookCase <>--->> BookData
      BookData <>--->> BookReference
      Selve <>--->> BookReference

      I hope it is more clear now. So the data of a book with multiple copies only resides one time in memory as an BookData object. While references to it and to its location on a particilar Selve can occur multiple times as several BookReference objects.

      Jimmy Venema

       
      • Reflection

        Reflection - 2002-04-22

        Cool.  That was what I needed.  Thanks.  I tryed it without hitch.  My Library will soon be open :-)

        This solution is much better than the last ditch effort I was making to use a linked list.

        However this problem has got me thinking on at least one more relationship that I think would be nice to have built into Class Builder.  Currently there are:
        One to One and
        One to Many.

        Possible additions are:
        Many to Many and
        Many to One. <Could be the same as One to Many)

        It is interesting however that two One to Many relations onto a reference class can represent a Many to Many relationship.

        Basically this says that:

        [A]<>--->>[B]<<---<>[C]

        ~=

        [A]<<--->>[C]

        Thanks again.  I hope this tidbit of information helps others on their projects.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.