Tito
Tito

Reputation: 9044

can singleton class in java act as a cache mechanism?

My java applciation is a console application.I am reading a lot of read only data and keeping it under a List called flatFileList.

 List flatFileList = readLotofDataFromFile(fileName);

Now this flatFileList is used in almost all parts of the code. I do not want to read the datafile again. So I created a singleton class , and a getInstance()

I use this getInstance() method anywhere I need to get the flatFileList object.My question is this,can this singleton pattern be called as a cache mechanism ? because I am not re reading the data again from the flatfile.

Upvotes: 2

Views: 1782

Answers (2)

mort
mort

Reputation: 13588

Sure, that's exactly what a cache is for: parts of (or all) data is read into a usually non-persistent memory close to the process that requires the data to improve access speed.

Upvotes: 3

Arseny
Arseny

Reputation: 7351

Other possible solution is to use Flyweight pattern which also may be used as a cache mechanism.

Upvotes: 3

Related Questions