Javascript design pattern-encapsulation and Information Hiding (lower)

Source: Internet
Author: User

Today, we will explain how to implement static methods and attributes in advanced patterns. There are other knowledge points about constants.

1. Static methods and attributes

In fact, we still need to use closures to implement this function. The third implementation method in the previous section also uses closures. However, internal attributes and methods are instance-level.

 
VaRBook1 =NewBook ('isn1 ', 'title1', 'author1');VaRBook2 =NewBook ('isbn2 ', 'title2', 'author2 ');

Alert (book1.gettitle (); // output title1
Alert (book2.gettitle (); // output title2

The above example shows that the created book1 and book2 have independent private attributes, and the assignment of values to the private attributes of book2 does not affect book1.

So how can we implement the class-level private attributes and methods? In addition to using closures, we also need to use anonymous functions.CodeCode 1

 VaR Book = ( Function  (){ //  Private Static attributes.  VaR Numofbooks = 0 ;  //  Private static method.  Function  Checkisbn (ISBN ){...}
// Return the constructor. Return Function (Newisbn, newtitle, newauthor ){ // Implements publication // Private attributes. VaR ISBN, title, author; // Privileged methods. This . Getisbn = Function (){ Return ISBN ;}; This . Setisbn = Function (Newisbn ){ If (! Checkisbn (newisbn )) Throw New Error ('book: Invalid ISBN .' ); ISBN = Newisbn ;}; This . Gettitle = Function (){ Return Title ;}; This . Settitle = Function (Newtitle) {Title = Newtitle | 'no title specified' ;}; This . Getauthor = Function (){ Return Author ;}; This . Setauthor = Function (Newauthor) {author = Newauthor | 'no author specified' ;}; This. getnumofbooks = function (){
Return numofbooks;
};

// Constructor code. Numofbooks ++; // Keep track of how many books have been instantiated // With the Private Static attribute. If (Numofbooks> 50) Throw New Error ('book: only 50 instances of book can be '+ 'created .' ); This . Setisbn (newisbn ); This . Settitle (newtitle ); This . Setauthor (newauthor );}})();
// Public static method. Book. converttotitlecase = Function (Inputstring ){...};
// Public, non-privileged methods. Book. Prototype = {Display: Function (){...}};

If you are not familiar with anonymous functions, you can view relevant information on your own. In fact, the above Code is equivalent to: Code 2

 VaR Tempbook = Function  (){ //  Private Static attributes.  VaR Numofbooks = 0 ;  //  Private static method.  Function  Checkisbn (ISBN ){...}  //  Return the constructor.  Return   Function (Newisbn, newtitle, newauthor ){ //  Implements publication ...
}};
VaR book = tempbook ();

// Public static method. Book. converttotitlecase = Function (Inputstring ){...}; // Public, non-privileged methods. Book. Prototype = {Display: Function (){...}};

So what will happen if you execute the previous Code:

VaRBook1 =NewBook ('isn1 ', 'title1', 'author1');VaRBook2 =NewBook ('isbn2 ', 'title2', 'author2 ');
Alert (book1.getnumofbooks (); // output 2
Alert (book2.getnumofbooks (); // output 2

Why is there such an effect? In fact, numofbooks is already a real private static variable through the combination of anonymous functions and closures, no matter how many book instances you create, there is only one numofbooks variable in the system. Some people may wonder why one numofbooks is not generated every time a new book instance is created?

We can find the answer from code 2. In fact, the created book instance executes the tempbook () method instead of the new tempbook object! The tempbook object runs only once in the system, so there is only one copy of its internal numofbooks variable. Each executionNewBook ('...','...','...'); In fact, it only executesFunction(Newisbn, newtitle, newauthor), there is no new tempbook object,All book instances call the same numofbooks method when using the getnumofbooks method.

I don't know if my explanation is clear. I hope it will help you.

 

2. Constants

In fact, there is nothing to say about this part. As long as you understand the above content, implement a variable similar to numofbooks in the anonymous function and do not modify it throughout the initialization process, no modification method is provided, and a method similar to the getnumofbooks method is provided for external access. Then, the code is OK, and I am too lazy to write it.

 

3. advantages and disadvantages of using Encapsulation

Benefits:

You can control the code and publish information that you want to disclose. It is also convenient for you to refactor the code. The user of the object you create will not affect the normal use of others because you modify the internal object (private variable or method. Surface-to-surface interface programming can achieve loose coupling between codes.

Disadvantages:

1. It is difficult to test internal variables. The best way is to test internal methods by testing public methods.

2. Code debugging becomes difficult due to complicated scopes.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.