Article Preview
Buy Now
COLUMN
Reconstituting Objects
Refactoring to Patterns
Issue: 5.1 (September/October 2006)
Author: Charles Yeomans
Author Bio: Charles is the author of "I Declare: Calling External Functions in REALbasic", available online at
Article Description: No description available.
Article Length (in bytes): 6,876
Starting Page Number: 39
Article Number: 5116
Related Web Link(s):
http://www.declareSub.com/
Excerpt of article text...
Here is our problem: we have saved persistent objects of various classes on disk as XML data. How do we read the objects back into memory?
Reading the XML and crunching it using the XML capability in REALbasic is simple enough. The real problem is how to create the persistent objects. The New operator requires that you specify a class at compile-time; that is, in code. But we only know the class of a persistent object at runtime, when we read it from the XML.
Now, classes, as opposed to instances of classes, cannot be created at runtime. Thus all possible classes are known. So the basic idea is to define a mapping from class names as Strings to calls to New. Starting from this, we proceed with a very simple implementation, and refactor it to improve encapsulation and isolate the the code that may need to be changed.
Let's begin with the method we know we need. We make it a shared method of Persistent, the parent class of all of our persistent objects, that takes a string representing the class name and returns an object of type Persistent. The simplest implementation is to write a Select Case statement.
Shared Function NewObject(className as String) as Persistent
...End of Excerpt. Please purchase the magazine to read the full article.