aspose file tools*
The moose likes Beginning Java and the fly likes Is there known pattern or this (object holds data across the application) ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is there known pattern or this (object holds data across the application) ?" Watch "Is there known pattern or this (object holds data across the application) ?" New topic
Author

Is there known pattern or this (object holds data across the application) ?

Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
Hello
i like to create single tone object that will hold data across the application .
for example :
i have api function invoked this api function returns me some values this values and some
addition data i want to fill this singletone object this object will share his data across the application scope . i could
query the data from in it and it will hold the data relevant only to the last api function that has been invoked ;
is there any well known pattern or way to do this?
thanks
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
hi

A singleton would do what you want. Before the API call returns you store the information in the singleton.
But why do you want to this ? What if several Threads access the API ? What if this information in the singleton is overwritten before it is read ?
Also it is pretty error prone to add these calls in the API. If you forget it in one method (e.g. if method is added later by another developer) ...

Perhaps you could do something with AOP (create a proxy around your API) ?

And then I have to say my mantra: do not use singleton pattern unless there is a really good reason for it.

pascal
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
hi
thanks for the fast reply
well i need it simple to query the information returned from the api function from different section in the application and not to pass
the result throw functions . allso the class that execute the api is closed class and can't really extend it only manipulate the data that returned
so what i was thinking is to executed the internal api call and then right after to fill some kind of global object with the data , and i wlll query it later .
the thread issue also need to be handled .
thanks
David Nemeskey
Ranch Hand

Joined: Nov 08, 2006
Posts: 52
I have a similar question. I would like to have a logger in my application, and I want to use it throughout the whole program, and only one. How do I do this "cleanly"?

I now have a utility class, with a static method getLogger(), which returns the application-wide logger.

What do you think about this solution? Do you have a better pattern for using loggers?

However, the same question can be asked about configuration settings, say, read from a file. Various classes may need the settings. But how do I make it available to them? Create a reference in each class, pass it in the constructor / a setter method to the configuration object? I think it is better to have a static class here as well, but I would really like to know what you think

Thanks,
David
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by David Nemeskey:

However, the same question can be asked about configuration settings, say, read from a file. Various classes may need the settings. But how do I make it available to them? Create a reference in each class, pass it in the constructor / a setter method to the configuration object? I think it is better to have a static class here as well, but I would really like to know what you think


I would prefer the former approach, because it makes the code easier to unit test, and more reusable.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there known pattern or this (object holds data across the application) ?