Best way to link objects in collections for simple library application?
I am just setting up a simple library application, basically a collection of "Members" and a collection of "Books". My idea was to implement both of these as NSMutableArrays so that I could easily add and remove objects. What I would like to ask is what is the best way to implement the link between a "Member" and a "Book" when they take a book out of the library?
Initially for simplicity I was thinking of limiting each member to withdrawing o开发者_如何学JAVAnly a single book at a time, but I would be interested in ideas to represent multiple book withdrawals too.
EDIT
Can I just ask if the correct way to do this would be to create a "Library" object as a top level object that holds the "Book" and "Member" collections. I was just thinking about this as otherwise these would need to be created in main() which seems a little un-object oriented.
many thanks for any help / info.
gary
Give each Member object a mutable-collection (either array or set, your choice) property containing the member's books. Also give each Book object a non-owning property identifying the member who has it checked out (if anybody; nil
if nobody).
You needn't remove a Book from the array of all books unless the library sells, gives away, or loses a book.
Can I just ask if the correct way to do this would be to create a "Library" object as a top level object that holds the "Book" and "Member" collections.
Yes.
You'll want to decide whether this is a controller object or a model object. If it's a model object, you'll need to create a controller to own the Library (and the Library window), and you'll expose the library in AppleScript. If it's a controller, it's the object that owns the Library window (as well as the Books and Members), and you won't expose it in AppleScript.
精彩评论