• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to allow all classes in your project have access to a single class?  RSS feed

 
Allen Hamilton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this class called UserPrefs, and it is accessed by many other classes in my program that need to write to it or read from it.

I need all of these classes to be accessing the same instance of this class in memory.

So what I do now is to create a single instance of it at the beginning of the program when creating the main JFrame, then I pass this reference to the constructor of the main panel, and any of the multiple dialogs that need access to it are passed a reference to it.

The problem is that I find myself having to pass a reference to this class from constructor to constructor, and it becomes difficult to follow the code after a while.

Is the easiest way to solve this just to make all of the methods in this class static?

I also briefly looked into the singleton design pattern, and it looks like it could be useful here.

Any ideas?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be aware that many people consider the Singleton pattern to be an Anti-pattern.
 
Allen Hamilton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Be aware that many people consider the Singleton pattern to be an Anti-pattern.


Thank you for pointing that out.

I did some quick research and apparently "dependency injection" (what I've been doing) is preferable over using a singelton.

Here is the best explanation of dependency injection that I've found:

Dependency Injection

Basically, all that "dependency injection" means is passing a variable into a constructor.

This is important in my case because this UserPrefs object is constantly mutating, and all these different classes need immediate access to its present state.

Anyway, the one thing that I could do to make the code more readable is to have the UserPrefs class implement a couple of different interfaces, and pass a specific interface object into each constructor, depending on that class's needs.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should also have a look at the Wikipedia article God object, which also seems to be relevant to your perceived requirements. It may turn out that the singleton object is in fact the best way to pass the information around, but there are alternatives. For example you may need to persist your user preferences (I'm making the wild guess that your UserPrefs class is for user preferences ), in which case using the classes in the java.util.prefs package to store them externally might be a possibility.
 
Allen Hamilton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:You should also have a look at the Wikipedia article God object, which also seems to be relevant to your perceived requirements. It may turn out that the singleton object is in fact the best way to pass the information around, but there are alternatives. For example you may need to persist your user preferences (I'm making the wild guess that your UserPrefs class is for user preferences ), in which case using the classes in the java.util.prefs package to store them externally might be a possibility.


Hey and thanks for your response.

Yep, I definitely need to persists UserPrefs. I've just been serializing the object up until now.

I do think UserPrefs has been turning into something of a God object lately, or maybe just a demi-god object. I'm storing both account information and user preferences in it right now. I knew a time would come when I needed to separate them out, so it may as well be now.

I'm already using java.util.prefs to store some gui preferences, so I may just end up using that for the user preferences. I suppose that would have made sense from the beginning
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!