The Application Factory is a framework for desktop applications:
The framework uses the Abstract Factory Pattern to build applications:
Note that the Application has references to a model, view, controller and optional view panel (the content pane of the view). These components are built by the application factory provided to the Application constructor. The start method launches the application by setting the visibility of the view to true:
view.setVisible(true);
Here's the design of the framework:
The controller is essentially a smart command processor (although it doesn't support undo/redo.) In this case commands are strings sent by the action listeners: View and ViewPanel. The controller handles the "quit" command and otherwise throws an exception back to the listener, which displays an error dialog.
Here's an implementation of the View menu:
Complete the implementation of the framework.
Do as much work as possible in the framework. For example, add a fully functional file menu to the view's menu bar.
The Totaller is a simple calculator that allows users to add numbers to a running total. The total, average, and count are updated each time a number is added. The reset button sets all values back to zero:
The listeners for the "add" button and menu item prompt the user for the number to add:
The view has a menu bar:
If an unsupported command is executed:
the framework controller displays an error dialog:
public class
public
static void
main(String[] args) {
Application app = new
Application(new TotallerFactory());
app.start();
}
}
public class
TotallerFactory implements AppFactory {
public Controller makeController(Model
m) {
return new
TotalerController((Totaller)m);
}
public Model
makeModel() {
return new Totaller();
}
public View
makeView(Model m, Controller c, ViewPanel vp) {
return new
TotallerView((Totaller)m, (TotalerController)c, (TotalerPanel)vp);
}
public ViewPanel
makeViewPanel(Model m, Controller c) {
return new TotalerPanel((Totaller)m,
(TotalerController)c);
}
}
public class
Totaller extends
Model {
private double sum = 0.0;
private int count = 0;
public void add(double f) {
sum += f;
count++;
notifyViews();
}
public int getCount() {
return count;
}
public double getSum() {
return sum;
}
// an example of a derived attribute
public double getAverage()
{
return sum/count;
}
public void reset() {
count = 0;
sum = 0.0;
notifyViews();
}
}
Complete the implementation of Totaller.