How the interface is implemented
/** * Interface class * @param {String} name Interface name * @param {Array} methods interface method array
*/function Interface (name, methods) {//check variables, input parameters must be 2 if(Arguments.length!==2) { Throw NewError ("State one interface require 2 parameters"); } This. Name =name; This. Methods = []; if(Methods.length <=0) { Throw NewError ( This. Name +": Interface method must state"); } for(vari =0; i < methods.length; i++) { if(typeofMethods[i]!=="string") { Throw NewError ( This. Name +": "+ Method[i] +": Interface method must be string"); } This. Methods.push (Methods[i]); } } //Create an interface varComposite =NewInterface ("Composite", ["Add","Delete"]); //classes that implement interfacesfunction Compositeimpl () {} Compositei Mpl.prototype.add=function () {Console.log ("Add ..."); }; CompositeImpl.prototype.delete=function () {Console.log ("Delete ..."); }; //declare an instance varC1 =NewCompositeimpl (); /** * Check method * @param {Object} object*/function Checkclassimplementinterface (Object) { if(Arguments.length <2) { Throw NewError ("Check interface is required 2 parameters"); } varImplementinterfaces = {}; for(vari =1; i < arguments.length; i++) {Implementinterface=Arguments[i]; varImplementinterfacemethods =Implementinterface.methods; for(varj =0; J < Implementinterfacemethods.length; J + +) { if(!Object[Implementinterfacemethods[j] | |typeof Object[Implementinterfacemethods[j]]!=="function") { Throw NewError (Implementinterface.name +"."+ Implementinterfacemethods[j] +"ID Not method"); } } } } //Check if the class implements all interfacesCheckclassimplementinterface (C1, Composite);
JS Implementation interface