|
OInterface
Overview of OInterface
OInterfaceOInterface provides object-oriented Interface design patterns. Defining an interfaceUsagevar IMyInterface = new OInterface( name, definition );
OverviewAn interface is an object that contains a definition (see Interface definition object) which other objects must adhear to in order to be considered to implement your interface. Interface definition objectThe interface definition object is a POJO that contains properties describing the interface you want to define. Each property in the object represents an item that must exist in objects that implement your interface, and the value is an OCheck condition or comparer. Examplevar ICanSpeak = new OInterface("ICanSpeak", {
speak: { isType: "function" }
});The above code snippet defines an interface called ICanSpeak which mandates that in order for objects to be considered to implement this interface, they must provide a speak function. Checking implementationInterfaces only become useful when you can check to see if objects properly implement them or not. OInterface provides two handy methods to help. implementedByChecks to see if the specified object implements the interface or not. Returns true if it does, or false if not. (boolean) IMyInterface.implementedBy( obj );
ensureImplementedByThrows an error if the specified object does not properly implement the interface. (boolean) IMyInterface.ensureImplementedBy( obj [, obj2 [, obj3]] );
Error messagesErrors thrown by ensureImplementedBy try to be helpful by describing why the object failed the inspection. If a console is available it will also write additional details to that. OInterface in classesPassing an instance of an OInterface into OClass as a mixin will cause OInterface to ensure the class properly implements the interface. If it does not, it will raise the relevant exception. For example, given the following interface: var ICanSpeak = new OInterface("ICanSpeak", {
speak: { isType: "function" }
});The following code will raise an exception because it does not have a property called speak that is of type function. var MyClass = OClass(ICanSpeak, {});However, the following code will execute normally: var MyClass = OClass(ICanSpeak, {
speak: function(){}
});
| ||||||||||||||||