3

My application requires access to certain Objects throughout the application. These objects are populated through the DB.

I would like to populate the objects on server startup , and then in the application on a need -to -do basis.

What is the best pattern to implement this, i have read about the Registry pattern, but not sure that is exactly what i want.

I am also using Spring 3.x in my application , is there something in Spring that can help me?

8
  • 2
    Part of your design consideration should be whether or not these objects are mutable or immutable. Commented Dec 6, 2011 at 14:56
  • 1
    I thought globals were an antipattern. Commented Dec 6, 2011 at 14:56
  • 1
    @TomHawtin-tackline: Every absolute claim is an antipattern (including this one). Commented Dec 6, 2011 at 14:59
  • 1
    @TomHawtin-tackline Using globals in OOP the way one might use global vars in procedural programming is an anti-pattern. However, when used appropriately it is ok. The problem is that most programmers don't use them appropriately... Commented Dec 6, 2011 at 15:01
  • @BrianDriscoll The objects would be mutable , as I would like to refresh the contents when required Commented Dec 6, 2011 at 15:02

5 Answers 5

3

Since you say you are using Spring. The best solution is to use FactoryBean. What this class allows you to do is create a bean of whatever type you like and return it to the Spring context. Spring will manage this bean and expose it to other beans by making it a singleton. An example:

public class MyObjectFooFactoryBean extends SimpleJdbcDaoSupport implements FactoryBean<ObjectFoo> {
    private String query;

    @Override
    public String getObject() throws Exception {
        return an ObjectFoo here using SimpleJdbSupport (Because I also extended SimpleJdbcDaoSupport)
    }

    @Override
    public Class<?> getObjectType() {
        return ObjectFoo.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}

Above does two things. 1) Extends SimpleJdbcSupport which allows you to have access to the databse, and 2) Implements FactoryBean which returns the object to Spring's context. After this is done, you can @Autowire it or use it in the xml file.

Sign up to request clarification or add additional context in comments.

2 Comments

So in cases where I would like to refresh the contents of the object, would that be possible without an extra overhead?
If you'd like to refresh the content, then that become tricky. You can't replace all the beans in Spring with a new instance. But you replace the content of each bean. So I suggest doing the work with in the object to refresh it. It can a little tricky. You can use a Timer to refresh every x seconds, or you can just make everything a prototype which means it will create a new instance every time. Update your question with more detail and I will see how I can help.
3

Are you sure your application really requires that?

Global objects are artifacts from the past of programming, they shouldn't exist anymore in a proper object-oriented environment, and are usually considered an anti-pattern. Surely you don't need to access these objects everywhere. Why don't you just inject them in the classes that need them? If you use Spring, that's even easier, only have to declare the dependency in the context.

If there are many of these objects that you need, you could wrap them all in one holder class, that you inject as needed.

6 Comments

There certainly are cases in the OOP paradigm in which a global object (or global objects) are required. Most applications I've seen have a singleton implementation of the application's runtime configuration, for instance.
Spring is built on top dependency injection all of the objects are singletons by default in the global space. I don't think there is any thing wrong with the way Spring does it because it does it right.
+1 for pointing out that this is exactly what the Spring singleton scope combined with DI is for! Declare them in Spring as singletons and then inject them when you need them.
An interesting take on the simpleton - sorry, I meant singleton: sites.google.com/site/steveyegge2/singleton-considered-stupid
@Alex funny I thought I had said that exactly :)
|
2

When I use global objects I usually wrap each object in a singleton pattern. It's the simplest solution, and you can even lazy load it only when needed.

In some cases this is a very valid pattern. For example in any iphone app you have a singleton Application object which is accessible globally through a public static method

2 Comments

I have tried implementing this , but when I want to refresh the contents of the object , it fails.. :(
why not add a refresh method on your object. on the first initialization of the singleton call refresh to do the initial load, and every other time you need to refresh just call the refresh explicitly. also, don't forget to make the refresh synchronized to make sure you don't update when some other thread is reading
1

This can get a bit hairy if your application is deployed in multiple servers (each with its own VM, thus having singletons as static fields will get messy, and having context-based attributes won't work unless the servers are set to replicate and that's unreliable as well). I'd recommend using a database memory table instead, or some other sort of server-independant memory cache. ( http://memcached.org/ for instance)

Comments

1

What about defining a global object to store these information? You can control access to this object. If you want to monitor this object's state, you can use observer patterns.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.