Singleton? #64

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

Comments

Projects
None yet
2 participants
@lviggiano
Owner

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

This comment has been minimized.

Show comment
Hide comment
@gembin

gembin Jan 22, 2014

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

gembin commented Jan 22, 2014

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

@lviggiano

This comment has been minimized.

Show comment
Hide comment
@lviggiano

lviggiano Jan 23, 2014

Owner

IoC can be done without any framework.

Owner

lviggiano commented Jan 23, 2014

IoC can be done without any framework.

@gembin

This comment has been minimized.

Show comment
Hide comment
@gembin

gembin Jan 28, 2014

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

gembin commented Jan 28, 2014

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

@lviggiano

This comment has been minimized.

Show comment
Hide comment
@lviggiano

lviggiano Apr 8, 2014

Owner

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.

Owner

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

This comment has been minimized.

Show comment
Hide comment
@lviggiano

lviggiano Apr 8, 2014

Owner

TODO-list before closing this issue:

  • documentation
Owner

lviggiano commented Apr 8, 2014

TODO-list before closing this issue:

  • documentation

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

@gembin

This comment has been minimized.

Show comment
Hide comment
@gembin

gembin Apr 28, 2014

Great, thx!

gembin commented Apr 28, 2014

Great, thx!

@lviggiano

This comment has been minimized.

Show comment
Hide comment
@lviggiano

lviggiano Apr 28, 2014

Owner

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.

Owner

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

This comment has been minimized.

Show comment
Hide comment

@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