Table of Contents
Interfaces
qooxdoo supports Java like interfaces.
Defining Interfaces
Interface definitions look very similar to normal class definitions.
Example:
qx.Interface.define("qx.test.ISample", { extend: [SuperInterfaces], properties: {"color": {}, "name": {} }, members: { meth1: function() { return true; }, meth2: function(a, b) { return arguments.length == 2; }, meth3: function(c) { return qx.Class.hasInterface(c.constructor, qx.some.IInterface); } }, statics: { PI : 3.14, staticMethod: function(z) { return typeof z == "string"; } }, events : { keydown : "qx.event.type.KeyEvent" } });
Definition
Interfaces are declared using qx.Interface.define. Interface names start by convention with an I (uppercase "i"). They can inherit from other interfaces using the extend key. Multiple inheritance of interfaces is supported.
Properties
Properties in interfaces state that each class implementing this interface must have a property of the given name. The property definition is not evaluated and may be empty.
Members
The member section of the interface lists all member functions which must be implemented. The function body is used as a precondition of the implementation. By implementing an interface the qooxdoo class definition automatically wraps all methods required by the interface. Before the actual implementation is called, the precondition by the interface is called with the same arguments. Only if the return value evaluates to false an exception will be thrown. In simple cases it is sufficient to simply return true.
Statics
Statics behave exactly like statics defined in mixins and qooxdoo classes. They are accessible through their fully-qualified name. For example, the static varaiable PI could be used like this:
var a = 2 * qx.test.ISample.PI * (r*r);
Events
Each event defined in the interface must be declared in the implementing classes. The syntax matches the events key of the class declaration.
Implementing Interfaces
With implement key of the class declaration, a list of interfaces can be listed, which the class implements. The class must implement all properties, members and events declared in the interfaces. Otherwise a runtime error will be thrown.
Example:
qx.Class.define("qx.test.Sample", { implement: [qx.test.ISample], properties: { "color": { check: "color"}, "name": { check: "String"} }, members: { meth1: function() { return 42; }, meth2: function(a, b) { return a+b }, meth3: function(c) { c.foo() } } events : { keydown : "qx.event.type.KeyEvent" } });
Validating Interfaces
qx.Class contains several static methods to check, whether a class or an object implements an interface:
qx.Class.hasInterface(): Whether a given class or any of its superclasses includes a given interface.qx.Class.implementsInterface(): Checks whether all methods defined in the interface are implemented in the class. The class does not need to implement the interface explicitly.
It is further possible to use interfaces as property checks.
Interfaces Quick Ref
- Interfaces Quick Ref - a syntax quick reference for interfaces
