Home > Archive > Java Help > January 2007 > using one instance of a class across multiple classes
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
using one instance of a class across multiple classes
|
|
| lambelly@gmail.com 2007-01-23, 4:14 am |
| I am attempting to write a class which handles numerous things for an
application that I'm working on. Multiple classes will use this manager
class. As such, I need to use the same instance of the class across the
whole of the application so that it'll remember what state these things
it's managing are in. What would be the best way to go about doing
this?
I'm sure this is a fairly easy question, but its answer is beyond my
knowledge of java objects.
| |
| Andrew Thompson 2007-01-23, 4:14 am |
| lambelly@gmail.com wrote:
> I am attempting to write a class which handles numerous things for an
> application that I'm working on. Multiple classes will use this manager
> class. As such, I need to use the same instance of the class across the
> whole of the application so that it'll remember what state these things
> it's managing are in. What would be the best way to go about doing
> this?
See the "Singleton design pattern".
Andrew T.
| |
| lambelly@gmail.com 2007-01-23, 8:08 am |
|
Andrew Thompson wrote:
>
> See the "Singleton design pattern".
>
> Andrew T.
Thanks, that was very helpful!
| |
| Andrew Thompson 2007-01-23, 8:08 am |
| lambelly@gmail.com wrote:
> Andrew Thompson wrote:
....[color=darkred]
> Thanks, that was very helpful!
You are welcome. It is always a pleasure to
help out folks who can ask smart questions,
and do a little research.
Andrew T.
| |
| Randolf Richardson 2007-01-28, 4:13 am |
| On Tue, 23 Jan 2007 03:26:00 -0800, Andrew Thompson
<andrewthommo@gmail.com> wrote:
> lambelly@gmail.com wrote:
> ...
>
> You are welcome. It is always a pleasure to
> help out folks who can ask smart questions,
> and do a little research.
Thanks from me too, Andrew -- I've done a lot of work with Reflection,
but not Singletons so far. I found this link, which cites some very good
examples:
Simply Singleton
Navigate the deceptively simple Singleton pattern
http://www.javaworld.com/javaworld/...gnpatterns.html
--
Randolf Richardson - kingpin+nntp@lumbercartel.ca
The Lumber Cartel, local 42 (Canadian branch)
http://www.lumbercartel.ca/
| |
| andrewmcdonagh 2007-01-28, 4:13 am |
|
On Jan 23, 9:25 am, lambe...@gmail.com wrote:
> I am attempting to write a class which handles numerous things for an
> application that I'm working on. Multiple classes will use this manager
> class. As such, I need to use the same instance of the class across the
> whole of the application so that it'll remember what state these things
> it's managing are in. What would be the best way to go about doing
> this?
>
> I'm sure this is a fairly easy question, but its answer is beyond my
> knowledge of java objects.
The Singleton is a valid design pattern....but it has some fundamental
problems. Google 'Singleton vs just create one'.
For me, the biggest problem is that a Singleton has 2
Responsibilities:
1) Ensure only one instance is created
2) Be that instance
e.g.
class BlarManager {
private static BlarManager intance = new BlarManager();
public static BlarManagergetInstance() {
return instance;
}
//...other methods.
}
This might sound weird, small, etc., but the problem comes from the
tight coupling of one class's instance around the entire code base.
A slightly different take on the Singleton is the 'Container' design
pattern approach.
Here, we have a Singleton-like class that is accessible to the whole
code base, but just holds references to other object.
class Main (
public static void main(String[] args) {
// setup container....
Container.addInstance("BLAR", new BlarManager() );
Container.addInstance("Feefoo", new FeeFooManager() );
// start the app.
}
}
class Container {
public static void addInstance(Object key, Object instance) {
instancesMap.put(key, instance);
}
public static Object getInstance(key) {
return instancesMap.get(key);
}
}
Now this is a very simplest Container to show the principle. if you
wanted this approach, then idea suggest using one of the many
opensource (fully tested) Container libraries.
PicoContainer is probably the best I've used.
http://www.picocontainer.org/3.1+Container+Overview
HTH
Andrew
|
|
|
|
|