Solved

NEWBIE: Please advise on Static design

Posted on 2007-03-29
12
221 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:BrianMc1958
  • 6
  • 3
  • 2
  • +1
12 Comments
 
LVL 8

Expert Comment

by:mnrz
ID: 18815789
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;
  }
}


0
 

Author Comment

by:BrianMc1958
ID: 18815835
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.
0
 
LVL 8

Expert Comment

by:mnrz
ID: 18815965
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
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:BrianMc1958
ID: 18816033
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...
0
 
LVL 8

Expert Comment

by:mnrz
ID: 18816174
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?
0
 
LVL 16

Expert Comment

by:imladris
ID: 18816666
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.
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 50 total points
ID: 18818812
BTW are you not looking forward to using some thing like Entity beans or Hibernate/ Toplink to have relationally mapped Java objects?
0
 

Author Comment

by:BrianMc1958
ID: 18839558
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
0
 

Author Comment

by:BrianMc1958
ID: 18839578
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%!
0
 
LVL 16

Accepted Solution

by:
imladris earned 450 total points
ID: 18844085
>Can I?
Yes.

>What happens to THEM if I instantiate the whole class as Static?
What I've shown is a normal instantiation of the class like any other instantiation. A reference to it is assigned to the persist variable, same as you would do whenever you create a new object. It just so happens that persist is static and so it is not going to go away.
The non-static (member) variables inside the DatabaseThing class will happily remain member variables. They are associated with the instance of the class.

Consider the canonical Circle class:

class Circle
{   Point centre;
     int radius;

     Circle(Point c,int r)
     {   centre=c;
          radius=r;
          return;
     }

     void draw(Graphics g)
     {   ...
     }
}

Circle is a regular class with member variables. If I instantiate it in a method in my manipulate class:

      void doCircle()
      {   Circle myc=new Circle(new Point(0,0),10);
           ...
      }

it's all fine. In this case the class is referenced by a local/stack variable (myc). If it goes to a member variable instead:

class Manipulate
{    Circle mcirc;

      Manipulate()
      {    ...
      }

      void doCircle()
      {   mcirc=new Circle(new Point(1,1),5);
           ...
      }
}

nothing much has changed. The Circle class is just the same; except now you can access this *instance* of it throughout the Manipulate class. Finally:

class Manipulate
{   static Circle scirc;

     Manipulate()
     {   scirc=new Circle(new Point(5,5),20);
          return;
     }

     void doCirc()
     {   draw(scirc);
     }
}

again a Circle object is created, of the exact same sort. But now *this instance* of it can be accessed from anywhere by prefixing the class name, since scirc is static:

class Other
{   Other
     {   ...
     }

     void doSomething()
     {   int r=Manipulate.scirc.radius;
     }
}

The data inside the Circle class remain member variables. By assigning to different references you alter how, and how widely, that reference can be accessed. But the Circle class itself does not change.

>Oh, and if I turn this thing into a really marketable product, can I use the name "DatabaseThing"?
Absolutely.
0
 

Author Comment

by:BrianMc1958
ID: 18845011
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
0
 

Author Comment

by:BrianMc1958
ID: 18845019
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.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HSSFWorkbook cannot be resolved error 10 71
servlet example 11 40
hibernate example issues from command prompt 10 42
Java string replace 11 48
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question