Interfaces Description

Syntax

The following construction is used to describe interfaces:

$ InterfaceDeclaration = INTERFACE ident [“:” BaseInterface]

InterfaceMemberList

END INTERFACE ident ";"

 

$ BaseInterface = qualident

$ InterfaceMemberList = [ InterfaceMemberHeading { “;” InterfaceMemberHeading} ]

$ InterfaceMemberHeading = ProcedureHeading | FunctionHeading | PropertyHeading

$ PropertyHeading = PropertyHeading_Old | PropertyHeading_New

 

$ ProcedureHeading = SUB ident “(“ [FormalParameters] “)”

 

$ FunctionHeading = FUNCTION ident “(“ [FormalParameters] “)” “:”qualident

 

$ PropertyHeading_Old = PROPERTY ident [ Indexes ] ":" ClassType Specifiers [ DEFAULT ";"]

$ Indexes = "(" [ISection {";" ISection}] ")"

$ ISection = ident ":" ClassType

$ Specifiers = ( GET ident | SET ident | GET ident SET ident ) ";"

 

$ PropertyDeclaration_New = PROPERTY ident [ Indexes ] ":" ClassType Specifiers_1 END PROPERTY ident ";" [ DEFAULT ";"]

 

$ Specifiers_1 = ( GetDeclaration | SetDeclaration | GetDeclaration SetDeclaration) ";"

$ GetDeclaration = GET

$ SetDeclaration = SET

Description

Like classes, interfaces define a set of properties, methods and events. Unlike classes, interfaces do not provide implementation. They are implemented by classes, though they are defined as elements independent of classes.

An interface is a contract where a class implementing the interface must implement each aspect of this interface in strict compliance with its definition.

Extended interface implementations are developed with no risk to the existing code, thus minimizing compatibility problems. New properties and methods can also be added at any moment by developing additional interfaces and implementations. Though interface implementations can be developed, it is not recommended to change interfaces. Changing the used interface can disrupt the work of the existing code. If an interface is regarded as a contract, it becomes clear that both contract parties are important. The interface publisher agrees not to change the interface, whereas the implementer agrees to implement the interface in strict compliance with its design.

An interface definition is contained between two key words, that is, Interface and End Interface. After a user interface has been named, you can specify one interface to inherit this interface from using ":".

NOTE. Inheritance from the system interfaces is forbidden.

The interface body can have a description of properties, procedures and functions. The interface has no implementation code or key words associated with the implementation code, including End Sub or End Function. To declare properties, use both old and new declaration methods.

NOTE. Depending on the method used when declaring properties in the interface, a corresponding method should be used when implementing properties in a class.

To call methods of the object implementing the interface, use the following syntax:

<object name>.<method name>.

Example

Let us consider the creation of a custom collection of symbol elements. The custom interface IMyInterface is used to describe the structure of the collection. This interface describes the signature of the two properties: Item and Items. The Item property is available for reading and writing. This is the default property (You can access this property without specifying the property itself. You need to specify only the index of the required element).  To access the whole collection use the Items property (a read-only property). Both the old declaration method (for the Item property) and the new one (for the Items property) are used. These properties are implemented in the MyArray class. All values of the collection are stored in the internal symbol array PropValue of the MyArray class.

Interface IMyInterface

Sub Set_Item(i: Integer; s: String);

Function Get_Item(i: Integer): String;

Public Property Item(i: Integer): String Get Get_Item Set Set_Item; Default; //Default property

 

Public Property Items: Array Of String

Get

End Property Items;

End Interface IMyInterface;

 

Class MyArray: Object, IMyInterface

 

Private PropValue: Array Of String;

 

Public Constructor Create(Length: Integer);

Begin

PropValue := New String[Length];

End Constructor Create;

 

Sub Set_Item(i: Integer; s: String);

Begin

PropValue[i] := s;

End Sub Set_Item;

 

Function Get_Item(i: Integer): String;

Begin

Return PropValue[i];

End Function Get_Item;

Public Property Item(i: Integer): String Get Get_Item Set Set_Item; Default; //Default property

 

Public Property Items: Array Of String

Get

Begin

Return PropValue;

End Get

End Property Items;

End Class MyArray;

 

Sub Main;

Var

Arr: IMyInterface;

s: String;

Begin

Arr := New MyArray.Create(2);

Arr(0) := "First element";

Arr.Item(1) := "Second element";

For Each s In Arr.Items Do

Debug.WriteLine(s);

End For;

End Sub Main;

After the Main procedure is started, a custom collection instance is created. Collection size equals to two elements. As the Item property is a default property, you cannot select its values without specifying the name of the property. Values of the elements can be specified using different methods. The set values of the collection elements will be displayed in the development environment console for checking.

See also:

Descriptions and Syntax Rules