Is it OK to have a class something like this
public class Weapon {
private string name;
private int might;
// etc...
private Weapon(String name, int might) {
this.name = name;
this.might = might;
}
public static final Weapon Alondite = new Weapon("Alondite", 16);
// 100++ weapon
}
Then calling the weapon anywhere in the project, like this Weapon.Alondite
, will this create new object every time the static method member called?
Or should I do like this, to ensure the object only created once
public class Weapon {
private string name;
private int might;
// etc...
private Weapon(String name, int might) {
this.name = name;
this.might = might;
}
private static Weapon mAlondite;
public static Weapon Alondite() {
//if (mAlondite == null) {
// mAlondite = new Weapon("Alondite", 16);
// return mAlondite;
//} else {
// return mAlondite;
//}
// EDIT: as suggested by everyone
if (mAlondite == null) {
mAlondite = new Weapon("Alondite", 16);
}
return mAlondite;
}
}
Best Answer
No, your first code snippet will not create a new weapon every time you reference it.
The second example you posted is an example of the singleton pattern.
Based on your comment at the bottom of your first example, it seems to indicate that you will have over 100 instances of different weapons. This is a lot. You're right that you don't necessarily want to have more than one instance of the same weapon (one might for different reasons, but your examples seem to indicate singleton-ness).
You might want to consider what might happen if you needed to make another weapon type, or maybe a hundred, or a thousand. You might not want to have to recompile every single time. Otherwise, the purpose of the class might be misconstrued as an weapon type list, not the weapon itself.
Based on your example, it would seem that the 100+ weapon types only differ in name and might. If that's the case, being only different in the data, not behavior, I might consider reading them from a file or something else, and providing a means to look them up (similar to the repository pattern).
You mentioned in a comment that your weapons will have many attributes, not limited to two. I think this moves more into the favor of using an external, non-compiled data file, as opposed to being compiled into the class.
You can compare something like this
To something like
Looking at this example, can you tell which of those numbers belong to which field; is the 11 of Betite spd-bonus or def-bonus? I certainly can't tell. I could go to the constructor and see what it is (it's def-bonus), but I don't really want to do that. If there's going to be 100+, it would be about this long
Do you want to release a new version of your program every time you make a change that doesn't really affect functionality? I don't. I'd much rather just have some sort of update checker that downloads the latest version of the data file. Functionality never changes, but suddenly you can add thousands more weapons. Or even removing weapons; if you add a weapon that ends up being more over-powered than anticipated, you can easily just remove the entry from the file; as opposed to building your code again, testing it, and making it available for your clients to download.