For example, I have a class that has two strings, one of which must be set, the other of which could be null:
public class SomeClass{
private String s1;
private String s2;
...
}
I could define a constructor as follows:
public SomeClass(String s1, String s2){
if(s1 == null && s2 == null) throw new SomeKindOfException("can't both be null");
this.s1 = s1;
this.s2 = s2;
}
I would rather do something along the lines of:
public SomeClass(String s1){
this.s1 = s1;
}
public SomeClass(String s2){
this.s2 = s2;
}
Which obviously can’t work because it defines two methods that take the same number and type of parameters.
So, I would like to do something like:
public SomeClass(SomeTypeOfString s1){
this.s1 = s1;
}
public SomeClass(AnotherTypeOfString s2){
this.s2 = s2;
}
This has the added advantage that the “types” of String could validate their contents (e.g. SomeTypeOfString must be 6 chars long and AnotherTypeOfString must be between 4 and 8 chars long and only contain alpha-numeric chars.
Also, it makes it clearer, when calling a function, what data should be passed in.
So firstly, does this concept sound sensible?
In terms of implementation, you cannot extend String. You could wrap it though. The “types” of String are essentially types of String, so it would make sense to have them extend an abstract wrapper class. This could be implemented as follows:
public abstract class StringWrapper{
private final String string;
public StringWrapper(String string){
this.string = string;
}
public String getString(){
return string;
}
}
public class SomeTypeOfString extends StringWrapper{
public SomeTypeOfString(String string){
super(string);
}
}
The code for the SomeTypeOfString class looks rather silly, as all it does is define a constructor that calls the super-class’s constructor… But assuming you also add some validation in there, it looks sensible to me. Does this implementation sound sensible?
Can anyone think of a better concept to solve the basic problem, or a better implementation for the concept I outlined?
Your concept and implementation do look sensible and can even improve readability if used consistently.
For example if one “type” of string represents a product id, then you can produce this class:
Note that
equals()/hashCode()is necessary to be able to useProductIDas a key in aMapor to ensure uniqueness in aSetand similar things (apart from being generally useful).toString()can either be useful as a debug tool (in which case I’d add the class name, maybe using the neatMoreObjects.toStringHelper()approach or manually) or for rendering the product id in a way that is used on the UI (in which case the given implementation might be fine).This has several advantages:
ProductIDobject holds a valid product id (at least syntactically)ProductIDmake it very explicit what that value holds. If they accept aStringinstead it’s not so clear.Unfortunately I can’t say that I have experience with such a system in any large-scale systems (I used it plenty in smaller systems). But some disadvantages that I’ve seen and/or can imagine are this:
String, but notProductID, so you’ll need to convertStringvalues: when the user enters a value, you might need to be able to store it, even if it’s not a valid product id.String/ProductIDwith tons of conversion everywhere.From these three, I think the last one is the most severe.
If, however, this seems like overkill to you, then using factory methods instead of constructors could be a solution:
Note that I use the Guava
Preconditionsclass to shorten the argument checking code.