The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

oh static..static static

i have read around in trying to understand when to make a class static and when to make method static?

i still can't seem to grasp the theory completly.

for eg., all the clients logged on to our server poll every one minute which in turn instantiates an object that basically finds  for that logged in user is there any messages etc..


now my question is would this class be a good candidate for static - class and the  method (ashould it be static) it implements.

why? instead of creating and destroying object every minute for each logged, it will be created once and if method is static,it will be loaded into code space once.

so is this a good approach? any pros /cons?

 next question would be, what will happen to variables defined?

continuing in the same vein,we have another class, through which every body instantiates database connection. so again, is this a good candidate? how abt the method that actually makes the db connection? would there be any drawback such as wrong connection going to the wrong callee?

thanks
dan
dan
Saturday, September 16, 2006
 
 
Use Static Methods for functions that do not act on member variables.  If you've got a function inside a class that just performs an operation on some variables, and never touches any properties, consider that as a static method. 

In the realm of usefullness, traditionally, Factory pattern implementations (all the "Object a = Object.Parse(string)" type methods) are static.  As are Singleton implementations ("Object.Instance.DoSomething()" type functions).  As are some comparitors.

Static classes are used to simply label and control the visibility of a group of static functions.

Ie//

public static class Bob {

        public static void DoSomething(){
            ... Do some stuff
            DoSomething_2();
        }
        private static void DoSomething_2(){
            ... Do something extra
        }
}

public class Bob2 {
        public void DoSomething(){
            ... Do Something...
        }

        // Factory implementation
        public static Bob2 Parse(string s){
            return new Bob2();
        }

        // Singleton
        // Any method in the class (public, protected, private) can change sm_Bob
        private static Bob2 sm_Bob = null;
        public static Bob2 Instance(){
            if (null == sm_Bob)
                sm_Bob = new Bob2();
             
            return sm_Bob;           
        }

        // Singleton v2. (C++ specific)
        public static Bob2 Instance2(){
          // The only way to ever touch the st_Bob
          // variable is to go through this method.
          static Bob2 st_Bob = null;

          // st_Bob is only freed when the application
          // terminates.
          if (null == st_Bob)
            st_Bob = new Bob2();

          return st_Bob;
        }

        public static int Add (int a, int b){
            return A + B;
        }
}

Main(){
        // Static Class
        Bob.DoSomething();

        // Factory
        Bob2 b = Bob2.Parse("some string");
        b.DoSomething();

        // Singleton
        Bob2.Instance.DoSomething();

        // Singleton V2
        Bob2.Instance2.DoSomething();

        int three = Bob2.Add(1, 2);
}

Hope this helps.

- James.
James Birchall Send private email
Saturday, September 16, 2006
 
 
A static method/class doesn't mean only 1 instance.
oh DLL hell.

Statics in C++ are a preatty wrapper on a global, which in general is bad.

For instance, the previous example (repeated below) is not thread safe, also depending on compilation, there can still be more than 1 bob, it's an 'almost a singleton'.

public static Bob2 Instance2(){
          // The only way to ever touch the st_Bob
          // variable is to go through this method.
          static Bob2 st_Bob = null;

          // st_Bob is only freed when the application
          // terminates.
          if (null == st_Bob)
            st_Bob = new Bob2();

          return st_Bob;
        }


----
Say you had your message class, and had a static to represent to all the instances do_somthing.

now you have a new type of message class, and to keep the code clean you put it in a seperate dll.

of course, you don't duplicate code, you refactor and put the static and other common stuff into a parent class.

but when you call the static function/set the static variable to do the action, only the clients using the second dll do anything now. the first ones don't get notified, because they have a different compiled instance.
of course you can avoid the issue by having gargatuan non pluggable build.

using a 'singleton' to provide your information has a similar problem, who owns the singleton?

make the workflow in your app, create the single object and pass it on construction to the children and then they can get the information, and you know whats goin on.
Gornin Send private email
Wednesday, September 20, 2006
 
 
I was wondering how long it would take anyone to catch that...

- James.
James Birchall Send private email
Wednesday, September 20, 2006
 
 
thanks both of you.

i profiling my application to determine how expensive are my object creation and destruction.
if i find any improvements and no problem, i may go head and try it out.
dan.
dan
Friday, September 22, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz