Vivek
Vivek

Reputation: 2101

Best Pattern to create Global Objects in Java

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?

Upvotes: 3

Views: 4933

Answers (5)

zhaoyw
zhaoyw

Reputation: 31

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.

Upvotes: 1

talkol
talkol

Reputation: 12897

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

Upvotes: 2

Slash
Slash

Reputation: 506

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)

Upvotes: 1

Guillaume
Guillaume

Reputation: 22822

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.

Upvotes: 3

Amir Raminfar
Amir Raminfar

Reputation: 34169

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.

Upvotes: 3

Related Questions