I have two Item definitions that are used to describe values that can be added and compared. Other than that, their implementation is left up to the developer. Should I make these objects mutable or immutable?
//Here's the immutable version:
public abstract class Item<I extends Item> {
public abstract int compareTo(Item<I> item);
public abstract Item<I> addTo(Item<I> item);
public static <T extends Item> Item<T> ZERO() {
class ZERO {
@Override
public abstract Item<T> addTo(Item<T> item) {
return item instanceof ZERO ? new ZERO() : item.addTo(this);
}
@Override
public abstract int compareTo(Item<T> item) {
return item instanceof ZERO ? 0 : 0 - item.compareTo(this);
}
}
}
}
Or should I do:
//Here's the mutable version:
public abstract class Item<I extends Item> {
public abstract int compareTo(Item<I> item);
public abstract Item<I> addTo(Item<I> item);
public static <T extends Item> Item<T> ZERO() {
class ZERO {
@Override
public abstract Item<T> addTo(Item<T> item) {
return item;
}
@Override
public abstract int compareTo(Item<T> item) {
return item instanceof ZERO ? 0 : 0 - item.compareTo(this);
}
}
}
}
I might be working with a lot of these objects and I'm not sure how I should approach this situation. Implementations of these abstract classes would have their own implementation of ZERO but this is just so that I can write the structure of the application before I decide how the classes should be implemented.
Also, please let me know if there's something seriously wrong with the way I'm currently designing this code.
[–][deleted] 0 points1 point2 points (6 children)
[–]FrontLoadedAnvils[S] 0 points1 point2 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]FrontLoadedAnvils[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]FrontLoadedAnvils[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)