Results 1 to 5 of 5
  1. #1
    mattlindsay is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default Best way to handle a collection of objects?

    Hi, Im pretty new to java although not new to programming in general.

    I wish to create an entity (for example, call it "BookClub"). That entity can have an unknown number of child objects (for example call them "Members").

    I want to have a collection that I can call methods on (for example "BookClub.sendMessage()"). In this way, I can write some code that will iterate through all members of the "BookClub" and send a message to each one of the "Member.GetEmailAddress()"

    Is the solution to use java collections to do this, or am I missing something very obvious about just using classes?!

    Hope that all makes sense!!

    Cheers, Matt

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mattlindsay View Post
    Hi, Im pretty new to java although not new to programming in general.

    I wish to create an entity (for example, call it "BookClub"). That entity can have an unknown number of child objects (for example call them "Members").

    I want to have a collection that I can call methods on (for example "BookClub.sendMessage()"). In this way, I can write some code that will iterate through all members of the "BookClub" and send a message to each one of the "Member.GetEmailAddress()"

    Is the solution to use java collections to do this, or am I missing something very obvious about just using classes?!
    Forget all the Java technology for a moment and think OOP: for a class D to be able to extend a class B, a D is a B. In your case: is a Member a Bookclub? I don't think so, I think all Members together form a BookClub, so a Bookclub has a Member and that smells like composition, i.e. a BookClub has a collection of some sort, filled with Members. If you feel like it you can read about the gory details if you google for "Liskov Substitution Principle".

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    mattlindsay is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    An excellent answer. You're quite right, it is composition.... I guess that leads me to the next part.... how do I implement it!! :S

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mattlindsay View Post
    An excellent answer. You're quite right, it is composition.... I guess that leads me to the next part.... how do I implement it!! :S
    Try it first. Your first guess will likely be pretty close.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mattlindsay View Post
    An excellent answer. You're quite right, it is composition.... I guess that leads me to the next part.... how do I implement it!! :S
    It's quite simple: a BookClub looks like this:

    Java Code:
    public class BookClub {
       private Collection<Member> members= ...;
       ...
       public void addMember(Member member) { members.add(member); }
       ...
    }
    I don't know what methods you want to call on those members, but a BookClub object can call that method on each and every member in its members Collection.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Counting vowles in a collection of String objects
    By sunde887 in forum New To Java
    Replies: 4
    Last Post: 01-18-2011, 07:22 AM
  2. Replies: 8
    Last Post: 11-22-2010, 10:09 AM
  3. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  4. handle FileNotFoundException
    By minifish in forum New To Java
    Replies: 13
    Last Post: 11-08-2008, 02:01 AM
  5. iterating through a collection of objects
    By Scotty Boy in forum New To Java
    Replies: 0
    Last Post: 04-10-2008, 01:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •