Friday 10 October 2014

Singleton Pattern Example in Java

Structure of the Problem Requirements 

In this problem we will learn how to implement Singleton design pattern in Java.you are required to implement a program demonstrating the use of Singleton Design Pattern. A class Whiteboard implemented as a Singleton that records messages posted by different objects (senders) in an array (or vector or any other collection object). This class will have two method addMessage(String mes) that records the mes in the collection (on the next available location in the collection) and String readMessage(int i) that returns the message at the i_th location in the collection (Of course the Singleton object must have a getInstance() method in it) Two other classes Reader and Sender, read and post messages on the Whiteboard respectively. Of course the Reader will have a read method and the Sender will have a post method.





SOURCE CODE 



import java.util.*;

public class SingletonTask{
public static void main(String [] args)
{
Whiteboard wb = Whiteboard.getInstance();
Reader r1 = new Reader();
Reader r2 = new Reader();
Reader r3 = new Reader();

Sender s1 = new Sender();
Sender s2 = new Sender();
Sender s3 = new Sender();
Sender s4 = new Sender();
Sender s5 = new Sender();

s1.post("message from Sender 1");
s2.post("message from Sender 2");
s3.post("message from Sender 3");

System.out.println(r1.read(3));

s4.post("message from Sender 4");
s5.post("message from Sender 5");
s2.post("message from Sender 2");

System.out.println(r2.read(5));

s1.post("message from Sender 1");

System.out.println(r3.read(7));
System.out.println(r2.read(1));
}


};

class Whiteboard{
private static Whiteboard instance;
private ArrayList<String> messages;

private Whiteboard() {
messages = new ArrayList<String>();
}

public void addMessage(String msg)
{
messages.add(msg);
}

public String readMessage(int i)
{
if(i <= messages.size())
{
String msg = messages.get(i-1);
messages.remove(i-1);
return msg;
}
return "Error: Out of range!";
}

public static Whiteboard getInstance(){
if(instance == null)
instance = new Whiteboard();

return instance;
}
}

class Reader{
private Whiteboard whiteboard;

public Reader()
{
whiteboard = Whiteboard.getInstance();
}

public String read(int i)
{
return whiteboard.readMessage(i);
}
}

class Sender{
private Whiteboard whiteboard;

public Sender()
{
whiteboard = Whiteboard.getInstance();
}

public void post(String msg)
{
whiteboard.addMessage(msg);
}
}

OUTPUT OF THE PROGRAM

Singleton Design Pattern in Java
Singleton Design Pattern in Java

Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

3 comments: Post Yours! Read Comment Policy!▼
Important Note:
We have Zero Tolerance to Spam. Chessy Comments and Comments with Links will be deleted immediately upon our review.

  1. When i run this Program then there is one output in which a message display " Error Out of range ! " What is mean by this one ??

    ReplyDelete
    Replies
    1. Its mean out of range i think he is removing a message through that statement
      messages.remove(i-1);
      and after removing message you can't access to that so its out of range 4 you ... !!

      Delete
  2. But there are other Creational pattern as like SFM and FM pattern ... Can you give me example of these design patterns ??

    ReplyDelete