zoukankan      html  css  js  c++  java
  • 对于初学者有效的JavaScript设计模式(2)

    二、 构造模式 

    利用prototype 来构造自己的函数。

     1 function Car(model,year,miles){  
     2     this.model = model;  
     3     this.year = year;  
     4     this.miles = miles;  
     5 }  
     6 Car.prototype.toString = function(){  
     7     return this.model + " has done " + this.miles + " miles";  
     8 };  
     9 var civic = new Car("Honda Civic",2009,20000);  
    10 var mondeo = new Car("Ford Mondeo",2010,5000);  
    11   
    12 console.log(civic.toString());  

    结果: Honda Civic has done 20000 miles 



    一个简单的toString()方法将在所有的Car 对象之间共用。 

    善用构造函数,用来简单的区分它们与其他的函数 

    三、 单体模式 


    在传统的软件工程中,通过创建有一个方法的类来创建类的实例,实现单体模式。使用JavaScript,单体作为一个命名空间提供与全局命名空间隔离的实现代码,从而提供一个功能单一的访问点。 
    在JS中最简单的一种形式就是——一个对象直接量与它相关的方法和属性。如下: 

     

    1 var mySingeton = {  
    2     prototype1: "something",  
    3     prototype2: "something else",  
    4     method1: function(){  
    5         console.log('hello world');  
    6     }  
    7 }  

     

    如果你想进一步的扩展,你可以通过封闭变量和函数增加自己的私有成员和方法。只暴露那些你想公有的部分。例如: 

     1 var mySingleton = function(){  
     2     /** 
     3      * here are my private variables and method 
     4      * */  
     5     var privateVariable = "something private";  
     6     function showPrivate(){  
     7         console.log(privateVariable);  
     8     }  
     9     /** 
    10      * public variables and methods (which can access private variables and methods) 
    11      * */  
    12     return {  
    13         publicMethod: function(){  
    14             showPrivate();  
    15         },  
    16         publicVar:"the public can see this!"  
    17     }  
    18 }  
    19 var single = mySingleton();  
    20 single.publicMethod();  
    21 console.log(single.publicVar);  

    结果: 

    something private  
    the public can see this! 

    上面的代码不错了。但是让我们进一步来考虑,如果你只想在当它需要的时候才实例化它这种情况。为了节约资源,你可以把实例化的代码放在另外一个构造函数中,如下: 

     1 var Singleton = (function(){  
     2     var instantiated;  
     3     function init(){  
     4     /*singleton code here*/  
     5         return{  
     6         publicMethod: function(){  
     7             console.log('hello world')          
     8         },  
     9             publicProperty:'test'  
    10     }  
    11     }  
    12     return {  
    13         getInstance: function(){  
    14         if(!instantiated){  
    15             instantiated = init();  
    16         }  
    17         return instantiated;  
    18     }  
    19   
    20     }  
    21 })()  
    22 /*calling public methods is then as easy as:*/  
    23 Singleton.getInstance().publicMethod();  

    结果: 

    hello world


    在实践中,单体模式有什么用处吗?当需要一个对象来协调整个系统的时候,单体是很有用的。最后一个关于单体的例子: 

     1 var SingletonTester = (function(){  
     2     function Singleton(args){  
     3         var args = args || {};  
     4         this.name = 'SingletonTester';  
     5         this.pointX = args.pointX || 6;  
     6         this.pointY = args.pointY || 10;  
     7     }  
     8     var instance;  
     9     var _static = {  
    10         name: 'SingletonTestet',  
    11             getInstance: function(args){  
    12             if(instance === undefined){  
    13                 instance = new Singleton(args);  
    14             }  
    15             return instance;  
    16         }  
    17     };  
    18     return _static;  
    19 })();  
    20 var singletonTest = SingletonTester.getInstance({pointX:5});  
    21 console.log(singletonTest.pointX);  

    结果:5

  • 相关阅读:
    JDBC的异常
    JDBC的事务
    JDBC的数据类型
    JDBC的结果集
    JDBC操作MySQL出现:This result set must come from a statement that was created with a result set type of ResultSet.CONCUR_UPDATABLE, ...的问题解决
    JDBC的Statement对象
    JDBC连接数据库
    JDBC驱动类型
    JDBC实例代码
    java与javax的区别分析(转)
  • 原文地址:https://www.cnblogs.com/lilyimage/p/2443944.html
Copyright © 2011-2022 走看看