Interface: defined principles for implementing a task, function, and purpose.
ProgramInterface in: specify several methods and the method name. (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:
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 ("the interface method name 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 two parameters are expected .");} For ( VaR I = 1, Len = arguments. length; I <Len; I ++ ){ VaR _ Interface = Arguments [I]; // Interface object. If (_ Interface. constructor! = Interface ){ Throw New Error ("interface, not through interface class, instance" );} // Obtain the method names in the interface object and combine them with the mobile phone object in this example to verify whether the mobile phone object has these two methods and whether the method names are the same; For ( VaR J = 0, methodslen = _ interface. Methods. length; j <methodslen; 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
// 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, // It must be the same as the method name specified in the preceding interface object; "Callfun ": Function () {Document. Write ( "Buttons" );}, // It must be the same as the method name specified in the preceding interface object. "Callnum ": Function () {Document. Write ( "Dial number" );}} // A Samsung mobile phone instance VaR Anycall = New Mobilepone ("anycall" ); // Checks 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 ();