An interface is used to divide the uses of features common to several elements from their implementations, which simplifies their re-usability.
An interface is a list of available features, a kind of contract ("I am committed to provide the following methods and properties").
- An interface contains a list of prototypes of methods and properties.
- All methods and properties are accessible (no public/protected/private).
- There is no implementation element (no member, no code, ...).
An interface is a type that can be declared in the initialization code of project or in the declaration code of an element, especially the sets of procedures.
// Declare an interface
Basket is Interface
Property NumberProducts <get>
Declaring an interface Hide the details
<Interface Name> is Interface
Procedure <Procedure 1> ([<Parameters]) [: <Type of return>]
Procedure <Procedure N> ([<Parameters]) [: <Type of return>]
Property <Property 1> [: <Type of Return>] [<Get/Set>]
Property <Property N> [: <Type of Return>] [<Get/Set>]
Name of interface to declare.
Name of procedures listed by the interface. These procedures can have one or more parameters and they can return a value. You have the ability to specify the type of value returned by the procedure. This type can correspond to a simple type (integer, real, string, ...) or to a structured type (structure, object, ...).
Name of properties listed by the interface. These properties can:
- return a value. You have the ability to specify the type of value returned by the property. This type can correspond to a simple type (integer, real, string, ...) or to a structured type (structure, object, ...).
- have attributes used to specify whether the property is read-only or read/write, ...
End of interface declaration.
An interface can be implemented by a class: the class implements all the methods and properties of interface.
The "implement" keyword is used to link the interface and the class.
InternetBasket is Class
m_ArrProducts is array of ints
PROCEDURE PUBLIC NumberProducts()
A compilation error occurs if a class does not implement all methods or properties of interface.
You have the ability to declare variables to store and handle the elements by their interfaces.
In this case, you must use the '<-' operator to specify the real element handled via the interface. The syntax is as follows:
<VariableName> is <interface> <-...
// After a reconnection, retrieve the previous basket
CurrentBasket is Basket <- RestorePreviousBasket()
If the real element does not implement all the methods or properties, the variable is assigned with 'Null' and no error occurs. This allows you to check whether the element was converted into its interface by comparing it to Null. On the contrary, any later use of variable will trigger an error.
An interface can be used to type a procedure parameter.
If a procedure expects an "Interface Xxx" parameter, any element that implements all the procedures and methods of interface can be passed as parameter.
A compilation error occurs if the element does not implement all the methods and all the properties.
The real class does not have to explicitly implement the interface. However, all methods and all properties must be found.
- The interfaces can be used from an external component.
- Interfaces exported from a component: The interface should not be declared in the initialization code of project. Indeed, in this case, the projects that call the component will not see the interface.