Article Preview
Buy Now
| Print: | |
| PDF: |
Object-Oriented Thinking
Return Type Covariance
Issue: 2.4 (March/April 2004)
Author: Charles Yeomans
Author Bio: Charles Yeomans pontificates on REALbasic from Lexington, Kentucky.
Article Description: No description available.
Article Length (in bytes): 4,652
Starting Page Number: 36
RBD Number: 2417
Resource File(s): None
Related Link(s): None
Known Limitations: None
Excerpt of article text...
REALbasic 5.5 adds a new language feature, return type covariance. Despite the technical name, it's easy to grasp by seeing it in use. So let's begin by seeing how return type covariance solves a problem.
The Factory Method Pattern
In REALbasic, Java, and C++, the New operator is not polymorphic; that is, you cannot select the class of the object returned at runtime. The Factory Method pattern is a standard scheme for implementing polymorphic object creation. In this pattern, you define an abstract class, or a class interface, with a NewObject function. You then implement this function using the New function in subclasses. NewObject can be called polymorphically, solving your problem. Let's make up an example.
PersistentObjectFactory is a class that handles the creation and management of objects stored in a database. PersistentObjectFactory has a function, NewObject() as PersistentObject. We'll implement it in subclasses corresponding to concrete subclasses of PersistentObject. Essentially, NewObject will be a wrapper function that calls through to New for a particular concrete subclass of PersistentObject.
...End of Excerpt. Please purchase the magazine to read the full article.
Article copyrighted by REALbasic Developer magazine. All rights reserved.
|










