JavaScript design pattern: JavaScript Design Pattern Learning interface

Source: Internet
Author: User
Tags object implement interface return

After reading the "JavaScript King return", in the library to find "JavaScript design mode" To see, before the design mode of the book read: "Head of the first design mode", "design mode of Zen", Gof's "Designing Patterns: Elements of reusable object-oriented Software read a part. Remember when you didn't have these basics, reading JavaScript design mode is simply not sure what the author is talking about.
Anyway
To be precise, JavaScript has no concept of classes, objects, JavaScript is based on object-oriented language, but the authors of the JavaScript design pattern have to say that JavaScript is object-oriented (and does not know whether translation is a problem). When using JavaScript to describe those design patterns, some of the features of Java, C + +, and other object-oriented languages (such as classes, interfaces, inheritance, public, private) need to be emulated through some features of JavaScript (such as closures, prototypes).
The book says three ways to simulate interface.
First: Describe the interface with annotations
I think this is an internal agreement, directly said "I realized the XX interface", in the language I personally feel that there is no binding, this really should not come out, a little impostors feeling.
The second type: Using attributes to examine mimic interfaces
A function called implements () is required to check
function implements (object) {
for (Var i=1;ivar InterfaceName = arguments[i];
var interfacefound = false;
for (Var j=0;jif (object.implementsinterfaces[j] = = InterfaceName) {//implementsinterfaces holds the name of the method defined by the interface
Interfacefound = true;
Break
}
}
if (!interfacefound) {
return false; Interface not found
}
}
return true;
}
This is part of the code in the JavaScript design pattern book, as the author describes it:
This method has several advantages. It provides a documentation description of the interfaces that the class implements. If the interface you want is not in the list of interfaces that a class claims to support, you will see an error message. By exploiting these errors, you can force other programmers to declare these interfaces. The main disadvantage of this approach is that it does not ensure that the class actually implements the interface that it claims to implement. Only know whether it says that it implements the interface, that it implements an interface when creating a class declaration, however, it is common to find an error when implementing the method provided by the interface method, but it is not possible for all checks to pass, but that method does not exist, which will bury a hidden danger in the code.
It leads to the third method: imitating the interface with Duck type identification
Principle: If an object has all methods with the same name as the method defined by the interface, it can be considered to implement this interface.
First you need an interface class interface
var Interface = function (name, methods) {
......
Accept two parameters: Interface Name, method name
This interface class also has a static method Ensureimplements ()
Interface.ensureimplements = function (object) {
......
}
The actual usage is this:
var composite = new Interface (' Composite ', [' Add ', ' remover ', ' getchild ']);
var formitem = new Interface (' Formitem ', [' Save ']);
var compositeform = function (id,method,action) {
......
}
function AddForm (forminstance) {
Ensureensureimplements (Formintance,composite,formitem);
This function would throw an error if a required method is not implemented
......
}
Finally, the author uses the first and third methods to implement the interface, the code is as follows:
var Interface = function (name,methods) {
if (arguments.length!=2) {
throw new Error ("Interface constructor called with" +arguments.length+
"Arguments,but expected exactly 2");
}
THIS.name = name;
This.methods=[]; This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20121229/35348.html

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.