Woot woot
15 years ago
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
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
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);
}