zoukankan      html  css  js  c++  java
  • 单例模式-1.单利模式的简单实现

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charst="utf-8">
    <title>单利模式的实现</title>
    </head>
    <body>
    
    </body>
    <script type="text/javascript">
        //单例模式的核心是确保只有一个实例,并提供全局访问
        var SingletonTester=(function () {
            function Singleton (args) {
                //设置args变量为接受的参数或者为空(没有提供的话);
                var args=args || {};
                //设置name参数
                this.name='SingletonTester';
                //设置pointX的值
                this.pointX=args.pointX || 6;//从接收的参数里面获得或者为默认值
                //设置pointY值
                this.pointY=args.pointY || 10;
            }
            //实例容器
            var instance;
            var _static={
                name:'SingletonTester',
                //获取实例的方法
                //返回Singleton的实例
                getInstance:function (args){
                    if (instance === undefined) {
                        instance = new Singleton(args);
                    }
                    return instance;
                }
            };
            return _static;
        })()
        var singleTest=SingletonTester.getInstance({name:'hanhui',pointY:4,pointX:8});
        console.log(singleTest.pointY);
    </script>
    </html>
  • 相关阅读:
    Linux-Oracle 安装配置步骤
    lombok 安装
    request (请求对象)
    response (响应对象)
    ServletContext (上下文对象)
    JavaWeb数据库配置
    HttpServlet
    博客园代码字体大小
    博客园背景美化
    用PHP实现反向代理服务器
  • 原文地址:https://www.cnblogs.com/hanhui66/p/7110716.html
Copyright © 2011-2022 走看看