Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Singleton? #64

Closed
lviggiano opened this issue Dec 6, 2013 · 8 comments
Closed

Singleton? #64

lviggiano opened this issue Dec 6, 2013 · 8 comments

Comments

@lviggiano
Copy link
Collaborator

Provide a way to define a singleton Config objects instead of creating new instances at every create() request.

3 possible ways:

  1. Provide a @Singleton annotation
  2. Provide a ConfigFactory.getInstance() method
  3. Nothing. It's a problem of IoC and as that should be solved.

Also provide an example (on a new examples mvn module) to show how to instantiate singleton objects with Spring. See also http://forum.spring.io/forum/spring-projects/container/48256-using-factory-beans-with-factory-method-parameters

@gembin
Copy link

gembin commented Jan 22, 2014

@Singleton +1
option 3 -1, not necessary to use a IoC framework always, i.e. foundation libraries

@lviggiano
Copy link
Collaborator Author

IoC can be done without any framework.

@gembin
Copy link

gembin commented Jan 28, 2014

what i mean is that there are many cases without using IoC. : - )

@lviggiano
Copy link
Collaborator Author

Hi @gembin

I come out with an implementation of a sort of singleton, that actually is a cache of Config classes.

Before you used ConfigFactory:

    MyConfig instance = ConfigFactory.create(MyConfig.class);

The same way you can use the new ConfigCache:

    MyConfig instance = ConfigCache.getOrCreate(MyConfig.class);

The difference is that, when using ConfigFactory a new instance of the Config object is created every time, while using the ConfigCache, instances are returned from an internal cache.

    MyConfig firstFromFactory = ConfigFactory.create(MyConfig.class);
    MyConfig secondFromFactory = ConfigFactory.create(MyConfig.class); 
    // firstFromFactory not same as secondFromFactory

    MyConfig firstFromCache = ConfigCache.getOrCreate(MyConfig.class);
    MyConfig secondFromCache = ConfigCache.getOrCreate(MyConfig.class);
    // firstFromCache same as secondFromCache  

You can assign an "id" to an instance,

    MyConfig firstFromCache = ConfigCache.getOrCreate("foo", MyConfig.class);
    MyConfig secondFromCache = ConfigCache.getOrCreate("foo", MyConfig.class);
    MyConfig thirdFromCache = ConfigCache.getOrCreate("bar", MyConfig.class);
    // firstFromCache same as secondFromCache  
    // thirdFromCache not same as secodFromCache or firstFromCache

The ConfigCache is designed to be thread safe.

@lviggiano
Copy link
Collaborator Author

TODO-list before closing this issue:

  • documentation

@lviggiano lviggiano added resolved and removed maybe labels Apr 8, 2014
@gembin
Copy link

gembin commented Apr 28, 2014

Great, thx!

@lviggiano
Copy link
Collaborator Author

Thanks to you for your help. I'll document this and the other improvements and cut a release asap, so you won't need to build the jar from a snapshot.

@lviggiano
Copy link
Collaborator Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants