Search code examples
javadesign-patternsnaming-conventionsfactory-patternthread-local

Design pattern name: Is it a factory?


The following class shows something similar to a real use case. It returns always the same instance for the same thread.

public class LookingForName {

    private static final ThreadLocal<Something> threadLocal = 
        new ThreadLocal<Something>(){
            @Override
            protected Something initialValue() {
                return getSomethingSpecial(); // not relevant
            }
        };

    /**
     * @return always the same instance of "Something" for the current thread.
     */
    public static Something getInstance() {
        return threadLocal.get();
    }

}

How would you call it? Is it a "factory"? A "value holder"? "ThreadLocalStore"?


Solution

  • Some simply called it the ThreadLocal Pattern. Another known name is Thread-local Storage (TLS).