Special

Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! Xojo Bundle

This bundle includes six back issues of the magazine -- all of year 21 in printed book and digital formats -- plus a one-year subscription (beginning with 22.1) so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as $35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Article Preview


Buy Now

Issue 5.1

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 http://www.declareSub.com/.
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.