🤓
blog-article
  • Introduction
  • 面试题
    • 1. 输入url发生了什么
  • Personal Works
    • 1. 简单的Web图床
  • CSS动画笔记
    • 1.transition 属性
    • 2.transition 实例
  • css 笔记
    • 1. box-shadow 阴影
    • 2. columns 设置列宽和列数
    • 3. 伪元素after与before
  • html 笔记
    • 1. 不常用的h5标签杂记
  • webpack4
    • 1. 文档学习记录
    • 2. 加载CSS
    • 3. 加载less
    • 4. 加载sass
    • 5. 根据浏览器自动添加css前缀
    • 6. 图片文件处理
    • 7. 字体文件处理
    • 8. 引入jQuery
    • 9. 打包HTML文件
    • 10. 分离css插件
    • 11. babel简单使用
    • 12. 清理旧打包文件插件
    • 13. 拷贝静态资源
  • javascript
    • 1. js获取某年某月的天数
  • npm
    • 2. JSON.stringify()完整用法
    • 1. 入门篇
    • 2. 进阶篇
  • Node.js
    • 1. 使用koa-body中间件后ctx.request.body内容为空
    • 2. uni-app使用Node+Koa2接收上传的文件
  • SQL
    • 1. 查询
    • 2. 插入
    • 3. 修改
    • 4. 删除
    • 5. 期末复习
  • Git
    • 1. git add命令后出现another git process...问题
    • 2. 将文件从暂存区移除
  • vue-cli
    • 1. vue-cli(脚手架)引入vue.config.js文件后热更新失效,每次保存都会刷新页面
  • uni-app
    • 1. uni-app中不使用scroll-view组件,监听页面滑直底部事件
  • 设计模式
    • 创建型模式
    • 单例模式
    • 工厂模式
    • 抽象工厂模式
  • Spring boot
    • 1. 使用自定义参数注解获取 token 中User数据
  • IDEA
    • 1. 设置maven项目的默认配置
  • Linux
    • 1.常用命令
    • 2.常用基础命令2
    • 3.linux目录介绍
    • 4. vi编辑器使用
    • 5.用户管理
    • 6. 文件属性与权限操作
    • 7. 文件归档与解压缩
    • 8. gcc的基本用法
    • 9. gdb调试器的基本用法
    • 10. Makefile基本用法
  • shell
    • shell与crontab定时器的结合
    • 1. shell 变量与常见符号
    • 2. shell中的四则运算符
    • 3. 条件判断
    • 4. 输入输出
    • 5. 输出彩色的内容
    • 6. 流程控制
    • 7. 函数
  • Docker
  • 嵌入式系统给结构及原理
  • 学校课程笔记
    • 1. java EE 复习
    • 2. 马原复习论述题
    • 3. 嵌入式软件技术复习
    • 4. 嵌入式操作系统复习
    • 5. 马原选择题
    • 6. 马原辨析题
    • 7. 马原材料题
    • 8. 计网学习笔记
    • 9. 计网复习
      • 1. 第一章
Powered by GitBook
On this page
  • 箭头函数写法
  • function写法
  • class写法
  • 实例1
  • 实例2

Was this helpful?

  1. 设计模式

单例模式

箭头函数写法

const singleton = function () { };
singleton.getInstance = (() => {
    let instance = null;
    return () => {
        if (!instance) {
            instance = new singleton();
        }
        return instance;
    }
})()
const s1 = singleton.getInstance();
const s2 = singleton.getInstance();

console.log(s1 === s2);//true

function写法

singleton.getInstance=(function(){
    let instance = null;
    return function(){
        if(!instance){
            instance = new singleton(); 
        }
        return instance;
    }
})();

const s1 = singleton.getInstance();
const s2 = singleton.getInstance();

console.log(s1===s2);

class写法

class Singleton{
    static getInstance(){
        if(!Singleton.instance){
            Singleton.instance = new Singleton();
        }
        return Singleton.instance;
    }
}

let s1 = Singleton.getInstance();
let s2 = Singleton.getInstance();
console.log(s1 === s2);//true

实例1

实现一个 Storage

①Storage--class实现

class Storage {
    static getInstance() {
        if (!Storage.instance) {

            Storage.instance = new Storage();
        }

        return Storage.instance;
    }

    setItem(key, value) {
        return localStorage.setItem(key, value);
    }

    getItem(key) {
        return localStorage.getItem(key);
    }
}

//调用
let s1 = Storage.getInstance();
s1.setItem("a", 666);
let s2 = Storage.getInstance();
console.log(s2.getItem("a"));

②闭包实现

// 基础的StorageBase类,把getItem和setItem方法放在它的原型链上
function storageBase() {
    storageBase.prototype.getItem = function(key) {
        return localStorage.getItem(key);
    }

    storageBase.prototype.setItem = function(key, value) {
        return localStorage.setItem(key, value);
    }
}

const Storage = (function() {
    let instance = null;
    return function() {
        if (!instance) {
            instance = new storageBase();
        }
        return instance;
    }
})()

let s3 = new Storage();
console.log(s3.getItem('a'));

实例2

实现一个全局唯一的Modal弹框

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #my-modal {
            height: 200px;
            width: 200px;
            line-height: 200px;
            position: fixed;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            border: 1px solid black;
            text-align: center;
        }
    </style>
</head>

<body>
    <button id="open" type="button">打开</button>
    <button id="close" type="button">关闭</button>
</body>
<script src="./modal.js"></script>
<script>
    document.getElementById('open').addEventListener('click', function() {
        let modal = Modal.getInstance();
        modal.style.display = 'block';
    })

    document.getElementById('close').addEventListener('click', function() {
        let modal = Modal.getInstance();
        modal.style.display = 'none';
    })
</script>
</html>

modal.js

class Modal {
    static getInstance() {
        if (!Modal.instance) {
            let modal = document.createElement('div');
            modal.innerHTML = '全局弹窗';
            Modal.instance = modal;
            modal.id = 'my-modal';
            modal.style.display = 'none';
            document.body.appendChild(modal);
        }
        return Modal.instance;
    }
}
Previous创建型模式Next工厂模式

Last updated 5 years ago

Was this helpful?