Javascript Design Pattern Chapter 2 Interface

Source: Internet
Author: User

First principle of Object-Oriented Design

Interface is one of the most useful tools in the toolbox of object-oriented javascript programmers. Gof mentioned in the book that the first principle of object-oriented design is: "interface programming rather than implementation programming ".
 
Javascript has no interface

However, javascript itself does not have built-in methods for creating or implementing interfaces. That is to say, javascript itself has no interface. However, due to the powerful flexibility of javascript, we can simulate the implementation of interfaces in javascript.

 
Interface Definition, what is an interface

The interface provides a means to illustrate the methods that an object should have. It can indicate the semantics of these methods, but does not specify the specific implementation of these methods. Haha, I feel very unprofessional.
 
Advantages and disadvantages

Benefits:
1. interfaces can promote code reuse
2. Stable communication between classes
3. Convenient testing and debugging
Disadvantages:
1. The interface strengthens the role of the type and reduces the flexibility of the javascript language.
2. javascript does not support native Interfaces
3. Performance impact of javascript imitation Interfaces
4. javascript cannot force other programmers to abide by your interface definition (this depends on administrative means, huh, huh)
 
Interface implementation in other languages

The interface and implements keywords are basically used in other languages and are supported internally.
 
Methods for Imitating interfaces in javascript

1. describe interfaces with annotations
Imitating an interface with annotations is the easiest way to write the interface definition information in the annotations. This is super simple, but the effect is not very good.
Example:
Java code
/**
// Here is the interface description
Interface XXXInterFace {
Function add (child );
Function remove (child );
}
*/
Var XXXClass = function () {// implements XXXInterFace

}
XXXClass. prototype. add = function (){
......
}
XXXClass. prototype. remove = function (){
......
}
 
2. Use the property check imitation Interface
This method is more rigorous than the first one. All classes must explicitly declare their own interfaces. Although the interface is still annotated, you can check an attribute to find out what interfaces are implemented.
 
Java code
/**
// Here is the description of the interface
Interface XXXInterFace {
Function add (child );
Function remove (child );
}
*/
Var XXXClass-function (){
This. implementsInterfaces = ["XXXInterFace"]; // declare the implemented Interface
}
XXXClass. prototype. add = function (){
......
}
XXXClass. prototype. remove = function (){
......
}
// Call the Class Method
Function dosome (xxxClassInstance ){
If (! Implements (xxxClassInstance, "XXXInterFace") {// check the implemented Interface
Alert ("XXXInterFace not implemented ")
}
}

Function implements (obj) {// check the method of the implemented Interface
......

}
 
3. Duck-type analog interface
DUCK model (duck test)-a DUCK is walking and screaming like a DUCK. This is very interesting. The book says that this is james Whitcomb Riley's famous saying. Do you know this great god? I googled it and added a connection to Wikipedia on the previous connection.
Write
James watcomley Riley (1849-1916), Indiana poet, may have invented this statement. "When I saw a bird walking like a duck, swimming like a duck, and singing like a duck, I called it a duck," he wrote ."

To put it bluntly, this method does not need to be annotated and can be enforced. However, there is an additional tool InterFace and an auxiliary function ensureImplements.

Java code
// Define the declarative Interface
Var XXXInterFace = new InterFace ("XXXInterFace", ["add", "remove"]);

// Call Method
Function doSome (instance ){
EnsureImplements (instance, XXXInterFace); // Interface Check
}
 
 
 
4. Interface implementation in this book, Interface Class
The book uses the combination of the first and third types, that is, I have written a comment to tell you the interface I implemented and used the duck identification Interface Check. That is to say, if we want to use interfaces, we can also simulate them. The other is to remind everyone to write comments carefully.
The specific InterFace classes in the book are as follows:

Java code
// Constructor. You cannot copy the source code on the Internet.

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 = [];
For (var I = 0, len = methods. length; I <len; I ++ ){
If (typeof methods [I]! = String ){
Throw new Error ("Interface constructor expects method names to be"
+ "Passed in as a string .");
}
This. methods. push (methods [I]);
}
};

// Static class method.

Interface. ensureImplements = function (object ){
If (arguments. length <2 ){
Throw new Error ("Function Interface. ensureImplements called with" +
Arguments. length + "arguments, but expected at least 2 .");
}

For (var I = 1, len = arguments. length; I <len; I ++ ){
Var interface = arguments [I];
If (interface. constructor! = Interface ){
Throw new Error ("Function Interface. ensureImplements expects arguments"
+ "Two and above to be instances of Interface .");
}

For (var j = 0, methodsLen = interface. methods. length; j <methodsLen; j ++ ){
Var method = interface. methods [j];
If (! Object [method] | typeof object [method]! = Function ){
Throw new Error ("Function Interface. ensureImplements: object"
+ "Does not implement the" + interface. name
+ "Interface. Method" + method + "was not found .");
}
}
}
};
 
 
 
Interface-dependent Design Mode

Factory Model
Combination Mode
Decoration Mode
Command mode

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.