Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

initialization of static utility class  problem

Thread view: 
puzzlecracker - 18 Aug 2006 16:11 GMT
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
Tobias Schierge - 18 Aug 2006 16:16 GMT
> 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
Patricia Shanahan - 18 Aug 2006 16:22 GMT
...
> Above utility class, can only be used when utility is initialized with
> specific name. I accomplish that by either setting the name or calling
[quoted text clipped - 9 lines]
> Can someone suggest an alternative, more elegant solution (perhaps
> along the AOP lines :) )?

Why all static, rather than singleton?

Patricia
puzzlecracker - 18 Aug 2006 16:30 GMT
> 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()

---
I don't (as well as a requirement) feel like burdening users with a
factory call.
Soren Kuula - 19 Aug 2006 00:38 GMT
>>Why all static, rather than singleton?
>
> 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
puzzlecracker - 19 Aug 2006 15:59 GMT
> >>Why all static, rather than singleton?
> >
[quoted text clipped - 37 lines]
>
> Soren

That is what I have. I was wondering whether another, more elegent, but
with the same net effect solution exists
Patricia Shanahan - 19 Aug 2006 17:10 GMT
...
> 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
Patricia Shanahan - 19 Aug 2006 20:28 GMT
...
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.