Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface Function {
- public void calculate(long t);
- }
- public class ConstantFunction implements Function {
- private double constant;
- @Override
- public void calculate(long t) {
- // ...
- }
- }
- public class LinearFunction implements Function {
- private double slope;
- private double yIntercept;
- @Override
- public void calculate(long t) {
- // ...
- }
- }
- <myapp>
- <function type="ConstantFunction>
- <!-- ... -->
- </function>
- <function type="LinearFunction>
- <!-- ... -->
- </function>
- </myapp>
- XStream oxmapper = new XStream();
- oxmapper.alias("myapp", MyApp.class);
- oxmapper.alias("function", ???);
- public class FunctionFactory implements Function {
- private double constant;
- private double slope;
- private double yIntercept;
- private Class<? extends Function> concreteClass;
- @Override
- public void calculate(long t) {
- // Do nothing. This class is a workaround to limitations with XStream.
- return;
- }
- }
- oxampper.alias("function", FunctionFactory.class);
- oxmapper.aliasField("function", "type", "concreteClass");
- XStream oxmapper = getConfiguredMapper();
- MyApp app = oxmapper.fromXml("<myapp>...</myapp>");
- FunctionFactory factory = app.getFunction();
- Function concretion = factory.getConcreteClass();
- app.setFunction(concretion);
Add Comment
Please, Sign In to add comment