UsersGuide:Chapter 10:Class Interfaces
From Real Software Documentation
Contents |
Class Interfaces
A class interface is a construct that you can use to tie together classes that do not share a super class but have something in common in your application. Class interfaces are used to specify what an object does without specifying how it does it.
In order to understand class interfaces better, it’s helpful to think of a class as consisting of two components, the (public) interface to the class and the implementation. The interface consists of the class’s Public method calls and the implementation is the code that implements the methods. The interface says what the class does and the implementation says how it does it.
In the object hierarchy, a subclass inherits both the interface and the implementation from its super class. That is, it gets both the method calls and the specific implementation of the method.
Class interfaces enable you to separate the two constructs. If two or more classes need to do the same thing but do it in different ways, you use an interface instead of a super class.
A class interface operates as a “spec” that contains a list of methods that custom classes in your project use. It does not actually contain any code for the methods themselves.
The methods in the class interface are placeholders for methods that are actually contained in each custom class that “implements” the class interface. Also, a custom class can implement more than one class interface.
The term “implement” simply means that the class has methods of the same names and declarations that are found in the class interface. The class interface specifies the methods and their declarations but not the code.
Many class interfaces are built into Real Studio. You can implement any of these in your classes or add and implement your own. For example, the Readable and Writeable interfaces specify methods for reading and writing data. Each class that uses the Readable or Writeable interfaces supplies the implementations. For example, a class that reads data from the Serial port would use a different implementation than a class that reads data from a binary file.
When you specify that a class implements a class interface, the class must implement all the methods in the class interface and the method declarations must match. However, the classes are free to implement the methods in different ways. For example, a method that changes the font in a Label would be implemented in a different way than a method that changes the font in a Canvas control that displays text via calls to the Graphics class.
The process involves three basic phases:
- Creating the class interface,
- Creating the classes that implement the class interface,
- Adding the classes to your project and calling the class interface methods in your program. Typically, that means writing generic code that tests whether a class implements a class interface and executing class interface methods where appropriate.
To create a class interface, do this:
|
To implement a class interface, do this:
|
You can also enter the names of class interfaces directly into the Interfaces field in the Properties pane. Click in the text area of the Interfaces field to get an insertion point and enter the name of the interface into the Interfaces field of the Properties pane. When you enter an interface, Real Studio displays a dialog asking you whether you’d like it to generate all the method declarations for the interface.
If you click Yes, it displays the Implement Interface dialog where you can choose the interface you entered.
If you don’t accept this choice, you must take care to implement all the methods yourself.
When you add a class interface to a class, the Project Editor adds a third column and it lists the class interfaces for the class’s interfaces.
Specifying the Interface
In rare cases you may find that a class implements more than one method with the same name but from different class interfaces. You may have decided that a method implements two interfaces that both have a method that shares the same name and declaration. Normally, there is no way to tell them apart. However, there is a way to specify the class interface to which each method belongs. You can do so using the optional Interfaces field in the method declaration dialog box.
Suppose you have two class interfaces, Foo and Bar, that both contain a method named “wahoo” It has no parameters. Let’s say the method Baz implements both methods. Here is the Project Editor for this example.
In order to tell them apart, you use the optional “Implements” field in the method declaration dialog box.
To specify the interface, do this:
|
Use the dot notation to specify it in the format interfacename.methodname. This illustration shows the completed specification for this example.
Implementing Methods
The next task is to implement the methods of the class interfaces that you added to the class. If you used the Implement Interfaces dialog box, you have a head start on this task because it adds the method names and declarations of each class interface to the class automatically. If you entered the names of the class interfaces manually, you now need to add the required methods to the class.
To implement a method, do this:
|
Modifying and Deleting Interfaces
If you change your mind and want to delete or replace an interface, you do so via the Interfaces field in the Project Editor.
Click in the Interfaces field to get an insertion point and then select the name of the interface you want to modify or delete. Type to make the desired changes.
Please note that any methods that were added to the class while the interface was implemented are not modified or deleted when you remove the interface that they belonged to. That is, if you add an interface via the Implement Interfaces dialog, the methods that are specified by that interface remain as part of the class even if you delete the interface itself. You must take care of any “clean up” activities.
A Class Interface Example Project
The following very simple example illustrates the basic concepts. The application has custom classes based on the TextField and Label classes that implement a class interface. The class interface has one method specification for updating a font. The user chooses a font from the popup menu and a message is sent to all controls in a window that implement the class interface.
The class interface, FontInterfaceManager, contains one method specification:
This method declaration area specifies that each class that implements this class interface has to have a method called UpdateFont and it must take one parameter as a String.
Two custom classes based on TextField and Label implement this method, as is shown in the Project Editor:
The implementation of the UpdateFont method happens to be the same in both classes, but this is not a requirement of class interfaces in Real Studio. It is simply:
With these objects in place, generic code can be written that determines whether a control in a window implements the FontInterfaceManager class interface.
Suppose several instances of CustomTextFields and CustomStaticTexts are added to a window. The following code in the Change event of a PopupMenu changes the font displayed by all such controls based on the user’s selection. The IsA operator tests whether a control implements the class interface. If it does, the UpdateFont method of the class interface is called, as implemented by each custom class. The term Me.Text contains the current selection of the PopupMenu.
For i=0 to Self.ControlCount - 1 //number of controls in the window
If Self.Control(i) IsA FontInterfaceManager then
FontInterfaceManager(Self.Control(i)).UpdateFont(Me.Text)
End if
Next
As you can see, you can specify that any custom class implements a class interface, regardless of its super class. In this way, class interfaces can group together classes that are not related to one another via the object hierarchy and give them functionality that can be managed in one place in your project.
Creating a new Class Interface from an Existing Class
If an existing class in your project contains methods that you want to reuse as a class interface, you can generate the new class interface from the Project Editor.
To use an existing class as the basis of a class interface, do this:
|
Real Studio creates the new class interface, adds it to the project, and makes the current class an implementor of the new interface. That is, the name of the new class interface is now shown in the Interfaces field of the class’s Project pane.



