Special

Clearance Sale!

We've been publishing for over five years now and it's time to clear out our inventory of back issues, so we're slashing prices!

RBD Magazines

Check out this amazing clearance sale of all our past issues. Missing some issues? This is a great time to complete your RBD collection. Save up to 40% off the regular price of our printed back issue packages. These prices are only good until the end of the year May 2008 and supplies are limited, so place your order today.

Article Preview


Buy Now

Print:
PDF:

Object-Oriented Thinking

Implementing the Singleton Pattern

Issue: 3.2 (November/December 2004)
Author: Charles Yeomans
Author Bio: Charles Yeomans is a software developer in Lexington, Kentucky.
Article Description: No description available.
Article Length (in bytes): 5,203
Starting Page Number: 33
RBD Number: 3215
Resource File(s): None
Related Link(s): None
Known Limitations: None

Excerpt of article text...

It is sometimes the case that not only do you want to implement a single instance of a class, but that you also need to prevent more than one instance from being created. This is the Singleton pattern.

The usual way to implement a singleton in REALbasic is with a module. Modules are created at startup, and you cannot create multiple instances of them. A minor disadvantage is that modules don't support constructors and destructors, but that's easy enough to work around with methods to be called in App.Open and App.Close. However, if you need to create a singleton object that is a subclass of another class, then you can't use a module.

So let's consider how to implement a singleton using a class. For most purposes, it suffices to provide a global accessor function and access the instance through it. Let's begin with this simple technique.

Define a class, SingletonClass. We'll provide access to an instance of it from a module, Module1, with the following accessor function; pObject is a private property of Module1.

...End of Excerpt. Please purchase the magazine to read the full article.

Article copyrighted by REALbasic Developer magazine. All rights reserved.


 


|

 


Weblog Commenting and Trackback by HaloScan.com