Here is an example of a simple animation illustrating the use of threads and two design patterns: model-view-controller (MVC) and observer-observable. The MVC pattern is to divide the program into three parts: model, view and controller. The Model does the work and extends the observable class and implements the runnable interface. Whenever it does something that another class might be interested in, it "broadcasts" the fact to all other classes that might be interested. Note that the Model does not need to know or care anything about which other classes, if any, might be interested, so it remains completely independent of those classes. The View is interested in observing the Model class; it does this by implementing the observer interface and defining the required The Controller in this program doesn't actually have any controls. However, it does set up the GUI, create the Model and the View, and register the View as an observer of the Model. The applet html code is here. This code is available as a BlueJ project in a zip file. |