Link to home
Start Free TrialLog in
Avatar of BrianMc1958
BrianMc1958

asked on

NEWBIE: Please advise on Static design

Dear Experts,

I have a class designed to encapsulate selections from a database table.  I want to use it for two different purposes:

First, I'd like to have a basically "Static" version, that would go read all the data from the table ONCE up front, and store it "Static-ally", so any other classes can easily access it.

Second, I'd like to use the same class to be able to read particular data for particular purposes.  This should NOT be static.  Each user class would want it's own copy.

Could you folks advise me on how to do this?

I know my class could contain both static and non-static sets of variables, but that seems ugly.  And I could have two versions of the class, but that seems wasteful...

The ideal, I think, would be to make an instance of the entire class "Static", but I've never done that and don't understand the implications.

Thanks!
BrianMc1598
Avatar of mnrz
mnrz

hi
first off, to design a class you have to choose one purpose for each class this is called High Cohesion. your class should be well-focused.

you have better to build a class for those static data and another for dynamic data. dont think of wasting memory for more classes.

public class BasicData {
  private static List basicData
  ....
  public static List getBasicData(){
          return basicData;
  }
}


Avatar of BrianMc1958

ASKER

Although I'm a newbie, I have to disagree here.  (I not THAT much of a newbie.)  

My class is fairly complex.  It is passed a connection, and then it does all the work of creating a statement, executing it, reading in all the results, and storing those results in pre-configured arrays.  (It does a lot of other stuff, too).

The ONLY difference between my two uses would be whether or not those resulting arrays were Static or not.

So it seems to me I definitely SHOULD use the same class, if at all possible.  (And as usual, I'm just depending on you Experts to tell me HOW to make it possible...)

If it's really not feasible, of course, I'll give up soon and follow your advice.  I'm sure I could accomplish this by creating a super class, but that would be a little complicated here.
a complex class is not a good design. you have to divide your class and decouple them.

you need a class only to create database connections
another class for managing your given queries and returning results from database

and this last class you are describing only use the second class and doesnt involve in database connections

if your data wont change during your application life and no need to re select them from database, you have better to store them in a static variable.

otherwise you have to keep them in non-static variables
I think these classes are reasonably well designed.  I DO have a separate class that manages connections.  By "complex", I just mean that out of the 200 or so lines of class code, only 3 would need to change to get these two different behaviors.  And those 3 would change only like this:

      public static List listAt_Application = new ArrayList();
      public static List listAt_ApplicationDesc = new ArrayList();
      public static List listAt_ApplicationGroup = new ArrayList();

Just the keyword "static" would be present or absent.  

It seems there must be a simpler way...
any way, you had better to use static method to access those list variables and mark them with "private"

you dont need to change anything else and your code.

as you said:
>>The ideal, I think, would be to make an instance of the entire class "Static", but I've >>never done that and don't understand the implications.

please tell what is that you cant understand  that implication?

did you mean you want to make all the class static?
I'm not quite clear on how the class works.

Would it be possible to create an instance of the class for the data you want to keep easily accessible and just assign it to a static variable?

Something like:

public static DatabaseThing persist=new DatabaseThing("getstaticdata");

The persist variable is public and static, and so easily accessible from anywhere.
SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've been away, and still can't take the time to check out these responses.  But they look good !

Will try to respond Wednesday.

--BrianMc1958
To imladris:

Your proposal was my original plan.  But I haven't ever instantiated a class as Static that wasn't designed to be instantiated that way.  As is, DatabaseThing has plenty of non-static variables and methods.  What happens to THEM if I instantiate the whole class as Static?  Can I?

Oh, and if I turn this thing into a really marketable product, can I use the name "DatabaseThing"?  I'll give you 15%!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To imladris:  After Experts answer my questions, I always say "Thanks" or even "Thanks a lot".  But I'm sometimes just blown away that someone I don't know would take so much time to not just answer the question, but explain it in such a way that I really understand what's going on behind the scenes.  This would be one of those times.  Thanks!

--BrianMc1958
To mayankeagle:  Yes, I looked at Hibernate a while ago (at the suggestion of EE, of course), and I'm looking at it again.  Thanks.