Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: where to put list of objects method OOP

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default where to put list of objects method OOP

    I have searched for this question and not found the answer. This should be simple, but i struggle with the answer. Hopefully someone can help.

    Where is the OOP acceptable place to put methods to get lists? For example, suppose i have a person class, and i need to get a list of all persons, a getPeople() method.

    Do i put this in the person class so i would have person.getPeople() or do i create some other class?

    Now let's say i have a WorkCenter class where multiple persons can be assigned to a work center, and i want to get all people assigned to a work center. Do i add a getPeople() method to the Work Center class or do i use some other class and send in the Work Center class as a filter?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: where to put list of objects method OOP

    First scenario:

    The Person class represents a single person. Why should it have to keep track of every Person which has been instantiated? It shouldn't. Different people are going to be instantiated for different reasons and there doesn't need to be a "master list" accessible from the Person class. There are technical implications on why it shouldn't (not unsolvable, just unnecessary problems to have to think about), but the above is the main reasons.

    Second scenario:

    A WorkCenter's representation may contain a list of People who work there. In this case it is perfectly valid for it to keep track of what people are related to a given WorkCenter and a getPeople() method is a great way to retrieve the list of workers.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: where to put list of objects method OOP

    for first scenario: Where do you propose i put the getPeople() method? Do i create a PersonList class, or a People class, or a generic master list class, or something/somewhere else?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: where to put list of objects method OOP

    It depends on what your application is doing. In general I don't think it's useful to have a list of every instance created of a particular class. It plays havoc on memory management systems because the garbage collector has no idea when a particular instance is ready to be destroyed.

    Wherever people are being used should keep track of what people instances they are interacting with (similar to case 2).

    What problem are you trying to solve that would involve keeping a global list of people instances? There may be a better way to structure your program to avoid it.

Similar Threads

  1. Replies: 7
    Last Post: November 6th, 2011, 06:15 PM
  2. Replies: 1
    Last Post: November 5th, 2011, 10:56 AM
  3. How Can i put mysql query results into Linked List.
    By Muhanguzi in forum JDBC & Databases
    Replies: 1
    Last Post: June 18th, 2011, 11:03 AM
  4. Conditional statements on Array list objects
    By jaguarpaw in forum Collections and Generics
    Replies: 3
    Last Post: May 10th, 2011, 09:52 AM
  5. Operate on list`s objects
    By MryJaho in forum Java Theory & Questions
    Replies: 5
    Last Post: February 24th, 2011, 04:44 AM