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
Owner

@lviggiano lviggiano commented Dec 6, 2013

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 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
Owner Author

@lviggiano lviggiano commented Jan 23, 2014

IoC can be done without any framework.

@gembin
Copy link

@gembin gembin commented Jan 28, 2014

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

lviggiano added a commit that referenced this issue Apr 8, 2014
@lviggiano
Copy link
Owner Author

@lviggiano lviggiano commented Apr 8, 2014

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
Owner Author

@lviggiano lviggiano commented Apr 8, 2014

TODO-list before closing this issue:

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

@gembin gembin commented Apr 28, 2014

Great, thx!

@lviggiano
Copy link
Owner Author

@lviggiano lviggiano commented Apr 28, 2014

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
Owner Author

@lviggiano lviggiano commented May 3, 2014

@lviggiano lviggiano closed this May 3, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.