I'm hoping there's a book or something out there for me to get...

If I have a class that has Collection as an instance variable, what is that method of coding called? A design pattern? If so, where can I find more information on it? As I've been working with this mentor, he's really helped me understand my programming weakness and that weakness is thinking in terms of collections or relationships between objects. It just seems so difficult for me right now and I need to read to become smarter. My mentor is a great guy, but I'm starting to feel like I need to learn more on my own.

public class Evaluation
    private List<Criterion> criterion = null;

    public Evaluation()
        criterion = new List<Criterion>();
  • 3
    Care to write out an "Example" of what you're referring to? – Brad Christie Jun 24 '11 at 20:49
  • @Brad I added something, but couldn't format it correctly – user20377 Jun 24 '11 at 20:58
  • Is this C#? Don't initialize criterion to null then initialize it to something else in the constructor. Default value is null already. – Jesse C. Slicer Jul 26 '11 at 3:03
  • @Jesse C. Slicer it's Java. – user20377 Jul 26 '11 at 3:46
  • Wow - they look more similar every day :) – Jesse C. Slicer Jul 26 '11 at 13:35


it's not a design pattern, it a fundamental practice of OOP

(as opposed to having your class inherit from the collection class)

| improve this answer | |
  • I have a beginner JAVA book (dietel) and there's isn't much about it. In the case of this evaluation it has many criterion (so I'm assuming a one-to-many relationship) I google "java one-to-many relationship" and I'm not getting anything back. Any more tips? – user20377 Jun 24 '11 at 21:02
  • 2
    just realized you typed "composition" at the top. I'll look at that up. – user20377 Jun 24 '11 at 21:03
  • 3
    @robertlewis2001 I think this is referred to as a "has a" relationship in some books. – Mahmoud Hossam Jun 24 '11 at 21:32
  • I'm new to the term. If the included class (List) wasn't generic would still this be composition? – vemv Jul 25 '11 at 23:37
  • 2
    @vemv: when one object uses another as a property or component, it's called 'composition'; the kind of object used does not matter – Steven A. Lowe Jul 26 '11 at 3:10

My mentor is a great guy, but he gets frustrated when I start asking lots of questions, so I'm starting to feel like I need to learn more on my own.

Can I suggest that you should be reading books too.

And writing code, making mistakes, etc.

The importance of the last cannot be underestimated. Asking questions, reading books, etc can give you the false impression that there is a "perfect solution" to every IT problem and you should never be satisfied until you've found it. In reality there are lots of solutions; many of them "good enough" and none (*) of them are perfect in the long term. You need to learn to make your own practical judgements, and you will do that by doing, not by asking / reading.

(* I'm probably exaggerating a bit, but not when you take the long view. Solutions that would have been judged as "almost perfect" / "state of the art" by the standards of the 1960's are now judged as old fashioned, unmaintainable and unscalable. Only a fool would confidently predict that the IT world won't continue to evolve. Objective perfection is probably only achievable for problems that are so simple that it hardly matters.)

| improve this answer | |
  • @Stephen C You are absolutely correct about writing code as I've been writing a lot more code since I posted this question and it's been more "help" than anything I've done. – user20377 Jul 26 '11 at 3:47
  • Also, there are often cases where there is No Solution. Especially in restricted-resources environment. Get accustomed to the idea that some things can't be done at all. – SF. Jul 26 '11 at 8:32

Encapsulation, a form of Information Hiding

In this example, the object has some internal state information (a List that you have named criterion).

| improve this answer | |
  • Composition: An object that can own one or more objects of another type or its own type is said to be "composed" of those objects. Ownership is important here - it means that when the lifetime of the composing object ends, so do the lifetimes of the composed objects.
  • Aggregation: An object that refers to one or more objects of another type or its own type is said to "aggregate" those objects. The difference with composition is ownership: if the aggregating object's lifetime ends, the lifetimes of the aggregated objects are not (necessarily) affected.
| improve this answer | |