5

In Java, there is no global access like in C++. So what would one do if they wanted to create a container of objects that can be accessed from any class? Or say a java bean that holds global values.

For example, if I am making an elevator simulator, fields that need to be known by all like int numElevators have to be place somewhere right? Same with the collection object for elevators Elevators[] elevators.

I can think of one way which is to create a singleton class to store all those global variables. Then use static methods to provide access from any class. But is there a more elegant solution?

Steve
  • 11,831
  • 14
  • 51
  • 63

5 Answers5

4

I would expect an instance of a Building to have a collection of Elevators. I think there are very few things in a project that are truly global and you can usually find some managing entity that should contain and distribute this knowledge.

By tying this within such an entity, you can a) control access and change/refactor more easily b) mock this and make testing easier.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • This is absolutely the right way. 99% of the time the thing you wanted to use a Singleton for can actually be associated with an object. A more difficult example is something like the number of steps you want to run a simulation for - but this can be dealt with by having a "Simulation" object. – DJClayworth Feb 01 '11 at 19:46
  • I like the idea of a managing entity and one that also distributes the knowledge. So would a good design be a singleton class that holds the elevators, and an elevator manager class that has a reference to that singleton bulding object? – Steve Feb 01 '11 at 19:50
  • I would expect an ElevatorManager *instance*, and to make that available only to the classes that need to know about it. If that seems like a huge number, then it's often a pointer that your modelling isn't quite right. – Brian Agnew Feb 01 '11 at 19:56
1

I can think of one way which is to create a singleton class to store all those global variables. Then use static methods to provide access from any class. But is there a more elegant solution?

Nope, that's the way to do it. A combination of static methods and singletons.

Karthik Ramachandran
  • 11,925
  • 10
  • 45
  • 53
1

You could create a non-singleton class with the desired fields, and provide an instance of this to whatever needs it.

Minimizing the amount of your code that presumes a single such context makes it easier to adapt later to multiple contexts.

For example, you might begin with a single set of elevators, but later want multiple sets, for different towers or buildings.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
0

You can create a class with a bunch of public static fields. Something like

class AppGlobals {
   public static final String IMPORTANT_STUFF = "something global...";
   ....

}

Or you can read in a properties configuration to get simple data like Strings and whatnot.

You can also combine 1 and 2.

Edit -- for your elevator/buildings example, proper OO design would eliminate the need for globals....

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
0

You might want to look into the Monostate Design Paradigm if you are shying away from Singleton. Here's a good StackOverflow question on it.

Monostate vs. Singleton

Community
  • 1
  • 1
jluzwick
  • 2,005
  • 1
  • 15
  • 23