Thursday, January 10, 2013

Design Patterns - Singleton

This pattern is very useful in scenarios where there should be only one instance of a certain class. Some frequent examples are classes representing a database connection or the logger used in a big application, which should be unique.

The key point of this design solution is that the constructor is private, so nobody can instantiate (although they can declare) objects of this type. However, the existence of only one instance of this class is assured by a static reference to only one object of this class, internally created.

This is how a singleton looks like implemented in Java:

class Singleton {
    private static Singleton theOneAndOnly;

    private Singleton();

    public Singleton getInstance() {
        if (theOneAndOnly == null) {
            theOneAndOnly = new Singleton();
        return theOneAndOnly;
    // other useful methods

As it can be noticed, the only way the single instance can be accessed is by using the getInstance() method. Inside this, the constructor is visible and, therefore, the unique object can be created for the first access.

A singleton cannot be subclassed because the constructor is declared private.

Another fact to remember is that starting with Java 6, the following alternative can be used to implement a singleton.

public enum Singleton {

    // other useful methods

It is straightforward to check that conditions for being a singleton are verified: there is only one instance that can be accessed by classes outside of this class and the constructor is private, which means nobody can create a new instance.

Sunday, November 18, 2012

How to install Eclipse on Ubuntu

Usually, the Eclipse IDE provided by the Software Manager on Ubuntu systems is not up to date. One fast way to use the latest Eclipse IDE is to actually download it from the official website and place it locally. More specifically, on Ubuntu systems, the installation would have the following steps:

1. Depending on the platform (x86 or amd64), download the desired (latest) Eclipse IDE.
Currently, the newest standard version for "Eclipse IDE for Java Developers" is:

2. Copy this to your $HOME/opt directory, as having a personal IDE and not a shared one is always good practice in companies where more users work on the same computer:

There is also the possibility to install just one Eclipse IDE per system in /opt and keep just a personalized configuration file in one's home directory, but I personally prefer and recommend having it entirely installed in $HOME/opt.

$ mkdir ~/opt
$ cp ~/Downloads/eclipse-java-juno-SR1-linux-gtk-x86_64.tar.gz ~/opt

3. Extract it, then remove the archive:

$ cd ~/opt
$ tar xvf eclipse-java-juno-SR1-linux-gtk-x86_64.tar.gz
$ rm eclipse-java-juno-SR1-linux-gtk-x86_64.tar.gz

4. Set the heap memory to 1024 MB in the eclipse.ini file to avoid any future problems:

$ cd ~/opt/eclipse
$ gedit eclipse.ini



Change the last line from "-Xmx512m" to "-Xmx1024m" to specify the maximum range of the heap memory.

While Eclipse works fine without this setting, certain plug-ins can produce memory issues, especially in larger projects.

5. Launch Eclipse for the first time:

$ ~/opt/eclipse/eclipse -clean

6. Launch Eclipse at any time using:

$ ~/opt/eclipse/eclipse

Sunday, October 21, 2012


This blog is about programming in general, tricky techniques and methods as well as presenting tools and technologies. Enjoy!