设计模式--单例模式

单例模式

单例模式,顾名思义,只有一个实例,可以理解为多次创建,只返回一个实例。

静态属性实现单例模式

<html>
    <head>
        <meta charset="utf-8">
        <style>
            #box{
                width: 100px;
                height: 100px;
                border: 1px solid red;
            }
        </style>
    </head>
    <body>
        <input type="button" id="txt" value="点击">
        <script>
            function Person(){
                if(!Person.instance){
                   Person.instance = {
                       name : 'ZhangSan',
                       age : 14,
                       show : function(){
                           var box = document.createElement('div');
                           box.innerHTML =  this.name + ' is ' + this.age + ' years old'
                           box.id = 'box';
                           document.body.appendChild(box)
                       }
                   }
                    return Person.instance;
                }
            }
            txt.onclick = function(){
                var person1 = new Person();
                if(person1.show){
                    person1.show();
                }
            }
        </script>
    </body>
</html>

不过静态属性存在在外界被修改的情况

即时函数实现单例

<html>
    <head>
        <meta charset="utf-8">
        <style>
            #box{
                width: 100px;
                height: 100px;
                border: 1px solid red;
            }
        </style>
    </head>
    <body>
        <input type="button" id="txt" value="点击">
        <script>
            (function (w){
                var instance = null;
                function Person(){
                    if(instance){
                        console.log('before',instance)
                       instance.show = null;
                       console.log('after',instance)
                       return  instance;
                    }
                    instance = this;
                    this.name = 'ZhangSan';
                    this.age = 14;
                    this.show = function(){
                        var box = document.createElement('div');
                        box.innerHTML =  this.name + ' is ' + this.age + ' years old'
                        box.id = 'box';
                        document.body.appendChild(box)
                    }
                }
                w.Person = Person;
            })(window)
            txt.onclick = function(){
                var person1 = new Person();
                if(person1.show){
                    person1.show();
                }
            }
        </script>
    </body>
</html>

这个例子运用匿名函数自运行来保存有局部变量,这样外部不可以获取这个变量,只可以获取暴漏给window的构造函数,避免了被修改。
这里使用console.log()输出了一下,判断出确实是一个实例。
在这里插入图片描述

惰性函数实现单例模式

暂时没有理解透彻,所以留待更新。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值