tag:blogger.com,1999:blog-71378308722183500312024-03-12T19:27:18.392-07:00DevStageUnknownnoreply@blogger.comBlogger3125tag:blogger.com,1999:blog-7137830872218350031.post-84529404524100331242013-01-10T16:09:00.000-08:002013-01-10T16:10:28.166-08:00Design Patterns - SingletonThis 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.<br />
<br />
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.<br />
<br />
This is how a singleton looks like implemented in Java:<br />
<br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;">class Singleton {</span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"> private static Singleton theOneAndOnly;</span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"><br /></span></span>
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"> private Singleton();</span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"><br /></span></span>
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"> public Singleton getInstance() {</span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"> if (theOneAndOnly == null) {</span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"> theOneAndOnly = new Singleton();</span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"> }</span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"> return theOneAndOnly;</span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"> }</span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"> // other useful methods</span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;">}</span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"><br /></span></span>
<br />
As it can be noticed, the only way the single instance can be accessed is by using the <span style="color: #cc0000;">getInstance()</span> method. Inside this, the constructor is visible and, therefore, the unique object can be created for the first access.<br />
<br />
A singleton cannot be subclassed because the constructor is declared <span style="color: #cc0000;">private</span>.<br />
<br />
Another fact to remember is that starting with Java 6, the following alternative can be used to implement a singleton.<br />
<br />
<br />
<br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;">public enum Singleton {</span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"> THEONEANDONLY; </span></span><br />
<br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"> // other useful methods</span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;">}</span></span><br />
<br />
<span style="font-family: inherit;">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.</span>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7137830872218350031.post-73226488429003540772012-11-18T12:30:00.001-08:002012-11-18T15:03:01.757-08:00How to install Eclipse on UbuntuUsually, the Eclipse IDE provided by the Software Manager on Ubuntu systems is not up to date. One fast way to use the <b>latest</b> Eclipse IDE is to actually download it from the official website and place it locally. <span style="font-family: inherit;">More specifically</span>, on Ubuntu systems, the installation would have the following steps:<br />
<br />
1. Depending on the platform (x86 or amd64), <a href="http://www.eclipse.org/downloads/">download</a> the desired (latest) Eclipse IDE.<br />
Currently, the newest standard version for "Eclipse IDE for Java Developers" is:<br />
<span style="font-family: "Courier New",Courier,monospace;"><i>eclipse-java-juno-SR1-linux-gtk-x86_64.tar.gz</i></span><br />
<br />
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:<br />
<br />
<span style="color: #cc0000;">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.</span><br />
<br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;">$ mkdir ~/opt</span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;">$ cp ~/Downloads/eclipse-java-juno-SR1-linux-gtk-x86_64.tar.gz ~/opt</span></span><br />
<br />
3. Extract it, then remove the archive:<br />
<br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"><span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;">$ </span></span>cd ~/opt </span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"><span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;">$ </span></span>tar xvf eclipse-java-juno-SR1-linux-gtk-x86_64.tar.gz</span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"><span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;">$ </span></span>rm eclipse-java-juno-SR1-linux-gtk-x86_64.tar.gz</span></span><br />
<br />
4. Set the heap memory to 1024 MB in the <span style="font-family: "Courier New",Courier,monospace;">eclipse.ini</span> file to avoid any future problems<span style="font-family: inherit;">:</span> <br />
<br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"><span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;">$ </span></span>cd ~/opt/eclipse</span></span><br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"><span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;">$ </span></span>gedit eclipse.ini</span></span><br />
<br />
<span style="background-color: white;"><span style="font-family: "Courier New",Courier,monospace;">eclipse.ini:</span> </span><br />
<span style="background-color: white;">----------------------------------------------------------------------------------------------------------------------------------</span><br />
<span style="background-color: white;"><span style="font-size: x-small;"><span style="font-family: "Courier New",Courier,monospace;">-startup<br />plugins/org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar<br />--launcher.library<br />plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.1.200.v20120522-1813<br />-product<br />org.eclipse.epp.package.java.product<br />--launcher.defaultAction<br />openFile<br />-showsplash<br />org.eclipse.platform<br />--launcher.XXMaxPermSize<br />256m<br />--launcher.defaultAction<br />openFile<br />-vmargs<br />-Dosgi.requiredJavaVersion=1.5<br />-Dhelp.lucene.tokenizer=standard<br />-XX:MaxPermSize=256m<br />-Xms40m<br />-Xmx512m </span></span></span><br />
<span style="background-color: white;">----------------------------------------------------------------------------------------------------------------------------------</span><br />
<br />
<span style="font-family: inherit;">C</span>hange the last line from "<span style="font-family: "Courier New",Courier,monospace;">-Xmx512m</span>" to "<span style="font-family: "Courier New",Courier,monospace;">-Xmx1024m</span>" to specify the maximum range of the heap memory.<br />
<br />
<span style="color: #cc0000;">While Eclipse works fine without this setting, certain plug-ins can produce memory issues, especially in larger projects.</span><br />
<br />
5. Launch Eclipse for the first time:<br />
<br />
<span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;"><span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;">$ </span></span>~/opt/eclipse/eclipse -clean</span></span><br />
<br />
6. Launch Eclipse at any time using:<br />
<br />
<span style="font-family: "Courier New",Courier,monospace;"><span style="background-color: #d0e0e3;"><span style="background-color: #d0e0e3;"><span style="font-family: "Courier New",Courier,monospace;">$ </span></span>~/opt/eclipse/eclipse</span></span>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-7137830872218350031.post-48552702390093777882012-10-21T13:45:00.001-07:002012-11-18T12:32:11.398-08:00Hello!This blog is about programming in general, tricky techniques and methods as well as presenting tools and technologies. Enjoy!Unknownnoreply@blogger.com0