1

I need to create a class that contains a lot of information (slow to initialize). I am trying to access this class from my other classes. This class should only be created once because it is slow to load and takes a lot of memory. My only idea of how to do this is to create an instance of the class in the loader class and then pass it from class to class as I switch through them.

I feel like there is a better way to do this. I thought about making all the methods and variables in this class static but then I would still have to make a new object in each new class which I think would then re-initialize the object(which I don't want to do).

I am not sure if there is a better way to do this... thoughts?

java
  • 1,319
  • 6
  • 15
  • 30
  • 3
    Singletons.....(but not really). Use dependency injection. – Sotirios Delimanolis Apr 28 '14 at 22:14
  • If you could write your scenario properly, we could help. Why do you need such a big class? Why can't you make it static? On what purpose should you access it? – padawan Apr 28 '14 at 22:16
  • if the class is static you do not need to make a new one in each class. That said it will be just one big global variable. Could get very painful quickly that. – Tony Hopkinson Apr 28 '14 at 22:24
  • Generally, it's ideal that you avoid putting all your data into a mule class. Give classes the data they need, and if another class has it, ask for a copy of the data. – Aarowaim Apr 28 '14 at 22:28
  • See [this fine answer](http://stackoverflow.com/a/16106598/256196) for a discussion/explanation and simple code to do what you need. I would close this question as a duplicate, excerpt you don't explicitly ask for a singleton. – Bohemian Apr 28 '14 at 23:19
  • I'm trying to create a more efficient activity switch in my android app. I have all the images in one class and load only the ones that are need and recycles those that aren't need. Because a lot of images are used between activities, it saves time not having to reload them. Also this makes it easy to recycle images no longer in use, thus saving memory. – java Apr 28 '14 at 23:19

1 Answers1

0

What you are asking for is a singleton. The easiest way of doing this is using an enum instance - this is the simplest way to takes care of thread-safety issues that can arise when you try to use static members.

That said, singletons are generally considered to be a sign of an architecture problem. This is a general rule, and you may very well have good reasons for doing it this way - just make sure that your reasons are valid. The singleton anti-pattern tends to create strong coupling between different layers of the application, leading to fragile, difficult to test code.

As an alternative, if dependency injection is an option for you, then construct your object once and inject it where needed. Note that you don't have to have a DI framework to use dependency injection (you can just pass the object in to the constructors of the dependent objects). This allows you to be hyper-aware of the locations where that singleton is actually used.

Kevin Day
  • 16,067
  • 8
  • 44
  • 68