?
Solved

NEWBIE: Please advise on Static design

Posted on 2007-03-29
12
Medium Priority
?
247 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 200 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 1800 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

764 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