Reputation: 3860
I'm trying to suss out object oriented design and have come across an issue I'm finding weird, and I'm not sure of a good strategy to structure my data.
The API I'm accessing provides data as follows:
<pair_name> = pair name
a = ask array(<price>, <whole lot volume>, <lot volume>),
b = bid array(<price>, <whole lot volume>, <lot volume>),
c = last trade closed array(<price>, <lot volume>),
v = volume array(<today>, <last 24 hours>),
p = volume weighted average price array(<today>, <last 24 hours>),
t = number of trades array(<today>, <last 24 hours>),
l = low array(<today>, <last 24 hours>),
h = high array(<today>, <last 24 hours>),
o = today's opening price
source: https://www.kraken.com/help/api#get-tradable-pairs
The bit I'm having trouble working out how to handle are the array(<today>, <last 24 hours>)
bits. I'd like to have a structure that conformed to this that each fits into. I'd separate them out in to Volume, TotalTrades and HighLow objects (or something like that) but the type for today/last 24 hours varies (sometimes int
sometimes double
).
I thought I'd try either this:
public abstract class DayTimeFrameData {
protected Object today;
protected Object lastTwentyFourHours;
}
or something like this:
public interface DayTimeFrameData {
Object today = null;
Object lastTwentyFourHours = null;
}
Then extending/implementing one of those for each data type. But I'm not sure that these make any sense at all.
Can someone offer me some pointers for how to go about structuring something like this, please?
Upvotes: 0
Views: 100
Reputation: 11999
An interface specifies methods but not fields, so you can't use that approach. You could use something like this:
public class DayTimeFrameData {
protected double today;
protected double lastTwentyFourHours;
}
since a double can also represent an integer. Then simply use objects of this type as fields.
public class PairInfo {
protected DayTimeFrameData volume;
protected DayTimeFrameData numberOfTrades;
protected DayTimeFrameData low;
}
However, if each of these needs its own specific methods, you may indeed make DayTimeFrameData abstract and extend it for each type. For example:
public class NumberOfTrades extends DayTimeFrameData {
/* methods and fields specific to NumberOfTrades */
}
If you'd like to restrict the types of the fields to specifically Double or Integer for specific implementations, you could use generics:
public abstract class DayTimeFrameData<T extends Number, L extends Number> {
protected T today;
protected L lastTwentyFourHours;
}
An implementation can then specify which types are allowed.
public class NumberOfTrades extends DayTimeFrameData<Integer, Double> {
/* methods and fields specific to NumberOfTrades */
}
The above would state that the today
value is an Integer, and the lastTwentyFourHours
value is a Double. If they both always need to be the same type, you can get by with one type parameter on DayTimeFrameData. Note that we do need to use the wrapper types (Double, Integer) instead of the primitive types (double, int) in this case.
Upvotes: 1