CS 4448 - Fall 1998
Object-Oriented Programming and Design
Talk 5.2
by
Ryan Fowler

Programming with Generic Interfaces
by
Lowell Kaplan
- Introduction
First, in order to understand what a generic interface is, we need
to define what a non-generic interface is (Listing 1).
- Get and set methods for every variable.
- Everything is laid out neatly.
- Moving on...
Now we want to look at a more generic interface (Listing 2).
- By using a hashtable we can eliminate individual data members.
- Now there are only two methods: one for accessing and one for mutating.
Advantages to this generic interface?
- Code is much smaller.
- Only need one member function for each data member.
- New attributes can now be added at runtime.
Disadvantages?
- Program needs to know the string names of the property to be used.
- They could be defined elsewhere, but that nullifies advantage #2.
- Loss of information about the properties.
- Variable "name" could be set with information for "price".
- Let's get more generic
Now let's create a method called "callMethod". It takes a string and an array.
The string is the name of the function and the array is the parameter list.
Advantages -
- We've attained a truly generic class.
- This would be easy to use for timing purposes.
- It could also make debugging code somewhat easier.
Disadvantages -
- The client must now know the names of the available properties.
- There is a loss of compile-time type checking.
- There is a significant loss of code readability.
- Conclusions
1. "Generic interfaces seem best suited for times when objects are likely
to change frequently, like during the beginning of a development projects.
Once the object's interface becomes more stable, a migration to regular
functions may be appropriate."
Copyright © University of Colorado. All rights reserved.
Revised: November 2, 1998