how to write a class of Dictionary using singleton pattern

please anyone help me ?
[30 byte] By [billyzhenga] at [2007-10-2 20:14:42]
# 1
You have to provide actual requirements, take your best shot, show your work, and ask an actual question first.
jverda at 2007-7-13 22:56:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

public class Dictionary

{

public Dictionary(String w, String d)

{

word = w;

definition = d;

}

public String getWord()

{

return word;

}

public String getDefinition()

{

return definition;

}

private String word;

private String definition;

}

is this class using singleton pattern?

billyzhenga at 2007-7-13 22:56:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

> is this class using singleton pattern?

What is your understanding of the singleton pattern? Have you studied it? Can you explain how this class fits that pattern?

When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

jverda at 2007-7-13 22:56:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4

public class Singleton {

private static Singleton instance;

private String word ;

private String definition;

private Singleton() {

String w;

String d;

word = w;

definition = d;

}

public String getWord()

{

return word;

}

public String getDefinition()

{

return definition;

}

public Singleton getSingleton() {

if (instance == null) instance = new Singleton();

return instance;

}

}

i read several times, this is the best i can do . i felt that is wrong , i am still comfuse about it

billyzhenga at 2007-7-13 22:56:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

Well, yeah, that's a singleton. But there are some problems, and I'm not sure what you're really trying to accomplish--what you mean by "a dictionary that uses the singleton pattern."

First, if this class represents a dictionary, call it Dictionary, not singleton.

Second, assuming it does represent a dictionary, it does not make sense for a dictionary to have a member var for word and one for definition. The dictionary would have a whole bunch of words and defs, probably in a map.

Rather than "create a dictionary that uses the singleton pattern," perhaps you should first just create a dictionary class. Decide what operations this class will provide and what data it needs to do those operations. Then, once that class works, you might go, "Hmmm.... There's no reason to have a bunch of these things floating around. I'll make this a singleton," and just change a couple pieces to accomodate that.

jverda at 2007-7-13 22:56:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6
thanks you very much from billy!!!!this is good help
billyzhenga at 2007-7-13 22:56:53 > top of Java-index,Java HotSpot Virtual Machine,Specifications...