There is some interfaces in my app and I want to create singleton class, that provides implementations of them. Like so:
public class Singleton{
//singleton's stuff
private Interface1 interface1Impl;
private Interface2 interface2Impl;
public Interface1 getInterface1(){
return interface1Impl;
}
public Interface2 getInterface2(){
return interface2Impl;
}
}
What I’m looking for – provide same interface implementation for each class in app. With this way everything works fine, but is it a good way to achieve this?
Yes, it looks much like a Factory (or maybe Service Locator is more suitable in your case).
Factory is almost always a better idea than a Singleton. For instance, a Factory can work as a Singleton when you need it (lazy initialization, caching), and you can alter this behavior when you need something else (for testing, thread safety, etc).