0

here is my problem--

I have 4 classes - Starter , Database and Scheduler and a Test class.

Test class will create a new instance of Starter (which loads and starts the entire process). Starter initializes Scheduler and Database classes.

Test class passes a data to Starter. Starter stores it in a HashMap in Database class. Scheduler reads the same HashMap from Database class.

Now to ensure that the exact same HashMap is access by all classes throughout my java project, I have 2 options-- to make HashMap static or to make Database a singleton class. I have made Database a singleton class for now.

The problem-- if Test class does this

Starter starterInstance1 = new Starter();

Starter starterInstance2 = new Starter();

how do i ensure starterInstance1 and starterInstance2 have their own instance of Database class or the HashMap?

==========

Just being more clear:

class Test{

Starter start1 = new Starter();//creating 1 instance of my application

start1.init();//this will initialize Scheduler etc and do a "getDatabaseInstance()"

for(int i=0;i<50;i++){

start1.sendData("abc"); //all these 50 requests will b submitted to a threadpool that will send request to be stored in HashMap in Database class. The HashMap will be accessed concurrently by Scheduler and other classes. I have made the Database class singleton so that all classes will be accessing the same instance of DB.

}

//Similarly creating another instance of my application

Starter start2 = new Starter();

start2.init();//now here if I do "getDatabaseInstance()", i will get the same instance as above. but i want a separate Database for this instance of application. how do i achieve this?

for(int i=0;i<50;i++){

start2.sendData("abc");

}

}

as.tek
  • 937
  • 3
  • 14
  • 20
  • If Database is a singleton, I don't see how they could have a different instance. That's the point of the singleton pattern. One part of your question says that the same exact map must be accessed by all the classes, and another one says that each starter instance should have their own instance. What do you want? – JB Nizet Feb 04 '12 at 08:49
  • yes singleton will imply having just 1 instance. but i dont want that. my entire application's starting point is Starter class. whenever a new instance of my application is created (i.e. new Starter()), i want it to have its own Database and its own Scheduler which will access the same Database. Making Database a singleton class isnt the solution here bcz it will imply every -- new Starter() -- will use the same Database. i want every new Starter() to have its own Database – as.tek Feb 04 '12 at 13:43

1 Answers1

1

The most general answer to your question would probably be to simply let Starter keep track of a reference to its own Database. Of course, Scheduler would have to do the same thing:

public class Starter {
    Database db;
    Starter(Database db) {
        this.db = db;
    }
}

/* ... */

Database db1 = new Database();
Starter starter1 = new Starter(db1);
Scheduler scheduler1 = new Scheduler(db1);

Database db2 = new Database();
Starter starter2 = new Starter(db2);
Scheduler scheduler2 = new Scheduler(db2);
Dolda2000
  • 25,216
  • 4
  • 51
  • 92
  • yes this is one way. but my application is a bit more complicated. I am using the mina nio framework for transfer of data over TCP connections. I will be needing the instance of this Database all over my application. So i cant just keep passing it to every class. Moreover i am using it in SessionHandler at client side. i cant pass DB instance as constructor argument there. thnx fr ur suggestion btw! – as.tek Feb 04 '12 at 13:49
  • Unless every Starter has its own thread (in which case you could store the database in a ThreadLocal), I don't see any other way. If you have so many classes which needs to acces the database instance, you probably have a design problem, and a lack of encapsulation. – JB Nizet Feb 04 '12 at 13:56
  • i agree with you. I am sure this is a design problem. but i am neck deep already and on closing date of completion! will just go through about ThreadLocal. meanwhile can u suggest a design pattern for this? – as.tek Feb 04 '12 at 14:13
  • In that case, you may want to look at my answer to [this question](http://stackoverflow.com/questions/9018360/how-to-have-a-shared-context-per-top-level-process-thread-without-using-inherita/9092653#9092653). – Dolda2000 Feb 05 '12 at 03:17