Design template for a & ldquo; Library & rdquo; In Java?

advertisements

I'm looking for a design pattern for a set of methods which will be used in several projects so I'm making a "library" with them.

At first I thought of making all of them static so they could be called by Library.methodName() and that's it. I did and realized I was passing around a couple of constant values for every method which can set once and then used inside the class. (This constant values change between projects)

Now I thought of having a Class which has to be instantiated with this values and access the methods trough the object without passing the values every time, however I want to prevent the creation of objects of the "Library" class every time so I thought of Singleton.

I did make a Singleton with all of this, however I need to make an explicit set for the class variables at first, but this approach requires the programmer to now that the value has to be set before using the methods, which will make it fail for sure eventually, even by myself.

doing Singleton.getInsance(value1, value2).method(...) all the time is out of the question, I'd prefer my initial approach. Also, I can't define a "default" value for this variables, they HAVE to be set.

Is there a design pattern that will help me have a Singleton which can be initialized with given values and not having to pass them around all the time?

or should I just stick with my initial approach and make the programmer pass the values all the time?

or any other option, any design pattern that fits, or anything else may also work.

Thanks


should I just stick with my initial approach and make the programmer pass the values all the time?

The only alternative to this I can see is to have a Constructor for a context which holds and sets all the mandatory values. I would avoid using a stateful singleton if you can.

If the caller wants to avoid creating the object multiple times, they can make it a singleton if they want to, but you shouldn't force them to use a singleton.