• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A Home for Collection of Objects

 
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Ranchers,

I need your help again. Where should I keep a collection of instances of my custom class? In the class itself in a static variable?


Is it fine like this or should I implement the collection elsewhere? What say you?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends. Why does the Item class need to keep track of all the Item instances? Does it make sense for it to do that in the context of your application? What can you accomplish by doing this?

One scenario where it might make sense is if this were a Factory of some sort. But again, there is not enough context to make a proper determination. You'll have to give us more details. However, generally, most designs have some other class responsible for tracking and managing a group of objects.
 
Mike Matthews
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:But again, there is not enough context to make a proper determination. You'll have to give us more details.


You're right. I read somewhere on the web that it's better not to make up scenarios for problems, but to actually present the real situation in which the problem emerged. Here goes.

You actually nailed it... if by "Factory" you mean a place where goods are manufactured. I need to keep track of Products on which various operations will be carried out (like adding them to Orders).
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, Junilu was probably referring to the Factory pattern.
In programming terms a factory is a class that has responsibility for building/creating other objects.
It combines well with the interface pattern.
You go to the factory and say "give me one of these" please (an interface). And it gives you an implementation of that interface. You don't need to know where it came from, or the details of how it was put together, or even what is "under the hood". You know that it will do its job.
It isn't THAT far removed from the real world.

> I need to keep track of Products on which various operations will be carried out (like adding them to Orders).

Sounds like a separate class to me, which keeps a list of products that can be ordered.
 
Mike Matthews
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:Actually, Junilu was probably referring to the Factory pattern.


Right, it's actually the first subject touched upon in Bloch's Effective Java. Silly me for not making a connection.

Stefan Evans wrote:Sounds like a separate class to me, which keeps a list of products that can be ordered.


So I should make a separate class that would have a static list of instances of Product class?
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Matthews wrote:

Stefan Evans wrote:Sounds like a separate class to me, which keeps a list of products that can be ordered.


So I should make a separate class that would have a static list of instances of Product class?



Avoid static if you can, barring constants.

Whatever owns the Products should hold the List.
They presumably don't exist in a vacuum.
 
Mike Matthews
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Mike Matthews wrote:

Stefan Evans wrote:Sounds like a separate class to me, which keeps a list of products that can be ordered.


So I should make a separate class that would have a static list of instances of Product class?



Avoid static if you can, barring constants.

Whatever owns the Products should hold the List.
They presumably don't exist in a vacuum.


What if I wanted to use the list throughout the program, in multiple places? And what if I wanted only one instance of such list? If you're not overwhelmed by a barrage of questions, then I have another one: why is it better to avoid static?
 
reply
    Bookmark Topic Watch Topic
  • New Topic