Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Meir Yan
Ranch Hand
Posts: 599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 547
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 599
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic