/** * The class that "represents the information (the data) of the application * and the business rules used to manipulate the data." [Wikipedia] * * This model is a trivial model - just a number that goes up or down * randomly over time. * * @author David Mutchler. * Created October 18, 2008. */ public class Model implements Runnable { private static final double DELTA = 0.01; private double data; /** * Sets the initial data value to 0. */ public Model() { this.data = 0; } /** * Repeatedly runs one step of the simulation. */ public void run() { while (true) { this.runOneStep(); } } /** * Randomly increases or decreases the data value by a small amount. */ private synchronized void runOneStep() { if (Math.random() < 0.5) { this.data += Model.DELTA; } else { this.data -= Model.DELTA; } } /** * Returns the data value. * * @return the data value */ public synchronized double getData() { return this.data; } }