Question:
Java design pattern question regarding factory and singleton design patterns?
Woot woot
15 years ago
Hello

so my question is what is the singleton design pattern? I've googled it many times and my textbook is useless. The definition in the book is that the "The singleton design pattern is a case in which the object returned is the one and only instance of the class." 'm not sure exactly what this means. Does it mean that only one object is returned from each class?

My assignment is to convert my current code and to have each class (except Main) follow the singleton design pattern. If someone could explain it to me and preferably use and modify one of my classes that would be really great. Below is my code

import java.io.*;
import java.util.*;

/*
This program will prompt a user for a txt file and compares that text to a
dictionary list */

class SearchFile {
private String fFileName;
public SearchFile(String FileName)
{
fFileName = FileName;
}
public void find(Database db) {
try {

FileReader fileReader = new FileReader(fFileName);
BufferedReader bufRead = new BufferedReader(fileReader);
int counting =0;
try{
String line = null;
line = bufRead.readLine();
while (line != null) {
//line = line.toLowerCase();
String[] strArr = line.split(" ");
for(int i =0; i < strArr.length; i++ ) {
if (strArr[i] == null || strArr[i].equals("")) continue;
if (!db.strFound(strArr[i].toLowerCase())) {
System.out.println(strArr[i]);
counting ++;
}
}
line = bufRead.readLine();

}
System.out.println("Number of words not in dictionary: " + counting);
}
finally {
bufRead.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
public int searchForStr(String search){
int Count = 0;
try {
// the text from the file is passed to the buffer reader.
BufferedReader br = new BufferedReader(new FileReader(fFileName));
String line = null;
String newline = null;

// i is counter for number of lines and j is counter for occurrences.
int i = 1;


// reads line by line until it reaches null. If the string searched is in
// the line, the line is printed. If the string is not in the text,
// an error message is displayed. else statement prints out occurrences.
while ((line = br.readLine()) != null) {
if (line.indexOf(search) != -1) {
newline = line.replaceAll(search, "*" + search + "*");
System.out.println( + i + ": " + newline);
Count++;
}
i++;
}
if (Count <= 0)
System.err.println(search + " was not found in the text");
else
System.out.println("\nThe number of occurrences: " +Count+ "\n" );
}
// Error message is displayed if file does not have .txt extension or
// does not exist.
catch (IllegalStateException e){
System.err.println("IllegalStateException: " + e.getMessage());
}
catch (FileNotFoundException e) {
System.err.println("FileNotFoundException: " + e.getMessage());
}
catch (IOException e){
System.err.println("IOException: " + e.getMessage());
}

return Count;
}
}

class Database {
private String fFileName;
private Hashtable> Table = new Hashtable();
public Database(String FileName)
{
fFileName = FileName;
};
public void loadDatabase(){

try {
FileReader fileReader = new FileReader(fFileName);
BufferedReader bufRead = new BufferedReader(fileReader);
try{
String line = null;
line = bufRead.readLine();
while (line != null) {
line = line.toLowerCase();
TreeSet set;
if (Table.containsKey(line.hashCode())) {
set = Table.get(line.hashCode());
set.add(line);
} else {
set = new TreeSet();
Table.put(line.hashCode(),set);
set.add(line);
}
Three answers:
Nigel
15 years ago
A singleton is that only one instance of a class exists and each time you ask for an object of that type, you always get the same instance back. This can be linked into the factory pattern as the factory is responsible for giving you an instance of the required class. This is where you'll always return back the same object.

It works in the same way as if you defined it as a static variable - all instances of a class share the same value.
anonymous
9 years ago
As pointed out by other three, ur concept about singleton is correct. I liked the third answer. As for its use I can't provide u sufficient verbal reasoning without thinking. But it has uses. For example a javax.sql.DataSource object. It is a standard practice to create a singleton pattern for the datasource which is used by all the classes to obtain a connection to the database. That's the practice SUN recomends. It has its advantages. If u want I can mail u some code and necessary links to articles and forum discussions. Thread safety can always be ensured.
BeerMeQuik
15 years ago
http://www.javabeginner.com/learn-java/java-singleton-design-pattern


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...