PureBasic and the Object-Oriented Programming

Interface instruction

Syntax:

       Interface <Name1> [Extends <Name2>]
         [Procedure1]
         [Procedure2]
         …
       EndInterface

The PureBasic Interface instruction allows grouping under the same Name (< Name1 > in the above box) various procedures.

Ex :

Interface My_Object
Procedure1(x1.l, y1.l)
Procedure2(x2.l, y2.l)
EndInterface

By declaring an element of My_Object, the access to its procedures looks like as follows.

  • The statement is made in the same way as for a Structure:

Object.My_Object

  • To use the object functions, write directly:

Object\Procedure1(10, 20)
Object\Procedure2(30, 40)

Using the Interface instruction leads to a very practical and pleasant notation to operate a procedure.
By writing " Object\Procedure1(10, 20) ", the Procedure1() from Object is called.
This notation is typical of the Object-oriented Programming.

Initialization:
After a variable statement follows normally the variable initialization.
It's the same for an element from an Interface.

As unexpected, by giving the name of a procedure inside the Interface:EndInterface block you don't refere to the implementation of the procedure, e.i. to refer to the Procedure:EndProcedure block of the wished procedure.

In fact, you can rename procedures inside a Interface:EndInterface block, by giving the names you want for the procedures that you go to use.

Then, how to connect this new name with the wished procedure?

As for overloaded methods, the solution is in the function addresses.
It is necessary to see names, into the Interface:EndInterface block, as function pointers in which the required function addresses are attributed.

However, to initialize the function pointers of an Interface specified element, it is necessary to process differently than for a Structure specified element.
In fact, it isn't possible to initialize individually each field defined by an Interface, because remember that Object\Procedure1() means a procedure call.

The initialization comes indirectly by giving to the element, the address of a initialized variable storing the function pointers.

A such variable is called: the table of the methods.

Ex: by resuming the Interface My_Object, let us consider the following Structure describing the function pointers:

Structure My_Methods
*Procedure1
*Procedure2
EndStructure

and the associated table of the methods:

Methods.My_Methods
Methods\Procedure1 = @My_Procedure1()
Methods\Procedure2 = @My_Procedure2()

where My_Procedure1() and My_Procedure2() are the wished procedure implementations.

Then, the initialization of Object from Interface My_Object looks like this:

Object.My_Object = @Methods

Next, by writing

Object\Procedure2(30, 40)

the Object Procedure2() function is called according My_Procedure2() implementation.

When an element from an Interface is declared, it is essential to initialize it before using element's procedures. Thus it is advised to initialize the element at statement time.

The table of methods structure composition must be the exact representation of the Interface composition. It must contain the same number of fields and must preserve the order of them to insure the right assignation between names and addresses of each function. It is only on these conditions that the element will be correctly initialized.

To summarize, using an Interface involve:

  • an Interface describing the required procedures to use,
  • a Structure describing the function pointers,
  • a table of the methods: a structured variable initialized with the required function adresses.

and the properties are:

  • an object-oriented notation
  • an easy way to rename procedures
Contents

[1-2-3-4-5-6-7-8-9]

Top of the page