หน้าเว็บ

วันพฤหัสบดีที่ 17 มกราคม พ.ศ. 2556

javascript encapsulation


/**
* author Redcrow
* http://na5cent.blogspot.com
* 18/01/2012
*/

//annonymous function return value
var Robot = (function(){

    //-------------------- static scope --------------------------- 
    //private static attribute
    var _id = 0;

    //private static methods
    function _increaseId(){
        _id = _id + 1;
        return _id;
    }

    function _resetId(){
        _id = 0;
    }
 
    function _currentId(){
        return _id;
    }
    //-------------------- static scope --------------------------- 
 
    return {
        
        instance : function(name){ //constructor

            //private attribute
            var _name = name || ""; //if(name === undefined) _name = ""; else _name = name;
   
            //call private static method
            var _id = _increaseId();

            //private methods -------------------------------------------
            function  _circuit(signal){
                //circuit work start
                //....
                console.log("circuit working ... signal [" + signal.first + ", " + signal.second + "]");

                if(signal.first === 0 && signal.second === 0){
                    //go to front
                    //call private method
                    _motorLeft('counterClockwise');
                    _motorRight('clockwise');
                }        

                //...
                //circuit work end
            }

            function _motorLeft(rotate){
                //motor left work
                //...

                console.log("motor left rotate [" + rotate + "]");
            }

            function _motorRight(rotate){
                //motor right work
                //...

                console.log("motor right rotate [" + rotate + "]");
            }       
            //private methods -------------------------------------------




            //privileged methods ----------------------------------------
            this.goToFront = function(){
                console.log("goToFront");
                _circuit({
                    first : 0,
                    second : 0
                });
            }

            this.goBack = function(){
                //...
                //... 
            }

            this.turnRight = function(){
                //...
                //...     
            }     

            this.turnLeft =  function(){
                //...
                //...      
            }

            this.getRobotId = function(){
                return _id;
            }
   
            this.getRobotName = function(){
                return _name;
            }
            //privileged methods ----------------------------------------
        },

        //create static methods reference to private method (closure _resetId() )
        resetId : _resetId,
        currentId : _currentId //reference to closure ( _currentId() )
    }
})();


//utilities
function separateLog(count){
    for(i=0; i< count/2; i++){
        console.log("");
        console.log("");
    }
}



/**
* example to use
*/

console.log("***** Before change");
console.log(Robot);



//modify class ----------------------------------------------
//add public static method
Robot.staticMethod = function(){
    console.log("call static method");
};


//add public method (multiple)
Robot.instance.prototype = {

    publicMethod1 : function(){
        console.log("call public method1");
    },

    publicMethod2 : function(){
        console.log("call public method2");
    }
};

//add public method (single)
Robot.instance.prototype.publicMethod3 = function(){
    console.log("call public method3");
};
//modify class ----------------------------------------------


separateLog(1);
console.log("***** After change");
console.log(Robot);



separateLog(2);
console.log("Test new instance");
console.log("robot1 ----------------------------------------------------------");
var robot1 = new Robot.instance("eibot"); //new instant and set name
console.log(robot1.getRobotId()); //id => 1
console.log(robot1.getRobotName()); //name = eibot
robot1.goToFront();


separateLog(1);
console.log("robot2 ----------------------------------------------------------");
var robot2 = new Robot.instance(); //new instance not set name
console.log(robot2.getRobotId()); //id => 2


separateLog(1);
console.log("robot3 ----------------------------------------------------------");
var robot3 = new Robot.instance();
console.log(robot3.getRobotId()); //id => 3


separateLog(1);
console.log("robot4 ----------------------------------------------------------");
//reset robot id
Robot.resetId(); //id => 0
console.log("reset robotId");


var robot4 = new Robot.instance();
console.log(robot4.getRobotId()); //id => 1 


separateLog(2);
//test call public method   
robot4.publicMethod1(); 
robot4.publicMethod2(); 
robot4.publicMethod3(); 

//check Robot current id
console.log("");
console.log("current robotId => " + Robot.currentId()); //call static method get current id


separateLog(2);
console.log("id of robot2 => " + robot2.getRobotId());
console.log(robot2);
//

ไม่มีความคิดเห็น:

แสดงความคิดเห็น