The schema definition

Singleton pattern: the creation pattern that ensures that a class has only one instance and provides global access to a method.

Pattern structure diagram

Code implementation

  • Lazy mode
1. In lazy mode, singletons are created as needed. If they have already been created, calling the fetch interface again will not recreate the new object, but return the previously created object. 2. If a singleton is used less times and its creation consumes more resources, it is necessary to implement the creation of singleton on demandCopy the code
namespace SingletonPattern {
    export class Singleton {
        private static singleton: Singleton;

        private constructor () {

        }

        public static getInstance () {
            if (Singleton.singleton === null) {
                Singleton.singleton = new Singleton();
            }
            returnSingleton.singleton; }}}Copy the code

Test code:

/// <reference path="Singleton.ts" />
var s1 = SingletonPattern.Singleton.getInstance();
var s2 = SingletonPattern.Singleton.getInstance();
if (s1 === s2) {
    console.log('Two instances are equal');
}
else {
    console.log('Two instances are not equal');
}
Copy the code
  • The hungry mode
1. In hungry mode, instances are created at class loading time and exist throughout the program cycle. 2. The advantage of this method is that the instance is only created once when the class is loaded, which is suitable for the case that the singleton occupies relatively small memory and will be used during initialization. Its disadvantages are also obvious. The singleton will be created even if it is not used, and it will be created after the class is loaded, wasting memoryCopy the code
namespace SingletonPattern {
    export class Singleton {
        private static singleton: Singleton = new Singleton;

        private constructor () {

        }

        public static getInstance () {
            returnSingleton.singleton; }}}Copy the code

The test code remains the same.

Here’s a brief explanation of namespaces:

The TS namespace can wrap code around it, passing throughexportKeyword to expose the accessed object. Pass is required outside of the namespace"Fully qualified name"To access exposed objects. The reference annotation references the namespace, which can be accessed through fully qualified names. The same namespace can be declared in different files.Copy the code

Pattern analysis

The purpose of the singleton pattern is to ensure that only one instance of a class occurs and to provide a globally accessible method.

Composition: - Private constructor, which ensures that users cannot create instances through new - static private member variable singleton stores unique instances - static public method getInstance(): static instance existence and instantiationCopy the code

advantages

  • Provides controlled access to a unique instance

disadvantages

  • Because there is no abstraction layer in the singleton pattern, it is not easy to extend
  • Singleton classes are too heavy on responsibilities and violate the “single responsibility principle” to some extent. Because the singleton class acts both as a factory, providing factory methods, and as a product, containing some business methods (content properties of the singleton)

The resources

  • Design Patterns in typescript.