typescript: Singleton Pattern

 

npm init -y
npm install -g typescript
tsc --version

npm install -g ts-node
npm install eslint --save-dev

npm install typescript typescript-eslint-parser --save-dev
npm install eslint-plugin-typescript --save-dev

npm install --save-dev ts-loader
npm install --save-dev typescript ts-loader
npm install --save-dev webpack webpack-cli
npm install webpack
npm i -D webpack webpack-cli webpack-dev-server
npm install webpack webpack-cli typescript ts-loader --save-dev

npm i -D @babel/core @babel/preset-env babel-loader core-js

npm i -D less less-loader css-loader style-loader
npm i -D postcss postcss-loader postcss-preset-env

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
 * file: Singletonts.ts
 *  Singleton Pattern 单例是一种创建型设计模式, 让你能够保证一个类只有一个实例, 并提供一个访问该实例的全局节点。
 * The Singleton class defines the `getInstance` method that lets clients access
 * the unique singleton instance.
 */
class Singleton {
    private static instance: Singleton;
 
    /**
     * The Singleton's constructor should always be private to prevent direct
     * construction calls with the `new` operator.
     */
    private constructor() { }
 
    /**
     * The static method that controls the access to the singleton instance.
     *
     * This implementation let you subclass the Singleton class while keeping
     * just one instance of each subclass around.
     */
    public static getInstance(): Singleton {
        if (!Singleton.instance) {
            Singleton.instance = new Singleton();
        }
 
        return Singleton.instance;
    }
 
    /**
     * Finally, any singleton should define some business logic, which can be
     * executed on its instance.
     */
    public someBusinessLogic() {
        // ...
    }
}
 
/**
 * The client code.
 */
function clientCodeDu() {
    const s1 = Singleton.getInstance();
    const s2 = Singleton.getInstance();
    let stdu="";
    if (s1 == s2) {
        console.log('Singleton works, both variables contain the same instance.');
        stdu="Singleton works";
    } else {
        console.log('Singleton failed, variables contain different instances.');
        stdu="Singleton failed";
    }
    return stdu;
}
 
 
let ssig="gevindu";
let strsig=clientCodeDu();
let strsig1="geovindu";
let messagesig: string = 'Hello World,This is a typescript!,涂聚文 Geovin Du Web';
document.body.innerHTML = messagesig+","+strsig+","+strsig1+",TypeScript 单例模式"

 

調用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <head><title>TypeScript:单例模式</title>
      <meta name="Description" content="geovindu,涂聚文,Geovin Du"/>
<meta name="Keywords" content="geovindu,涂聚文,Geovin Du"/>
<meta name="author" content="geovindu,涂聚文,Geovin Du"/> 
    </head>
    <body>
        <script src="dist/Singletonts.js"></script>
    </body>
</html>

  

輸出:

 

npm install --save-dev ts-loader
npm install --save-dev webpack webpack-cli
npm install webpack

https://www.typescriptlang.org/docs/handbook/2/modules.html
https://www.typescriptlang.org/tsconfig#module

 

  

 

npm init -y

npm install --save-dev ts-loader
npm install --save-dev typescript ts-loader
npm install --save-dev webpack webpack-cli
npm install webpack
npm i -D webpack webpack-cli webpack-dev-server
npm install webpack webpack-cli typescript ts-loader --save-dev

npm i -D @babel/core @babel/preset-env babel-loader core-js

npm i -D less less-loader css-loader style-loader
npm i -D postcss postcss-loader postcss-preset-env

 

posted @   ®Geovin Du Dream Park™  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2022-10-06 CSharp: Composite Pattern in donet core 3
2022-10-06 CSharp: Flyweight Pattern in donet core 3
2022-10-06 Python: Factory Method Pattern
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示