Static Member Data and Singleton Objects

Static Member Data and Singleton Objects

For access to this article, please select a purchase option:

Buy chapter PDF
(plus tax if applicable)
Buy Knowledge Pack
10 chapters for £75.00
(plus taxes if applicable)

IET members benefit from discounts to all IET publications and free access to E&T Magazine. If you are an IET member, log in to your account and the discounts will automatically be applied.

Learn more about IET membership 

Recommend Title Publication to library

You must fill out fields marked with: *

Librarian details
Your details
Why are you recommending this title?
Select reason:
A Guide to MATLAB® Object-Oriented Programming — Recommend this title to your library

Thank you

Your recommendation has been sent to your librarian.

Quite often, a class needs to manage data that must be shared among all objects of the class. Every object of the class needs full and immediate access to this classwide data. When one object changes a value, this change must be immediately available to every object in the class. In other languages, such classwide data are often called static. So-called static member variables can't be stored with an object's private structure because objects maintain separate copies of their private data. Using global data is a possibility, but that has its own set of limitations. Under the right conditions, a persistent variable is perfectly suited for this application. In this chapter, we implement a static member variable strategy that uses a persistent variable. The implementation creates a standard location and an interface that fit nicely into the group-of-eight framework. The example implementation also includes a way to save and load static variables along with the private variables. Objects with this kind of load and save capability are often called persistent objects. With the introduction of static variables, we can now define a class using only static variables. Objects of an all static variable class are called singleton objects because all objects of the class share a single copy of their variables.

Chapter Contents:

  • 20.1 Adding Static Data to Our Framework
  • 20.1.1 Hooking Static Data into the Group of Eight
  • Static Variables and the Constructor
  • Static Variables in get and set
  • Static Variables in display
  • 20.1.2 Overloading loadobj and saveobj
  • 20.1.3 Counting Assignments
  • 20.2 Singleton Objects
  • 20.3 Test Drive
  • 20.4 Summary
  • 20.5 Independent Investigations

Inspec keywords: object-oriented programming; data privacy; object-oriented languages

Other keywords: classwide data; data management; class object; object private structure; static member variable strategy; object load capability; static member variable; static variable; static member data; persistent object; object save capability; singleton object; group-of-eight framework

Subjects: High level languages; Data security; Object-oriented programming

Preview this chapter:
Zoom in

Static Member Data and Singleton Objects, Page 1 of 2

| /docserver/preview/fulltext/books/pc/sbpc001e/SBPC001E_ch20-1.gif /docserver/preview/fulltext/books/pc/sbpc001e/SBPC001E_ch20-2.gif

Related content

This is a required field
Please enter a valid email address