initialization of static utility class problem

P

puzzlecracker

I have an interesting problem to which I have written a solution I don'
t like.


Here is a sample static class (followed by more precise requirements):


public class Initializer{

private String name ="DefaultName";

private SomeUtil utility=null;

private static void init(){
if (utility!=null){
return;
}
//initialize utility with name
}

static void setName(String name){
this.name=name;
}

static T someOp1 (T t){
init();
//some stuff
}
static T someOp1 (T t){
init();
//some stuff
}
static T someOp1 (T t){
init();
//some stuff
}

//***

}

Above utility class, can only be used when utility is initialized with
specific name. I accomplish that by either setting the name or calling
an operation, that performs a dirty check to decide whether
initialization is needed, and if so - default name is set

Support initialization is expensive and doing more than once in not
desirable.


I don't like the above solution because it calls init on every
operation.


Can someone suggest an alternative, more elegant solution (perhaps
along the AOP lines :) )?

Thanks
 
T

Tobias Schierge

Here is a sample static class (followed by more precise requirements):
....
Can someone suggest an alternative, more elegant solution (perhaps
along the AOP lines :) )?

Must the utility methods be static? If not, why not use a singleton for
this?

Regards,

Tobias
 
P

Patricia Shanahan

puzzlecracker wrote:
....
Above utility class, can only be used when utility is initialized with
specific name. I accomplish that by either setting the name or calling
an operation, that performs a dirty check to decide whether
initialization is needed, and if so - default name is set

Support initialization is expensive and doing more than once in not
desirable.


I don't like the above solution because it calls init on every
operation.


Can someone suggest an alternative, more elegant solution (perhaps
along the AOP lines :) )?

Why all static, rather than singleton?

Patricia
 
P

puzzlecracker

Why all static, rather than singleton?

I want users to either use this class without any initialization:

UtilityClass.oper1()

.... as well as , a really, for really tiny community, tp have an
option to initialize with name


UtilityClass.setName();
UtilityClass.oper1()
 
S

Soren Kuula

puzzlecracker said:
I want users to either use this class without any initialization:

UtilityClass.oper1()

Can't you do something like:

class MyUtils {
private static boolean alreadyDecidedWhatToDoAboutInitialization;

private static void init() {
if (!alreadyDecidedWhatToDoAboutInitialization) {
alreadyDecidedWhatToDoAboutInitialization = true;
computePlanForSolvingWorldEnergyProblems(OtherClass.something);
saveTheWhales();
//...
}
}

public Type1 DoStuff1(Type2 t) {
init();
BlaBlah(t);
//more
}

public Type3 DoStuff1(Type3 t) {
init();
BlaBlahBlah(t);
//more
}
}

It's fairly standard. .. the call to init() when the if-test eval to
false takes almost no time.

BTW, it is not a "static class" -- just a class without any instance
members / methods. A static class is a class nested inside another
class, and declared static.

Soren
 
P

puzzlecracker

Soren said:
Can't you do something like:

class MyUtils {
private static boolean alreadyDecidedWhatToDoAboutInitialization;

private static void init() {
if (!alreadyDecidedWhatToDoAboutInitialization) {
alreadyDecidedWhatToDoAboutInitialization = true;
computePlanForSolvingWorldEnergyProblems(OtherClass.something);
saveTheWhales();
//...
}
}

public Type1 DoStuff1(Type2 t) {
init();
BlaBlah(t);
//more
}

public Type3 DoStuff1(Type3 t) {
init();
BlaBlahBlah(t);
//more
}
}

It's fairly standard. .. the call to init() when the if-test eval to
false takes almost no time.

BTW, it is not a "static class" -- just a class without any instance
members / methods. A static class is a class nested inside another
class, and declared static.

Soren

That is what I have. I was wondering whether another, more elegent, but
with the same net effect solution exists
 
P

Patricia Shanahan

puzzlecracker wrote:
....
That is what I have. I was wondering whether another, more elegent, but
with the same net effect solution exists

I don't know of one, given the all-static-method requirement.

Patricia
 
P

Patricia Shanahan

puzzlecracker wrote:
....
Support initialization is expensive and doing more than once in not
desirable.

How about default name initialization in a static block, and redo the
initialization if the name gets set. I think you said that name setting
is infrequent?

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top