Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

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
0
BrianMc1958
Asked:
BrianMc1958
  • 6
  • 3
  • 2
  • +1
2 Solutions
 
mnrzCommented:
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
 
BrianMc1958Author Commented:
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
 
mnrzCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
BrianMc1958Author Commented:
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
 
mnrzCommented:
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
 
imladrisCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
BTW are you not looking forward to using some thing like Entity beans or Hibernate/ Toplink to have relationally mapped Java objects?
0
 
BrianMc1958Author Commented:
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
 
BrianMc1958Author Commented:
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
 
imladrisCommented:
>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
 
BrianMc1958Author Commented:
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
 
BrianMc1958Author Commented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now