单例模式的装饰器： 在需要单例的类定义上加 @singleton 即可
329558
def singleton(cls):
    
    INSTANCES = {}

    def _singleton(*args, **kwargs):
        if cls not in INSTANCES:
            INSTANCES[cls] = cls(*args, **kwargs)
        return INSTANCES[cls]

    return _singleton
487373
@Override
	public T get() {
		T result = reference.get();

		if (result == null) {
			result = init();
			if (false == reference.compareAndSet(null, result)) {
				// 其它线程已经创建好此对象
				result = reference.get();
			}
		}

		return result;
	}
441031
protected function bindSingletons()
    {
        foreach ($this->singletons as $concrete => $abstracts) {
            if (!is_array($abstracts)) {
                $this->app->bind($abstracts, $concrete, true);
                continue;
            }

            $first = array_shift($abstracts);

            $this->app->bind($first, $concrete, true);

            foreach ($abstracts as $abstract) {
                $this->app->alias($first, $abstract);
            }
        }
    }
606874
public static final VariableNumMap singleton(int varNum, String varName, Variable variable) {
    return new VariableNumMap(new int[] {varNum}, new String[] {varName}, new Variable[] {variable});
  }
889889
public static synchronized final void init ( Properties props ) throws CIFSException {
        if ( INSTANCE != null ) {
            throw new CIFSException("Singleton context is already initialized");
        }
        Properties p = new Properties();
        try {
            String filename = System.getProperty("jcifs.properties");
            if ( filename != null && filename.length() > 1 ) {

                try ( FileInputStream in = new FileInputStream(filename) ) {
                    p.load(in);
                }
            }

        }
        catch ( IOException ioe ) {
            log.error("Failed to load config", ioe); //$NON-NLS-1$
        }
        p.putAll(System.getProperties());
        if ( props != null ) {
            p.putAll(props);
        }
        INSTANCE = new SingletonContext(p);
    }
732183
def singleton_init_by(init_fn=None):
    

    if not init_fn:
        def wrap_init(origin_init):
            return origin_init
    else:
        def wrap_init(origin_init):
            def __init__(self):
                origin_init(self)
                init_fn(self)

            return __init__

    def inner(cls_def: type):
        if not hasattr(cls_def, '__instancecheck__') or isinstance(cls_def.__instancecheck__,
                                                                   (types.BuiltinMethodType, _slot_wrapper)):
            def __instancecheck__(self, instance):
                return instance is self

            cls_def.__instancecheck__ = __instancecheck__

        _origin_init = cls_def.__init__
        cls_def.__init__ = wrap_init(_origin_init)

        return cls_def()

    return inner
936862
public void addInstance(InstanceInfo i) {
        instancesMap.put(i.getId(), i);
        synchronized (instances) {
            instances.remove(i);
            instances.add(i);
            isDirty = true;
        }
    }
1225414
def singleton(name, klass = nil, &block)
      @registry[name.to_sym] = Resolver::Singleton.new(block || klass)
    end
380119
private void instantiateSingletons() {
        if (log.isDebugEnabled()) {
            log.debug("Initializing singletons in " + this);
        }

        Activity activity = context.getDefaultActivity();
        for (BeanRule beanRule : beanRuleRegistry.getIdBasedBeanRules()) {
            instantiateSingleton(beanRule, activity);
        }
        for (Set<BeanRule> beanRuleSet : beanRuleRegistry.getTypeBasedBeanRules()) {
            for (BeanRule beanRule : beanRuleSet) {
                instantiateSingleton(beanRule, activity);
            }
        }
        for (BeanRule beanRule : beanRuleRegistry.getConfigurableBeanRules()) {
            instantiateSingleton(beanRule, activity);
        }
    }
919033
@Override
    public Set<Object> getInstances() {
        Set<Object> set = new HashSet<>();
        ClassLoader cl = Thread.currentThread().getContextClassLoader();

        Consumer<JsonArray> reader = array -> {
            if (array != null && array.size() > 0) {
                for (int i = 0; i < array.size(); i++) {
                    try {
                        Class<?> clazz = cl.loadClass(array.getString(i));
                        set.add(clazz.newInstance());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };

        JsonArray binders = config.getJsonArray(CONFIG_BINDERS, null);
        JsonArray instances = config.getJsonArray(CONFIG_INSTANCES, null);

        reader.accept(binders);
        reader.accept(instances);

        return set;
    }
30347
static Object newInstance(String className, ClassLoader cl,
                                      boolean doFallback)
        throws ConfigurationError
    {
        // assert(className != null);
        try{
            Class providerClass = findProviderClass(className, cl, doFallback);
            Object instance = providerClass.newInstance();
            debugPrintln("created new instance of " + providerClass +
                   " using ClassLoader: " + cl);
            return instance;
        } catch (ClassNotFoundException x) {
            throw new ConfigurationError(
                "Provider " + className + " not found", x);
        } catch (Exception x) {
            throw new ConfigurationError(
                "Provider " + className + " could not be instantiated: " + x,
                x);
        }
    }
246460
public void register(T instance) {
		if(!isContained(instance)) {
			registry.add(instance);
			if(current == null) {
				current = instance;
			}
		}
	}
165040
public function addInstance()
    {
        if (!$this->isXmlHttpRequest()) {
            throw new common_exception_BadRequest('wrong request mode');
        }

        $id = $this->getRequestParameter('id');

        $this->validateInstanceRoot($id);

        $this->signatureValidator->checkSignature(
            $this->getRequestParameter('signature'),
            $id
        );

        $response = [];

        $clazz = $this->getClass($id);
        $label = $this->getClassService()->createUniqueLabel($clazz);

        $instance = $this->getClassService()->createInstance($clazz, $label);

        if(!is_null($instance) && $instance instanceof core_kernel_classes_Resource){
            $response = [
                'label' => $instance->getLabel(),
                'uri'   => $instance->getUri()
            ];
        }

        $this->returnJson($response);
    }
511030
function (key, instance) {
            if (arguments.length == 1) {
                instance = key;
                key = null;
            }

            if (key) {
                this.$singletonInstanceCache[key] = instance;
            } else {
                this.$singletonInstanceCache.push(instance);
            }


        }
1123813
@Override
    protected final T create(Object... args) {
        if (this.creating.get()) {
            throw new IllegalStateException(
                    "Singleton creator has been called again while creation is in progress, this is indicative of a creation loop in a single thread");
        }

        this.creating.set(true);
        try {
            final T instance = this.createSingleton(args);
            this.instance = instance;
            this.created.set(true);
            return instance;
        } finally {
            this.creating.set(false);
        }
    }
160405
def getInstance(cls, *args):
        '''
        
        '''
        if not cls.__singleton:
            cls.__singleton = DriverManager(*args)
        return cls.__singleton
537910
public static synchronized final SingletonContext getInstance () {
        if ( INSTANCE == null ) {
            try {
                log.debug("Initializing singleton context");
                init(null);
            }
            catch ( CIFSException e ) {
                log.error("Failed to create singleton JCIFS context", e);
            }
        }
        return INSTANCE;
    }