Solved

NEWBIE: Please advise on Static design

Posted on 2007-03-29
12
200 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:BrianMc1958
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now