Reputation: 5656
I am implementing a program which uses a shared utility class with singleton behavior.
One instance of the utility class is created in the main thread and passed to all other objects instantiated:
SomeUtil util = new SomeUtil();
...
Foo foo = new Foo(util, arg1, arg2)
Bar bar = new Bar(util, arg3, arg4, arg5)
Is there some more elegant way of achiving this (i.e. design pattern)?
Upvotes: 3
Views: 3789
Reputation: 3419
Probably the nicest way to get around this is add/use some Dependency Injection framework.
Upvotes: 1
Reputation: 116266
As others have mentioned, Singleton can be an alternative. Note though that your current design is easy to unit test (since you are injecting the SomeUtil
dependency, which can thus easily be replaced by a mock object during unit tests), while Singleton makes unit testing awkward and difficult:
That being said, if it is a real utility class, i.e. it has no internal state, and it is not dependent on anything which would make unit testing difficult (like a DB, or file system), it can be OK to use it as a Singleton (although this begs the question why do you need to instantiate it at all - usually utility classes have only static methods, and a private constructor to prevent instantiation).
Upvotes: 5
Reputation: 81074
Well, you could actually use the singleton pattern which typically stores a static reference to the single instance in the class itself.
public class SomeUtil {
private static final SomeUtil instance = new SomeUtil();
public static SomeUtil getInstance() {
return instance;
}
//...
}
Doing this can make things less unit testable however, so be aware. It's typically much easier to break an injected dependency than a global one.
Upvotes: 3
Reputation: 10598
Why do you pass around utility object? The SomeUtil can have static methods, so that the methods in Foo and bar can use it.
Also, you can implement an actual singleton pattern.
Upvotes: 4
Reputation: 6450
Why not just have an actual singleton? Much nicer than passing a single instance around everywhere.
Upvotes: 1