Approaches to Web Development for Bioinformatics

Developing Applications with C#

Previous  Contents  Next 
References

The Singleton Pattern, Static Data, and Constants

To limit instantiation of a class to your own factory method or when there are only static methods use a private constructor. If a class has a private constructor and no public constructors, then other classes cannot create instances of this class. If you don't use an access modifier before the constructor it will be private by default.

Static constructors are used to initialize static data within a class or perform an action that only needs to be executed once during the lifetime of the program. A static constructor cannot have access modifiers or parameters. Static constructors are called automatically by the virtual machine to initialize the class. Example of these concepts are given in class DNAAlphabet.cs below.


C#

// Class represents an entity with a name and descriptive annotation
private abstract class Symbol
{
private string name, annotation;

string GetName() { return name; }

string GetAnnotation() { return annotation; }
public Symbol(string _name, string _annotation)
{
this.name = _name;
this.annotation = _annotation;
}

public override string ToString()
{
return "Name: " + name + ", Annotation: " + annotation;
}
}

// Class represents a symbol for the nucleic acid base Adenine
public class Adenine : Symbol
{
private const string NAME = "a";
private const string ANNOTATION = "Adenine";

private static Symbol instance;

private Adenine(string _name, string _annotation) : base(_name, _annotation)
{
}

public static Symbol getInstance()
{
if (instance == null)
{
instance = new Adenine(NAME, ANNOTATION);
}
return instance;
}
}

// Other symbol defintitions for Guanine, Cytosine, and Thymine

// Class represents a collection of DNA symbols
public class DNAAlphabet
{
public static Symbol a;

static DNAAlphabet()
{
a = Adenine.getInstance();
System.Console.WriteLine("DNAAlphabet static constructor invoked. a: " + a);
}

public DNAAlphabet()
{
System.Console.WriteLine("DNAAlphabet instance constructor invoked. a: " + a);
}

static void Main()
{
DNAAlphabet dNAAlphabet = new DNAAlphabet();
}

}

The private constructor for Adenine prevents multiple instantiation of the class from outside. A private static instance is given out to consuming code that wants an instance. Adenine demonstrates invocation of the parent constructor using the base keyword. The DNAAlphabet class has a static constructor, which gets the singleton Adenine instance. This pattern (the singleton pattern) can be used with a large sequence of DNA symbols that actually only refer to four Symbol object for Adenine, Guanine, Cytosine, and Thymine.

Running the program produces the output:


>DNAAlphabet.exe
DNAAlphabet static constructor invoked. a: Name: a, Annotation: Adenine
DNAAlphabet instance constructor invoked. a: Name: a, Annotation: Adenine

Constants should be used for values that do not change. The Adenine class above demonstrates the const keyword to define the constants NAME and ANNOTATION.


Previous  Contents  Next 
References

Contributed Comments and NotesAdd a comment.

There are no user comments.

Google

Please send ideas and opinions by email at alexamies@gmail.com.

© 2006-2007 Alex Amies