美文网首页
设计模式---单例模式

设计模式---单例模式

作者: noyanse | 来源:发表于2019-07-11 22:20 被阅读0次

设计模式:

6ebf5555367caac534f248f63c759e7.png

单例模式

类似于单机游戏存档,下次进入游戏的时候,读取进度

  1. 特定类,访问时,始终返回同一个实例
  2. 单例
  3. 访问单例的方法, 或者直接 new 一个实例

应用:
当一个类的实例化过程消耗的资源过多,可用单例避免性能浪费
当项目中需要一个公共的状态,需要使用单例保证访问一致性

优点

1.在内存中只占一个实例,节省内存开支和实例化消耗,特别是开销较大的类 ,如数据库连接,不断销毁和重新实例化很浪费资源

  1. 可以解决对资源的多重占用,比如读写文件,只有一个实例,可避免对一个文件进行同时操作
  2. 可减少GC 浏览器中会卡顿较少,CPU资源占用更少

缺点

  1. 不容易扩展
  2. 与单一职责原则冲突
/**
 * @description 单例模式 IIFE方式加闭包 本质是通过函数作用域的方式来隐藏内部作用域的变量
 */
const Singleton = (function() {
    let _instance = null // 存储单例

    const Singleton = function() {
        if (_instance) return _instance // 判断是否已经有单例
        _instance = this
        this.init() // 初始化
        return _instance
    }

    Singleton.prototype.init = function() {
        this.foo = 'Singeton Pattern'
    }

    Singleton.getInstance = function() {
        if (_instance) return _instance
        _instance = new Singleton()
        return _instance
    }
    
    return Singleton
})()

const vistor1 = new Singleton()
const vistor2 = new Singleton()
const vistor3 = new Singleton.getInstance()
console.log(vistor1 === vistor3) // true

----------------------------------------------------------------------------------------------------------------------------------

/**
 * @description 单例模式 ES6的块级作用域 目的是为了 _instance隐藏内部变量
 */
let getInstance

{
    let _instance = null

    const Singleton = function() {
        if (_instance) return _instance // 判断是否已经有单例
        _instance = this
        this.init() // 初始化
        return _instance
    }

    Singleton.prototype.init = function() {
        this.foo = 'Singeton Pattern'
    }

    getInstance = function() {
        if (_instance) return _instance
        _instance = new Singleton()
        return _instance
    }
}

const vistor1 = getInstance()
const vistor2 = getInstance()

----------------------------------------------------------------------------------------------------------------------------------

/**
 * @description 单例 抽离 创建 和 功能逻辑
 */
class FuncClass {
    constructor(bar) {
        this.bar = bar
        this.init()
    }
    init() {
        this.foo = 'Single Pattern'
    }
}

const Singleton = (function() {
    let _instance = null

    const ProxySingleton = function(bar) {
        if (_instance) return _instance
        _instance = new FuncClass(bar)
        return _instance
    }
    ProxySingleton.getInstance = function(bar) {
        if (_instance) return _instance
        _instance = new Singleton(bar)
        return _instance
    }
    return ProxySingleton
})()

const vistor1 = new Singleton('单例1')
const vistor2 = new Singleton('单例2')
const vistor3 = Singleton.getInstance()

----------------------------------------------------------------------------------------------------------------------------------

/**
 * @description 单例模式 用ES6的proxy拦截 new
 */
class Person {
    constructor(name, age) {
        this.name = name
        this.age = age
    }
}

function Singleton(FuncClass) {
    let _instance
    return new Proxy(FuncClass, {
        construct(target, args) {
            return _instance || (_instance = Reflect.construct(FuncClass, args))
        }
    })
}
const PersonInstance = Singleton(Person)

const person1 = new PersonInstance('张三', 20)
const person2 = new PersonInstance('李四', 25)

----------------------------------------------------------------------------------------------------------------------------------

/**
 * @description 单例模式的应用 element-ui 的 loading
 */
import Vue from 'vue'
import loadingVue from './loading.vue // 自己写的Loading组件
const LoadingConstructor = Vue.extend(loadingVue)

let fullscreenLoading

const Loading = (options = {}) => {
    if (options.fullscreen && fullscreenLoading) { // 如果之前创建国,直接返回
        return fullscreenLoading
    }

    let _instance = new LoadingConstructor({
        el: document.createElement('div'),
        data: options
    })

    if (options.fullscreen) {
        fullscreenLoading = _instance
    }
    
    return _instance
}

export default Loading

相关文章

  • 单例模式Java篇

    单例设计模式- 饿汉式 单例设计模式 - 懒汉式 单例设计模式 - 懒汉式 - 多线程并发 单例设计模式 - 懒汉...

  • python中OOP的单例

    目录 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计模式 是 前人...

  • 单例

    目标 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计模式 是 前人...

  • 设计模式 - 单例模式

    设计模式 - 单例模式 什么是单例模式 单例模式属于创建型模式,是设计模式中比较简单的模式。在单例模式中,单一的类...

  • 设计模式

    常用的设计模式有,单例设计模式、观察者设计模式、工厂设计模式、装饰设计模式、代理设计模式,模板设计模式等等。 单例...

  • 2018-04-08php实战设计模式

    一、单例模式 单例模式是最经典的设计模式之一,到底什么是单例?单例模式适用场景是什么?单例模式如何设计?php中单...

  • python 单例

    仅用学习参考 目标 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计...

  • 基础设计模式:单例模式+工厂模式+注册树模式

    基础设计模式:单例模式+工厂模式+注册树模式 单例模式: 通过提供自身共享实例的访问,单例设计模式用于限制特定对象...

  • 单例模式

    JAVA设计模式之单例模式 十种常用的设计模式 概念: java中单例模式是一种常见的设计模式,单例模式的写法...

  • 设计模式之单例模式

    单例设计模式全解析 在学习设计模式时,单例设计模式应该是学习的第一个设计模式,单例设计模式也是“公认”最简单的设计...

网友评论

      本文标题:设计模式---单例模式

      本文链接:https://www.haomeiwen.com/subject/gzljkctx.html