Singleton Class
Before, we had utility classes, no instances were allowed.
Singleton class -> only one object is allowed to be instantiated.
Good for:
- Things that need to be globally constant, e.g. configuration and system status details.
- The key is the access specifier of the constructor: it must be private.
Object creation will take place within the class ---> created instances will be returned using a method other than the constructor...e.g getInstance()
Attempt 1
- Use static features to count # of instances created -> instanceCount variable.
- getInstance() checks instanceCount.
- If 0, create an instance and return it, set instance to 1
- If 1, return null.
Better way
- get Instance returns the single instance whenever it is called.
- class has to keep track of the object reference - via a private static variable.
Attempt 2a
- getInstance() checks instanceCount
-If 0, creates a new instance and saves it to a private static attribute and increments instanceCount
-returns the reference to the object.
Attempt 2b
-Don't need an instance counter
-Initialize instance when declared.
-This attempt is a design pattern for singleton classes.
Singleton Design Pattern
-private static instance holder --> creates and holds the only instance
-public static getInstance() -> returns instance
-private constructor -> prevents instances from being created by the client.
-no mutator for the instance holder, instance is final.
Welcome
Welcome to my blog.
I will be updating this blog with lecture notes and miscellaneous information for:
Prof. A. Eckford, Section-Z: MWF at 10:30 in R S137
Feel free to leave me a message or send me an e-mail at producap@yorku.ca.
Here is a link for MyMail, Hotmail, G-mail, and PRISM login.
On that note, you must have a PRISM account in order to write the lab tests.
If you have not created a PRISM account yet (e.g., if you received transfer credit for 1020), see the lab monitor in CSEB 1006 as soon as possible.
I will also leave the blog open to comments, etc. much like the CSE forums.
Thanks for visiting.
Phil.
Sunday, January 27, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment