3 ms·
Well you have chosen an example that is little more than an API object in the first place. class IdealGas implements EquationOfState { private fina
by BlackFly 6y ago
Well you have chosen an example that is little more than an API object in the first place.
class IdealGas implements EquationOfState {
private final double gamma;
public IdealGas(double gamma) {
this.gamma = gamma;
}
public double energyDensity(double pressure, double internalEnergy) {
return (1 + gamma) * pressure * internalEnergy;
}
}
Why create a separate type over this class which is just a projection of its data? You can just use a JSONObject as the API object. You are already going to need some special tricks to deal with the union with other EquationOfStates on top of some out of band type field to designate which class is to be used.
You will have the same sort of boiler plate in either case. Either a `public EquationOfStateData getEOSData()` or a `public JSONObject getJSON()`. In one case you use type safety and the deserializer provides your validation messages but should still do some custom validation on top to handle mismatched unions, in the other case you perform the type check (ordinarily done by using method like `json.getDouble()` and get to give custom messages.
Choose your poison, they really aren't all that different.
- stickfigure 6y ago> Why create a separate type over this class which is just a projection of its data? Because it lets you refactor your API separately from your domain. And use the full power of the IDE/typechecker in both cases. This approach especially shines with JAX-RS; you can write fully typed tests against your API methods, which look like pure logic functions.