Design Pattern Learning - (1. Encapsulation)

I looked at design patterns a few years ago, but didn't study them in depth, and now I'm re-learning them.


Package:

First of all, you must understand that encapsulating a class, what parts of this class are made up

1: In the constructor, for copying the instantiated object.

2: Outside the constructor, directly added by dot syntax, only for this class. The instantiated object cannot be accessed. (static public properties, fan)

3: In the prototype of the class, that is, the method generated by the prototype. The instantiated object is accessible.


Create a class:

    // Part 1, each instantiated object has its own storage space and does not affect each other
    var Book = function (id , bookname, price) {
        //Private properties, methods --------------- not accessible from outside
        var id = id,
                name = "";
        function coutId(){
            return id;
        }

        //Public properties, methods --------------- externally accessible
        this.price = price;
        this.copy = function () {

        };

        // Privileged method, can access the object's private public --------------- externally accessible
        this.getName = function () {
            return name;
        };
        this.setName = function ( _name) {
            name = _name;
        };
        this.getId = coutId()

        // constructor
        this.setName( bookname );

    };
    // part 3
    Book.prototype.display = function () {

    };
    //or write like this
    Book.prototype = {
        publicVal : "Public property objects are accessible",
        publicFun: function () {
            console.log( "Public method, object accessible" );
        }
    };
    // part 2
    Book.staticPublicVal = "Static public properties, instantiated objects cannot access";
    Book.staticPublicFun = function () {
        console.log("Static public methods, instantiated objects cannot be accessed");
    };

    var book = new Book( "1", "js design mode" , "99" );
    console.log( book.getId );
    console.log( book.getName() );
    book.setName("Zhang Xiaofeng Design Pattern"); // Change the value of private variables through privileged methods
    console.log( book.getName() );
    console.log( book.staticPublicVal ); //undefined
    console.log( Book.staticPublicVal ); // "Static public properties, instantiated objects cannot access";

Closure implementation:

var Book = (function () {
            // Static private variables, the variables in the instantiated object all point to the unified memory space
            var bookNum = 0;
            // static private method
            function checkBook( name ){}
            // create class
            function _book( newId, newName , newPrice ){
                // private variable
                var name, price;
                // private method
                function checkId( id ){};
                // privileged method
                this.getName = function () {
                    return name;
                };
                this.setName = function ( _name) {
                    name = _name;
                };
                this.getPrice = function () {
                    return price;
                };
                this.setPrice = function ( _price ) {
                    price = _price;
                };
                // public properties
                this.id = newId;
                // public method
                this.copy = function(){};
                // constructor
                this.setName( newName );
                this.setPrice( newPrice );

                bookNum++;
                if( bookNum >100 ){
                    throw new Error("Static private variables exceed 100, too many instantiated objects");
                }else{
                    console.log( "Number of instantiated objects: "+bookNum );
                }
            }
            // build prototype
            _book.prototype = {
                publicVal : "Public property objects are accessible",
                publicFun: function () {
                    console.log( "Public method, object accessible" );
                }
            };
            // return class
            return _book;
        })();

        var b1 = new Book( "1", "js design mode" , "99" );
      
        var b2 = new Book( "1", "js design mode" , "99" );



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326019136&siteId=291194637