Javascript object-oriented (3) interface code _ js object-oriented

Source: Internet
Author: User
Interface: the interface in the principle program specified for the implementation of a task, function, purpose, and so on: specifies several methods and method names. (Because the tasks to be completed in the program are implemented through functions or methods .)

Interface in javascript: compare an "type object" of the instance with the "interface object" of the instance to conform to the rules. You can say: this object implements the specified interface;

(Interface Class: this class is used to implement different interfaces. That is, different interface instances, that is, the number and name of different methods)
(For comparison: the essence is to determine whether the object of the Child-type instance has the method name stored in the interface object and the number of methods .)
Small instance:
The following sub-categories are available: "server", "Mobile Phone", and "tablet phone. These sub-classes share a common task, function, or purpose-[disconnect the phone]
To implement this function, different sub-classes can have different internal implementation methods, so that the phone can be called. However, for the user's consideration, a rule must be made:
No matter what type you are, the objects that come out of your instance, that is, the objects that have the phone function, there must be two methods to achieve [disconnect the phone,
That is: 1. Press the outgoing phone number (number key) 2. Press the dial key;
The following is a fixed design pattern:

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 = [];
For (var I = 0, len = methods. length; I If (typeof methods [I]! = 'String '){
Throw new Error ("the name of the interface method must be a string ");
}
This. methods. push (methods [I]);
}
};
// Static class Method
Interface. ensureImplements = function (myobject1, Iobject1 ){
If (arguments. length! = 2 ){
Throw new Error ("method Interface. ensureImplemnents specifies" + arguments. length + "parameters, but expected to be 2 .");
}
For (var I = 1, len = arguments. length; I Var _ interface = arguments [I];
// Interface object, which must be provided through the interface class instance
If (_ interface. constructor! = Interface ){
Throw new Error ("Interface, not through Interface class, instance ");
}
// Obtain the method name in the interface object and combine it with the mobile phone object in this example to verify whether the mobile phone object has these two methods, and whether the method name is the same;
For (var j = 0, methodsLen = _ interface. methods. length; j Var method = _ interface. methods [j];
If (! Myobject1 [method] | typeof myobject1 [method]! = 'Function '){
Throw new Error ("verified Function: Interface. ensureImplements:" + myobject1.name + "Object method" + method + "cannot be found or is not a Function ");
}
}
}
};


The following is an example

The Code is as follows:


// Through the interface class, the instance generates an "Disconnect call" interface. Now this interface object testInterface requires two methods, and the method names are "callfun" and "callnum"
Var testInterface = new Interface ("call", ["callfun", "callnum"]);
// Mobile phone class, constructor;
Var mobilepone = function (call ){
This. name = call;
}
// Public methods for mobile phones
Mobilepone. prototype = {
"Constructor": mobilepone,
// The method name must be the same as the method name specified in the preceding interface object;
"Callfun": function (){
Document. write ("Press ");
},
// The method name must be the same as that specified in the preceding interface object
"Callnum": function (){
Document. write ("dial ");
}
}
// A Samsung mobile phone instance
Var anycall = new mobilepone ("anycall ");
// Check whether the Samsung mobile phone object implements the [disconnect phone] interface. That is, the Samsung mobile phone object and the interface object are passed into the verification function as parameters for comparison.
Interface. ensureImplements (anycall, testInterface );
Anycall. callnum ();

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.