![]() ![]()
Last edited - March / 99 |
-- Hear and forget; see and remember; do and understand...
the model-view-controller (MVC) design pattern
the heartbeat modelOur model of the heart (source) will be a pretty simple loop : public void run() { while (running) { numberOfBeats++; updateObservers(); try { Thread.sleep(SLEEP_DURATION * excitementLevel); } catch (InterruptedException e) { // do nothing if interrupted } } }
Each time through the loop, we increment the number of beats, and continue
to loop until public void stopHeart() { running = false; }
Likewise, the model provides a method for
adjusting the public void adjustExcitementLevel(long delta) { excitementLevel += delta; if (excitementLevel < 1) { excitementLevel = 1; } } These are the only methods available to the controller objects. These methods define the model's API and there are no other ways of altering the model's state.
The views are updated via the Let's look closely at the controller object now. |