Implementation of Singleton mode under JavaScript (JScript)

xiaoxiao2021-04-02  215

Synual definition: Singleton mode The main role is to ensure that a class class Class exists in a Java application.

In many operations, such a single-threaded operation is required such as establishing a directory database connection.

Also, Singleton can be statically customized; so that multiple single classes can serve as a status warehouse as a status repository, for example, you want to discus the post counter, each time you browse a count, single class Can you keep this count, and you can use Synchronize's security to add 1. If you want to save this number permanently to the database, you can easily do it without modifying a single-state interface.

In addition, Singleton can also be unstable. The function of providing tools is provided, and Singleton mode provides us with this implementation. The benefits of using Singlet are also saving memory because it limits the number of instances, which is conducive to the Garbage Collection. We often see that in the factory model, the Class Loader is also implemented in the Singleton mode because the collateral is actually a resource.

The above is the basic theory, the following is the code. I found it when I study JSWIKI and organized.

/ ** /

/ ** * CREATE A Constructor with singleleton pattern, maintains only one single installation * @Param _Constructor * /

FUNCTION

$ Singleton (_Constructor)

...

{Var instance = null; return function () ... {return (instance! = Null)? Instance: (Instance = new _constructor ());}}

Here is an application example (Note that this is the server-side JavaScript code): var $ fso = $ singleton ("scripting.filesystemObject");});

转载请注明原文地址:https://www.9cbs.com/read-131350.html

New Post(0)