Saturday, May 15, 2010

Confirm your feed

Feed My Inbox - Confirm your feed

Greetings,

You have chosen to receive email updates from the following sites and/or feeds:

About.com Focus on Java

To CONFIRM your subscription, please click the link below:

Confirm Subscription
To cancel your subscription, please click the link below:

Uhh no thanks!
To create an account and manage your sites, click the link below:

Create Account
2 Quick Tips
1. Add updates@feedmyinbox.com to your address book, so that future emails from us don't end up in your spam filter.
2. Once confirmed, you can expect to receive your first update in 24 hours or so if the feed has any entries.
QUESTIONS? Feed My Inbox
If you have any, simply reply to this email for help.
Your friends at,
FeedMyInbox.com
help@FeedMyInbox.com

Confirm your feed

Feed My Inbox - Confirm your feed

Greetings,

You have chosen to receive email updates from the following sites and/or feeds:

Javalobby - The heart of the Java developer community

To CONFIRM your subscription, please click the link below:

Confirm Subscription
To cancel your subscription, please click the link below:

Uhh no thanks!
To create an account and manage your sites, click the link below:

Create Account
2 Quick Tips
1. Add updates@feedmyinbox.com to your address book, so that future emails from us don't end up in your spam filter.
2. Once confirmed, you can expect to receive your first update in 24 hours or so if the feed has any entries.
QUESTIONS? Feed My Inbox
If you have any, simply reply to this email for help.
Your friends at,
FeedMyInbox.com
help@FeedMyInbox.com

Confirm your feed

Feed My Inbox - Confirm your feed

Greetings,

You have chosen to receive email updates from the following sites and/or feeds:

TheServerSide : Thread List - News

To CONFIRM your subscription, please click the link below:

Confirm Subscription
To cancel your subscription, please click the link below:

Uhh no thanks!
To create an account and manage your sites, click the link below:

Create Account
2 Quick Tips
1. Add updates@feedmyinbox.com to your address book, so that future emails from us don't end up in your spam filter.
2. Once confirmed, you can expect to receive your first update in 24 hours or so if the feed has any entries.
QUESTIONS? Feed My Inbox
If you have any, simply reply to this email for help.
Your friends at,
FeedMyInbox.com
help@FeedMyInbox.com

Confirm your feed

Feed My Inbox - Confirm your feed

Greetings,

You have chosen to receive email updates from the following sites and/or feeds:

The Java Posse

To CONFIRM your subscription, please click the link below:

Confirm Subscription
To cancel your subscription, please click the link below:

Uhh no thanks!
To create an account and manage your sites, click the link below:

Create Account
2 Quick Tips
1. Add updates@feedmyinbox.com to your address book, so that future emails from us don't end up in your spam filter.
2. Once confirmed, you can expect to receive your first update in 24 hours or so if the feed has any entries.
QUESTIONS? Feed My Inbox
If you have any, simply reply to this email for help.
Your friends at,
FeedMyInbox.com
help@FeedMyInbox.com

Java Singleton Design Pattern

Copied from http://www.javabeginner.com/learn-java/java-singleton-design-pattern

Java has several design patterns Singleton Pattern being the most commonly used. Java Singleton pattern belongs to the family of design patterns, that govern the instantiation process. This design pattern proposes that at any time there can only be one instance of a singleton (object) created by the JVM.

The class’s default constructor is made private, which prevents the direct instantiation of the object by others (Other Classes). A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.

One such scenario where it might prove useful is when we develop the help Module in a project. Java Help is an extensible, platform-independent help system that enables authors and developers to incorporate online help into applications.

Singletons can be used to create a Connection Pool. If programmers create a new connection object in every class that requires it, then its clear waste of resources. In this scenario by using a singleton connection class we can maintain a single connection object which can be used throughout the application.

Implementing Singleton Pattern

To implement this design pattern we need to consider the following 4 steps:

Step 1: Provide a default Private constructor
public class SingletonObjectDemo {

// Note that the constructor is private
private SingletonObjectDemo() {
// Optional Code
}
}

Step 2: Create a Method for getting the reference to the Singleton Object

public class SingletonObjectDemo {

private static SingletonObject singletonObject;
// Note that the constructor is private
private SingletonObjectDemo() {
// Optional Code
}
public static SingletonObjectDemo getSingletonObject() {
if (singletonObject == null) {
singletonObject = new SingletonObjectDemo();
}
return singletonObject;
}
}

We write a public static getter or access method to get the instance of the Singleton Object at runtime. First time the object is created inside this method as it is null. Subsequent calls to this method returns the same object created as the object is globally declared (private) and the hence the same referenced object is returned.

Step 3: Make the Access method Synchronized to prevent Thread Problems.

public static synchronized SingletonObjectDemo getSingletonObject()

It could happen that the access method may be called twice from 2 different classes at the same time and hence more than one object being created. This could violate the design patter principle. In order to prevent the simultaneous invocation of the getter method by 2 threads or classes simultaneously we add the synchronized keyword to the method declaration

Step 4: Override the Object clone method to prevent cloning

We can still be able to create a copy of the Object by cloning it using the Object’s clone method. This can be done as shown below

SingletonObjectDemo clonedObject = (SingletonObjectDemo) obj.clone();

This again violates the Singleton Design Pattern’s objective. So to deal with this we need to override the Object’s clone method which throws a CloneNotSupportedException exception.

public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}

The below program shows the final Implementation of Singleton Design Pattern in java, by using all the 4 steps mentioned above.

class SingletonClass {

private static SingletonClass singletonObject;
/** A private Constructor prevents any other class from instantiating. */
private SingletonClass() {
// Optional Code
}
public static synchronized SingletonClass getSingletonObject() {
if (singletonObject == null) {
singletonObject = new SingletonClass();
}
return singletonObject;
}
public Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
}

public class SingletonObjectDemo {

public static void main(String args[]) {
// SingletonClass obj = new SingletonClass();
                //Compilation error not allowed
SingletonClass obj = SingletonClass.getSingletonObject();
// Your Business Logic
System.out.println("Singleton object obtained");
}
}


Download
SingletonObjectDemo.java

Another approach

We don’t need to do a lazy initialization of the instance object or to check for null in the get method. We can also make the singleton class final to avoid sub classing that may cause other problems.

public class SingletonClass {

private static SingletonClass ourInstance = new SingletonClass();
public static SingletonClass getInstance() {
return singletonObj;
}
private SingletonClass() {
}
}

In Summary, the job of the Singleton class is to enforce the existence of a maximum of one object of the same type at any given time. Depending on your implementation, your class and all of its data might be garbage collected. Hence we must ensure that at any point there must be a live reference to the class when the application is running.

Friday, May 14, 2010

Computer Flip Flop

Hups....Finally I created my own blog. I had thought about creating blog since seven years ago. It took long time until I have one. Why did it take so long time? Why today, 2010-05-14, did I create my blog? And why its name is Computer Flip Flop? Just keep reading to find them out. :)

I experienced with the difficulties and found obstacles when I wanted to create my own blog. I thought that I have to do code computer programming. It happened because I had less information. I did not browsed, read books or articles. If I could open my eyes and mind, there are many resources, group discussions, threads which talks about how to create your own blog. Next, I really faces the problem about how to express your mind into your writing. This is really big trouble. I do not know what I will write. How to write your idea, how to describe it. Moreover, because there was not reason why I have to have my own blog, therefore I did not spare my time to do try to create blog and write my first posting.

This is my first posting. I tried hard to start. I had to do my first posting, so that I will have the others. I believe with this, The first step is difficult, but It should be done therefore the next steps will be. This is my first step, and the next posting will follow. I didn't want the sun goes down without seeing my ideas, mind, thoughts is painted in my blog wall.

Why its name Computer Flip Flop. First of all, I will draw lines, triangles, squares and any shape of computer world in my blog. Next, Flip Flop is what page you go forward and or go back you will find the same idea, it is computer.

I hope this blog will find and have its community, therefore it will get the benefits and knowledge. I thank to all visitors and its community for keeping reading my posting. Hope to get feedback from all of you. Let begin.

Cheers.

Jakarta, 2010-05-14

Ryo Saeba