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?
> 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.
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
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.