My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
OInterface  
Overview of OInterface
Updated May 18, 2010 by matr...@mac.com

OInterface

OInterface provides object-oriented Interface design patterns.

Defining an interface

Usage

var IMyInterface = new OInterface( name, definition );

IMyInterface A variable to hold the interface you are creating
name A string containing a human-readable name for your interface. Useful to add meaning to any error messages that might occur during development.
rules An object containing the definition of your interface (see Interface definition object below)

Overview

An 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 object

The 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.

Example

var 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 implementation

Interfaces only become useful when you can check to see if objects properly implement them or not. OInterface provides two handy methods to help.

implementedBy

Checks to see if the specified object implements the interface or not. Returns true if it does, or false if not.

(boolean) IMyInterface.implementedBy( obj );

IMyInterface The OInterface you want to check obj for
obj The object being checked.

ensureImplementedBy

Throws an error if the specified object does not properly implement the interface.

(boolean) IMyInterface.ensureImplementedBy( obj [, obj2 [, obj3]] );

IMyInterface The OInterface you want to check obj for
obj The object being checked.
obj2, obj3 More objects to check

Error messages

Errors 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 classes

Passing 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(){}
});

Sign in to add a comment
Powered by Google Project Hosting