views:

176

answers:

8

Let's say I have a simple class called WebsterDictionary that has a function that can take a word and return its definition. Perhaps there is another function that can take a definition and return a word. The class is used all the time by many clients.

To facilitate the lookups, the class contains a member variable that is an in-memory Dictionary which stores the words and their associated definitions. Assume the Dictionary can never change once it is initialized -- it's constant and would not vary across instances.

Is this a good candidate for static class? I've been reading that static classes should be stateless...but this class has state (the in-memory dictionary) right?

EDIT: Also, if this does become a static class, when do I initialize the Dictionary since there would no longer be a constructor? Do I do check to see if the reference to the Dictionary is null every time one of the static methods is called?

Thanks.

A: 

You could either go Singleton or a Static class. I would probably go with a Singleton but I think it's mostly a preference issue in this particular situation.

BobbyShaftoe
A: 

A static class is appropriate when only one "instance" should ever exist, in which case the Singleton pattern may or may not be more appropriate (depending on the details). For an immutable object that you'll need multiple instances of, of course a static class is inappropriate.

Eddie
A: 

What you are looking for might be Singleton?

It is not necessary that static class need not have state (it can have static members as part of it, which can be part of its state).

shahkalpesh
+2  A: 

A static class is suitable when the functionality doesn't need to be replaceable (e.g. for tests). If you might want to use a stub or a mock, you should create an appropriate interface, and then implement it with a singleton.

Jon Skeet
Regardless of whether the implementation is a singleton or a static class, do you believe that the example I provided should not allow for multiple instances since the state would not vary between instances?
vg1890
It depends if there are other reasons for allowing multiple instances (if it makes some other aspect easier, e.g. dependency injection) - but my gut feeling is no, it shouldn't.
Jon Skeet
A: 

It sounds very much like you're describing the Monostate pattern: you would like the same WebsterDictionary to be shared by everyone. It just so happens that the WebsterDictionary is also immutable, but that is an orthogonal concern: just make it impossible to update (for example, by using a read-only wrapper).

John Feminella
+1  A: 

To expand upon others' answers, a static class or singleton is useful when you need to have only one instance of a class. This is much easier to accomplish when the data is immutable. Thus, there is a possibility that a static class is what you want to use for this. But it's not necessarily the case that it's automatically what you want to use.

My advice is to ask yourself one question: will the world come crashing down if I instantiate more than one of these objects? If so, use a singleton or static class. Otherwise, use a regular class.

Jason Baker
+1  A: 

A static class might be what you want here (see other answers). But don't make the mistake of calling your dictionary "immutable".

Immutable does not mean "can never change at runtime" in the sense that you used the phrase, because your Dictionary actually does change at runtime; after you must create it you must also add the items. Even at this point you may intend that it never change again, but it is possible to change it. Your intent is not enforced anywhere.

A true immutable object cannot change after creation, no matter how much you try. Instead, when you need a variation of the object you must create a new instance with the desired attributes.

You can think of a static class in one sense as having exactly one instance. That's probably not the best choice for a pattern where you depend on creating new instances for each state change.

Joel Coehoorn
If the class doesn't expose a way for its state to be changed, it won't vary per instance. In this case, should it be static? If it is static, when does the dictionary get initialized if there is no constructor?
vg1890
A: 

As you said the class is holding some form of global state, even if it is read only. Going the Singleton approach makes it really clear it is holding data.

If you use dependency injection you can have it inject singleton instances, instead of making all the classes have code that gets you the instance. This means the other classes won't be tied to the Singleton approach, and if you can more easily replace it when testing (combined with an interface to enable replace with test mocks).

eglasius