Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

[Program design] How to manage often used objects?

Thread view: 
Kai Schlamp - 17 Nov 2007 00:32 GMT
Hello!

I have a question regarding general program design.
I ask myself, what's the best way to handle for example a database
connection object or general settings object, that is accessed in many
different locations in my program.
Is it best to store those in a static class to easily access them, or
is it better to pass the instance itself through the whole program?

Kai
Arne Vajhøj - 17 Nov 2007 00:34 GMT
> I have a question regarding general program design.
> I ask myself, what's the best way to handle for example a database
> connection object or general settings object, that is accessed in many
> different locations in my program.
> Is it best to store those in a static class to easily access them, or
> is it better to pass the instance itself through the whole program?

Pass around or singleton depending on how much clutter needed to
pass it around.

Arne
Stefan Ram - 17 Nov 2007 00:50 GMT
>Is it best to store those in a static class to easily access them

 One can not condemn this solution, because this is just the
 way Sun is doing it with »java.lang.System.out«, and I have
 not heard criticism of »java.lang.System.out«.

 In fact, most Java tutorials start with a simple program using
 »java.lang.System.out«. I have not yet heard someone
 criticising these tutorials for »teaching a bad style right
 from the start«.

 Another solution is a static getter, like
 »java.lang.Runtime.getRuntime()«.

>is it better to pass the instance itself through the whole program?

 If it is an object used for debugging, than, to get the
 production version without it, all interfaces would need to be
 changed.

 Thus, public static fields can be used, but care is needed to
 tell, when to use them and when to use non-static fields or
 parameters or other means instead.
Lew - 17 Nov 2007 01:49 GMT
>> Is it best to store those in a static class to easily access them
>
[quoted text clipped - 19 lines]
>   tell, when to use them and when to use non-static fields or
>   parameters or other means instead.

If something belongs to the class as a whole, make it static.

If not, make it instance.

If in doubt, usually (not always) make it instance.

Signature

Lew

Stefan Ram - 18 Nov 2007 15:45 GMT
>>is it better to pass the instance itself through the whole program?

 Another means I have not considered yet, would be dynamic scoping

http://research.microsoft.com/users/drh/pubs/dynamic.pdf

 It is not implemented in Java, but one could use a call

Util.get( "name" )

 which would inspect the current call stack and the variables
 of each method's object for the first occurence of a variable
 named »name« (or »dynamic_name«, in order to make sure that
 this does not coincidentally match).
Mark Space - 17 Nov 2007 04:53 GMT
> Hello!
>
[quoted text clipped - 6 lines]
>
> Kai

Recently there's has been some trends to use design patterns such as
Inversion of Control, including Dependency Injection and Aspect Oriented
Programming.  Look those up with Google, the results are interesting.

I would, without knowing anything else about your system, make a single
factory object that returns a single reference to this expensive object:

class MyFactory {
    private static DBC dbc;
    public DBC getDBC() {
        if( dbc == null)
            dbc = new DBC();
        return dbc;
    }
}

But don't let your lower level routines call it directly.  Instead,
inject either this object (or the result of its getDBC() method) into
the lower level business object by passing the dbc as a parameter.  You
can do it at object creation as part of the constructor

    ShoppingCart cart = new ShoppingCart( customerName,            
        MyFactory.getDBC() );

or create the object, then set the dbc with a setter method.
    ShoppingCart cart = new ShoppingCart();
    cart.setDBC( MyFactory.getDBC() );

The thing to watch for is testing. It's hard to test when there's hard
coded references to a class like MyFactory everywhere.  By setting a
parameter, you can test just by making a test DB object and passing
that.  Your code will automatically be more flexible and easier to
maintain as a result.
Kai Schlamp - 19 Nov 2007 01:06 GMT
Thanks a lot for all the answers and suggestions. You helped me a lot.
I will go the singleton way together with dependency injection. That
sounds really perfect for my needs.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.