In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. While this concept is also sometimes generalized to restrict the instance to a specific number of objects, in most cases, I tend to have it instantiated only once and used by the entire system. A good use for this would be loading property values and retrieving the class that holds those properties. Here is a sample code for a singleton class.

This class is hooked into a jar file and loads all properties found in the package com/property/. Once we call the class’ getInstance() method, a static object will be called that holds all properties found in the mail.properties file. This way, whenever we call it to retrieve a property, the object will only be instantiated once and called as many times.

To retrieve a property, you can call it like this:

Related Posts Plugin for WordPress, Blogger...