Suppose I have the following IntPattern class:
public class IntPattern implements IPattern
{
private int value;
// ...
public IPattern withType(IType type)
{
if (type == Type.INT) return this; // This is supposed to match an int (= primitive)
if (type.isSuperTypeOf(Type.INT)) ... // This is supposed to match a supertype of Int (Number, Object, ...) (= reference)
return null; // Invalid Type -> null
}
public void write(MethodWriter writer)
{
// do specific stuff depending on whether this is supposed to match a primitive or reference value
}
}
Basically, I have two options to implement the above requirements:
- Adding a variable that determines whether or not this
IntPatternmatches a primitive or reference value andreturn thisat.... - Adding a new class
IntRefPatternthat matches reference values and returns anew IntRefPattern(this.value)fromwithTypeat....
Which of the two versions are better in terms of performance and memory consumption? Is there a design pattern for this type of problem?
Aucun commentaire:
Enregistrer un commentaire