Singleton Pattern #4: Difference between Singleton and Static classes

The basic differences between singleton and static classes (when we say  static class in java, it is a class with static members) in nutshell are:

1. Singleton classes can hold instance variable while static can’t
2. Singleton classes can be inherited
3. Static classes are more useful while handling the stateless data
4. At a later stage if you think that you need more instances of a singleton class, you can simply modify the class. Basically make the constructor public/ make the singleton class non-singleton, depending on your requirement
5. For simple functionalities where we need utility methods to be implemented on some data and simple return statements, static classes are of better help. For example, creating a string utility or math utility class which have methods like sqare (takes an integer say 2 and return 4 ), cube (takes 2 and simply returns 8 )

Singleton Pattern #3: Problems with Singleton, Thread-Safe Singleton Class

A side effect of using singleton class: as the constructor of the singleton class is private, you cannot create a subclass of a singleton class, but in most of the cases that is desirable. Additionally you can declare the class as final to explicitly show that the class can’t be subclassed. But in case if it is required to create a subclass of singleton class one can define the constructor of singleton class as protective instead of private.

Another point to note is that the singleton classes are not thread-safe. Simply putting it if the two threads enter the

If(instance==null)
{
//create new instance.
}

block simultaneously, it will create two different instances of the class. This is an undesirable condition and cause unwanted behavior in the application. Also note here that in case of eager instantiation this problem does not occur as the instance gets created beforehand.

Making your singleton class thread-safe is easy, you just have to make your getinstance method synchronized.

Public synchronized static singletonExample getInstance()
{
//if instance is null create else return old instance
}

Originally posted February 15, 2007

Brought back using archive.org

Singleton Pattern #2: Lazy vs Eager Instantiation for a Singleton Class

This post is in continuation to my previous post where I discussed about basics of singleton pattern. The kind of instantiation mentioned in that post, where we create an instance of singleton class only when it is called for the first time is called lazy instantiation. Another variation can be creating the instance before it is called, this kind of instantiation is called eager instantiation

Public Class singletonExample {
private static singletonExample instance=new singletonExample();
private singletonExample{
// no instantiation is possible
}

public static singletonExample getInstance()
{
return instance;
}
}

If you look carefully, here we are using

private static singletonExample instance=new singletonExample();

to initialize the object ‘instance’ rather than setting it as null initially. And in our getInstance() method we are just returning the ‘instance’ without any checks. So the advantage in this case is that we are saved making one check everytime getinstance is called. One apparent disadvantage is that in this case the instance of singletonExample will always be present whether it is called or not.

Originally posted February 13, 2007

Brought back using archive.org

Singleton Pattern #1: Basics, Definition, Usage

Singleton pattern: As the name suggests, a singleton pattern refers to a pattern where you need only a ’single’ instance of a class to be present. The examples can be windows manager where you want only one window to be active at a time or a file read write where only one method should be updating a file at a time.

Here is a skeleton example of a singleton class:

Public Class singletonExample
{
private static singletonExample instance=null;
private singletonExample{
// no instantiation is possible
}

public static singletonExample getInstance()
{
if (instance==null)
{
instance=new singletonClass();
}
return instance;
}
}

the caller class

public class singletonUser{
singletonExample singletonInstance= singletonExample.getInstance();
}

Explanation:

Here singletonExample is our singleton class. In the first line we have created an instance of this class as null. We have declared a private constructor for the class to make sure that no object of class can be created outside the class.

getInstance() method is the entry point for the class which will return the only instance present for the class. This method is marked as static as it will be called directly from other classes. In this method we check if the instance of singletonExample is already present, if yes, return that instance else create the instance and return it. This makes sure that only one instance of the class is present at any point.

In the user class, that is calling our singleton class, we just need to call our singletonExample.getinstance() method which will return us the instance of singletonExample.

Originally posted February 13, 2007 at 2:10 pm

Brought back using archive.org

How to create an effective professional PowerPoint presentation

Bringing old post back from dead courtesy archive.org

I have seen a lot of official presentations which are awfully boring and instead of conveying any message they confuse the audience. So when I had to deliver a presentation some days back, I did some research on how to create an effective PowerPoint Presentation; and here are my findings-

1. KISS your presentation. Ok, don’t create the presentation and then kiss the monitor. What I mean by KISS here is “Keep It Simple, Stupid (Sir :) )”. Yes, if you are creating a professional PowerPoint presentation, there is no place for flashy or cool looks.
2. Size of presentation should be controlled; very big presentations can bore the audience.
3. No over attracting backgrounds to the PowerPoint.
4. The background color and font color should be contrasting, so that text can be easily read.
5. Font selection: No designer fonts are recommended, simple Ariel, SanSerif, Verdana etc. should be good. Font Size should be big enough to read clearly, minimum of 22 or 24.
6. Consistency: Changing background, Fonts, Colors can be distracting.
7. Contents: Be precise; don’t give too much detail in your PowerPoint presentation. You can provide additional details separately if required; you can also provide online links in the presentation for detailed info.
8. Animations: No place for cool animations in a professional PowerPoint presentation, too many flying texts and images will distract the audience attention from the actual content. A simple Appear/disappear or similar plain animations should do.
9. Focus: Check for every slide where the default focus is. That is, it may be possible that the important information is on top-left part but the attention is occupied by some other portion as some image, useless text is displayed somewhere else.
Point to note for checking the slide’s focus

->Control font size: Font size can be very important in terms of attracting attention. The bigger the size, the more attention it will attract.
->Use lists to break the complex info in points format
->Make information appear on the screen when you need it. E.g. if there are 4 points (text or images) to be shown and you want to emphasize each point, make them appear one by one.
->Use graphs to simplify complex data.
->Use images and graphics to support your point as they can be memorized better than the words.

10. Finally take a look at your presentation as a viewer and see how others will perceive it. If you are not convinced with your slides, no way others can be influenced by the same.

Suggested readings

http://www.microsoft.com/atwork/getworkdone/presentations.mspx
http://entrepreneurs.about.com/cs/marketing/a/7sinsofppt.htm
http://sbinformation.about.com/od/sales/a/presentationtip.htm
http://headrush.typepad.com/creating_passionate_users/2005/06/kill_your_prese.html

Originally posted March 28, 2007 at 5:02 pm

archive.org- Someone is keeping a track

Came across this website today archive.org. The website claims to take snapshots of all the websites available on the internet. Initially I thought “What use is that of?”. And then for sake of fun I typed in my blog’s url. To my surprise it showed me various snapshots of my blog and to add to my surprise the first snapshot dated back to somewhere in 2005, the year when I started my blog (my blog crashed twice since then and lost a lot of posts).

Man! that brought a lot of nostalgia. I mean my first post was literally one liner-

The first post

May 20, 2005 on 11:28 pm | In General | No Comments

I am only a brain. The rest of me is appendix.

In my defense, that was 8 years back and yeah, that was the first post 🙂

Anyways, I am actually thinking it can be very useful for me. I can check the old posts and see if anything is there which is still relevant and can be re posted to my blog.  Archive(r), thanks for keeping my posts safe.

ZK framework: AJAX without JavaScript

Recently came across ZK framework which can be used to create AJAX based UI without actual JavaScript code. It claims that you do not need to be a programmer to code using ZK framework which actually uses ZUML Markup language to do create the UI. I have not used it yet but look interesting and would like to give it a try.

http://en.wikipedia.org/wiki/ZK (framework)

http://www.zkoss.org/

Why rownum will nor work for greater or equal to checks

Someone asked a question on a forum that why rownum=2 does not work for his query, so i though of sharing some details here.

Using rownum is a tricky affair. Safest bet is to use it only when you want to limit the number of results to be shown. For example rownum<2 or rownum<=5.

Why rownum=2 or rownum>2 will not work?

Read here – http://www.oracle.com/technetwork/issue-archive/2006/06-sep/o56asktom-086197.html

In summary, this is how oracle execute a query

  1. The FROM/WHERE clause goes first.
  2. ROWNUM is assigned and incremented to each output row from the FROM/WHERE clause.
  3. SELECT is applied.
  4. GROUP BY is applied.
  5. HAVING is applied.
  6. ORDER BY is applied.

rownum<=2 clause will get converted to

ROWNUM = 1
for x in
( select * from emp )
loop
    exit when NOT(ROWNUM <= 2)
    OUTPUT record to temp
    ROWNUM = ROWNUM+1
end loop
SORT TEMP

if you change exit when NOT(ROWNUM <= 2) with rownnum=2, you can see it will fail in the first run itself

So if I cannot use rownum, what can I use. Try using row_number() http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions137.htm

It works something like

SELECT last_name FROM
   (SELECT last_name, ROW_NUMBER() OVER (ORDER BY last_name) R FROM employees)
   WHERE R BETWEEN 51 and 100;

Are my online transactions secured? HTTPS to rescue

You must have wondered at times that how can my data be secured over the internet when using a website that needs sensitive data like bank account details (or how secured is a banking site).

The protocol that comes to rescue is HTTPS. Whenever we are working on a website where we want ourselves to be secure, we should make sure the url starts with https://www.mysite.com instead of http. http is hyper text transfer protocol which just specifies some rules as to how the data needs to be transferred and understood on the internet. https is http + secure or http over SSL (Secured socket layer).

If I ask you, how can I secure the data? The obvious answer is encryption. The most common type of encryption is symmetric encryption which uses a single key to encrypt and decrypt the data. That is the same key that encrypts the data will decrypt it. The problem with this kind of encryption is that if someone gets access to this key, he can create or read the data using the key. To solve this problem, there is concept of 2 key encryption. In this we have a private and public key. the beauty of this approach is that data encrypted using one key can be decrypted using second key only. On the downside this approach is a bit slow because of added complexity.

Lets quickly talk about concept of certificates as well. There are various certification authorities (CA) which can provide a certificate of authentication to a website. Whenever you are on a https website, you will see a small icon of a lock (may vary from browser to browser), on which when you click, will show you the certificate details. This will guarantee that the website is indeed what it claims to be. So for example you misspelt a link or opened a link from a spam email, looking for https and certificate will be sufficient for you to ensure authenticity of the website.

Now lets go behind the scenes and see how my websites are secured because of https.

Step 1: You click a secured url or type into your browser.

Step 2: The server listens to your request and returns back a certificate and a public key (server will never share the private key for security reasons)

Step 3: Your browser will verify this certificate (shows error in case of some doubt)

Step 3a: After verifying certificate, browser will generate a symmetric key and encrypt it using “the public key” sent by server.

Step 3b: Browser then sends this encrypted symmetric key to web server.

Step 4: Web server decrypts the data sent by browser using its private key (as this was encrypted using public key). The decrypted data is symmetric key from browser.

Step 5: Now this symmetric key will be used by both parties (browser and web server) to transfer the data (remember public/ private key is time consuming, so it is used only to initially transfer the symmetric key).

The above steps ensure that the communication between server and browser is always secured and hence you can do the online shopping or banking without fear.

Want to Install Mac OS on Windows virtual Machine

If you are addicted to windows but still want to try your hands in Mac OS, you can do that on your windows machine using a virtual machine. Here are a few resources which will help

http://www.codeproject.com/Articles/335596/How-to-install-latest-Mac-OS-X-Lion-Virtual-Machin

http://www.sysprobs.com/working-method-install-mac-107-lion-vmware-windows-7-intel-pc

http://www.sysprobs.com/vmware-workstation-8-0-8-0-1-unlocker-to-run-mac-os-x-guest-in-windows-7