Page 1 of 1

Global Class

Posted: Mon Aug 14, 2006 9:17 am
by Benjamin
What is the procedure for a class that meets the following criteria:

1. May be called from many other classes
2. Generally, only 1 instance is needed, but sometimes more (ie cannot be a Singleton
3. Has already been instantiated before any other classes

Using a global var would be easy but I'm not sure that is the proper method.

I don't want to instantiate it everytime I need it, but sometimes I will need to have 2 or more copies of it running concurrently. Would it be best to simply make a copy of it?

EDIT: Also, what would be the best way to call this class from other classes?

Posted: Mon Aug 14, 2006 9:19 am
by feyd
either pass a reference to it when creating the objects (or calling their methods) ... or use a registry. The latter doesn't seem to fit well though since you need to deal with multiple instances.

Posted: Mon Aug 14, 2006 9:21 am
by Jenk
Search for Factory and Registry.

Posted: Mon Aug 14, 2006 5:12 pm
by Christopher
A class that can have one or more instantiated objects and is not a Singleton is just a plain old class. If you want it created before it is used then you have the option of creating an instance and passing it into where you will use it either directly or via a Registry/Factory. You can also Lazy load the class/object by supplying the "wiring" to instansiate it to something like Service Locator and then it will be created the first time it is asked for.

Posted: Mon Aug 14, 2006 6:39 pm
by Chris Corbyn
Registry for me. You can still use multiple instances but you can also use the registry to keep fetching the same instance if you want.