![]() |
Using Static Methods to create a Singleton
PrerequisitesThis Magercise covers some aspects of method access and static variables and methods by creating a PersonFactory class. This new class acts as a singleton factory class to create Persons.Static variable declarations are discussed in detail in the course notes. Work LocationPerform all work for this magercise in VisualAge project MageLang Magercises, package magercises.singleton. If this project does not appear in your Workspace add it from the repository (if it exists there) or create a new project using this name. Tasks
Perform the following tasks:
Create the PersonFactory class with a private constructor that does nothing. Add a static variable to the PersonFactory class to hold an instance of that class. Add the method instance to the PersonFactory class. It will always return the single instance of the PersonFactory class, which is held in the static variable, instance. Add the method newPerson to the PersonFactory class to act as a surrogate for the now hidden constructor for Person. Modify the main method to use the new factory class.
|
Copyright © 1996-1997 MageLang Institute. All Rights Reserved. |