How can I use Singleton in a shared library, in multiple modules, without conflict?

I have a singleton object that implements some basic functions in a shared library. This is much more complicated than this, but to document the problem, below is the Singleton implementation with removing all internal properties / objects:

package mystuff.lib.common

public class CommonSingleton {


    public final static CommonSingleton INSTANCE = new CommonSingleton();

    private Cache<String, Object> cache = null;

    private CommonSingleton() {
        // Exists only to defeat instantiation.
        Initialize();
    }

    private void Initialize() {
        cache = null;
    }

    public Cache<String, Object> getCache() {
        if (cache == null) {
            cache = new Cache<String, Object>();
        }
        return cache;
    }

    public void Reload() {
        Initialize();
    }

}

I have included this library in two SEPARATE projects. The library is not installed on the computer where the code is running. Shared library content is included in every JAR when artifacts are created in IntelliJ IDEA.

( JAR) ( ) , JVM. , JAR , JAR . , , , , .

/ β„–1: TestPluginOne.JAR

package mystuff.plugins.TestPluginOne;

import mystuff.lib.common.CommonSingleton;
import mystuff.lib.common.Objects.Cache;

public class TestClassOne {

    public TestClassOne() {
    }

    public Boolean SaveItemInCache(String key, String content) {
        return CommonSingleton.INSTANCE.getCache().add(key, content, 30);
    }

    public void Reload() {
        CommonSingleton.INSTANCE.Reload();
    }

}

/ β„–2: TestPluginTwo.JAR

package mystuff.plugins.TestPluginTwo;

import mystuff.lib.common.CommonSingleton;
import mystuff.lib.common.Objects.Cache;

public class TestClassTwo {

    public TestClassTwo() {
    }

    public String GetItemFromCache(String key) {
        return CommonSingleton.INSTANCE.getCache().get(key);
    }

    public void Reload() {
        CommonSingleton.INSTANCE.Reload();
    }

}

, , , ( JAR , ), . Cache, , - , , , . / -, , //, Singleton.

. .NET() DLL, DLL .

Singleton, , Singleton?

!

+3
1

/* . */

, , , . , : , DLL, .

( ), , - , .

, , .

+1

All Articles