binarycreations
binarycreations

Reputation: 3181

Should I use a modified singleton design pattern that only allows one reference to its instance?

I have a class that would normally just generate factory objects, however this class should only used once throughout the program in once specifix place. What is the best design pattern to use in this instance?

I throught that having a modified singleton design which only allows one reference to instance throughout the program would be the correct way to go. So only the first call to getInstance() returns the factory library.

Is this a good or bad idea? Have I missed out another fundermental design pattern for solving this problem?

Thanks for your help.

Upvotes: 0

Views: 216

Answers (4)

mP.
mP.

Reputation: 18266

Instead of adding a final static field = new SingletonClass() and making the ctor private, why not control the creation yourself when you pass it as a parameter to all the stuff that needs a copy. This is the entire point of dependency injection, classes dont need to call static fields to get a singleton, they instead receive the instance they need as a parameter in the ctor or setter.

Upvotes: 2

Don Roby
Don Roby

Reputation: 41137

I have a class that would normally just generate factory objects, however this class should only used once throughout the program in once specifix place. What is the best design pattern to use in this instance?

If it should only be used once, only call it once. If you wish to enforce that it can be called only at that place in the code, you can control that completely by making it a private inner class of the class containing the call.

Upvotes: 1

Erick Robertson
Erick Robertson

Reputation: 33068

The question is - if sometime in the future someone decides to use this class in a different part of the program, how should it work?

A. That's fine, let them. It won't impact the other use of it. (Don't use a singleton.)

B. If there's a good reason for it, that's fine, but it's going to mess up the first use of it (due to shared resources or whatever) so they have to use the same instance that's being used elsewhere. (Use a singleton.)

You say that it should only be used once throughout the program and in one specific place. If that's true because of a technical restriction, make it a singleton. If it's true because that's what your view of the design is, but there's no technical reason to put the restriction in place, then don't make it a singleton.

Upvotes: 1

Simeon
Simeon

Reputation: 7792

however this class should only used once throughout the program in once specifix place

I don't think making a singleton would prevent someone to use it in more than one place, since usually singletons are global state.

In your case I would probably just make the factory class internal to the class which needs to use it. Or simply use access modifiers to ensure its not used anywhere else (package-private or protected might be what you're looking for).

Upvotes: 1

Related Questions