Category Archives: General

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.

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

Using hosts file to redirect URLs locally

There are times when you want to access machines on LAN or internet using the IP address. Hosts files in various systems can help you auto redirect logical names to the IP address. For example

127.0.0.1 mysite.com

Now whenever you will call mysite.com, you will be pointing to localhost. Similarly you can use any IP address on LAN or Internet and redirect logical names to them.

More on usage of hosts file : en.wikipedia.org/wiki/Hosts_(file)

Infosys launches Big Data Edge

Infosys has launched “Big Data Edge” tool for analyzing large amount of data and extracting information. The data analysis tool claims to find required information in 40 % lesser time compared to existing solutions. It is backed by 50 customizable dashboards and 250 built-in algorithms to provide it a world class performance.

It is good to see some innovative products coming out of India. More info available here

http://www.infosys.com/newsroom/press-releases/Pages/bigdataedge-rapid-business-insights.aspx

http://www.infosys.com/industries/financial-services/features-opinions/Pages/change-game-big-data.aspx

Android app: out of memory exception

An android app has some limitations when it comes to memory availability. Unlike a normal Java app, you do not have luxury of expanding memory your application uses, as it will be completely dependent on the device on which the app is running, memory available in the phone, number of applications running simultaneously. We will not have control over these external factors so we will need to optimize memory usage on our end.

Some of the techniques I used in one of my last projects which used a lot of images/ bitmaps and hence ran into many memory issues.

  • Use of flags: Set android:hardwareAccelerated=”true” and android:largeHeap=”true” flags in manifest file. Downside is that these flags are only available after android 3.0. More than 50% of android devices still run on gingerbread (2.3+), so this is not a very reliable solution if you are targeting a larger audience. Though worth a try for high end (3.0+) devices.
  • Memory leakage: DVM grabage collection works n principle of JVM garbage collection, that is it will reclaim the memory for an object if there is no reference to it. So make sure when you are done with an object and do not need it anymore, mark the object reference as null.
  • Use minimum space for an object: Design your classes intelligently, for example if you are handling student data and you address details will not be used frequently, create a separate class and initialize the object only when it is required. Remember, if a char can work, don;t use int or long.
  • Objects are always placed on heap: One of my colleagues was wondering that how can an object occupy space, after the method exits, if the object is declared inside a method. Remember, only references are stored in the stack for method, the actual object is always stored in the heap. So the memory will be freed only when next cycle of garbage collection occurs
  • Use Garbage collection: You can call system.gc(); to initiate garbage collection, but this only suggests JVM/DVM to run a garbage collection, you cannot force it.
  • Playing with Bitmaps/ Images: Bitmaps and images take highest amount of memory, so make sure you recycle() bitmaps as soon as you are done using it. Make sure you have removed all references (imageviews/ objects) to bitmap before calling recycle().
  • Handle Activity Stack: If the app can be used back and forth multiple times, it is better to handle the back-front flow in the code, as default back button handling in android needs to keep a tack of activity stack and uses memory for that. Simplest will be to call previous activity on back button through code and always finish current activity before moving to next.

100 Door puzzle

Puzzle: There are various versions of the puzzle, there are 100 doors (or some other object), and 100 cops (prisoners). All the doors are closed initially. Cop 1 goes and opens up all the the doors, Cop 2 goes and closes door at even number 2,4,6 etc. Cop 3 goes and alters the state of doors divisible by 3 (3,6,9), so if the door was closed it is opened and if it was open, it is closed by cop 3. Cop 4 will alter state of all the doors divisible by 4 and so on. At the end we have to find out how many doors are closed now.

Solution: Before getting to a generic solution, lets take case of few doors 1 by one. We can easily see that a door’s state is same if it was visited even number of time (initially closed than (open + close) (open + close).. ), and it is changed if the door is visited odd number of times.
Case of Door 1: Only visited once by Cop1, so the state changes to open
Case of Door 2: Visited twice by cop1 and cop2, so finally it is closed, no change in state (see it is visited even number of times)
Case of Door 3: Visited by 1 and 3- Closed
Case of Door 4: Visited by 1, 2 and 4- Open
Case of Door 5: Visited by 1 and 5- Closed
Case of Door 6: by 1, 2, 3 and 6- Closed
Case 9: 1, 3 and 9- Open
Case 10: 1,2, 5 and 10- Open

So on careful examination, figure out doors which have even number of divisors are not changing state. When will a number have odd number of divisors? A divisor pair of a number z is x*y (always 2, and hence even)

for 50: 1*50, 2*25, 5*10- Even
for 100: 1 * 100, 2*50, 4*25, 5*20, 10*10- Odd (As 10 appears twice)

So for all the perfect squares, we can see we have odd number of divisors and hence the state of the door will be changed.

Installing postgres 9

I was trying to install postgres 9 on my Ubunto 11.04 machine, but the GUI installer simply hanged. A little bit of googling provided the workaround.

Follow the steps

1. sudo apt-get install python-software-properties
2. sudo add-apt-repository ppa:pitti/postgresql
3. sudo apt-get update
4. sudo apt-get install postgresql-9.0 libpq-dev

Source
http://www.dctrwatson.com/2010/09/installing-postgresql-9-0-on-ubuntu-10-04/

Forgot MySql password?

Forgot the root password for mysql database. Here are easy steps to recover

1. Restart MySql database without password in safe mode
/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables &

2. Change root password 
mysql -u root
mysql> use mysql;
mysql> update user set password="mypassword" where User='root';
mysql> flush privileges;
mysql> quit

**please note that the password might be in encrypted form. use PASSWORD('some_pass') method 
mysql> update user set password=PASSWORD('mypassword') where User='root';

3. Restart database
/etc/init.d/mysql stop
/etc/init.d/mysql start

References: http://www.cyberciti.biz/tips/recover-mysql-root-password.html