180 likes | 354 Views
Car Rental DB Explained. Car Rental DB Explained. The application demonstrates the Model-View-Controller (MVC) pattern The user can Add a car to a car catalog View the car catalog Use a database to save/load car data. Car Rental DB Explained. Application design:
E N D
Car Rental DB Explained • The application demonstrates the Model-View-Controller (MVC) pattern • The user can • Add a car to a car catalog • View the car catalog • Use a database to save/load car data RHS – 2009
Car Rental DB Explained • Application design: • Follows MVC pattern, but with a few compromises • Uses interface classes for main parts of the functionality • Database functionality is isolated to the model implementation RHS – 2009
Car Rental DB Explained CarRentalViewInterface CarRentalView View CarRentalApp CarRentalCarListDialog CarRentalAddCarDialog Auto-generated classes (but we modify the View class a bit) CarRentalControllerInterface Controller CarRentalController CarRentalModelInterface Car Model Interface classes CarCatalog CarRentalDBModel RHS – 2009
Car Rental DB Explained • Dependencies – View layer • View classes (the dialog classes) know the controller, but only through an interface • View classes know the model, but only through an interface • View classes know the domain model (the Car class) – this is not quite in agreement with the pure MVC pattern! RHS – 2009
Car Rental DB Explained • Dependencies – Controller layer • Controller class knows the model, but only through an interface • Controller class knows all current view objects, but only through an interface. This enables the controller to notify all view objects of changes to the model RHS – 2009
Car Rental DB Explained • Dependencies – Model layer • Model class does not have any dependencies to other classes • All references to the database are isolated to the implementation of the model interface • This is why we can easily change from using a file to using a database, since we only have to change the model implementation RHS – 2009
Car Rental DB Explained • Start-up • When starting the application, we must choose a specific implementation of the controller and model – this is done in the CarRentalView constructor • Since no dialogs are open at the start-up, we do not create any view objects RHS – 2009
Car Rental DB Explained // Code from CarRentalView.java if (controllerInterface == null) { controllerInterface = new CarRentalController(); controllerInterface.setModel( new CarRentalDBModel()); } Choosing a specific controller implementation Choosing a specific model implementation RHS – 2009
Car Rental DB Explained • Opening a dialog • Create the actual dialog object • Call addView on the controller, so the controller is aware of the dialog object, i.e. a view implementation • Make the dialog visible RHS – 2009
Car Rental DB Explained // Code from CarRentalView.java private void jMenuAddCarActionPerformed(…) { CarRentalAddCarDialog dlg = new CarRentalAddCarDialog(null, false); controllerInterface.addView(dlg); dlg.setVisible(true); } Now the controller knows that this dialog is open RHS – 2009
Car Rental DB Explained • Opening a dialog (continued) • Note that in addView, the view object is assigned a reference to both the model and the controller • Any view object can therefore communicate with both the controller and the model RHS – 2009
Car Rental DB Explained // Code from CarRentalController.java public void addView( CarRentalViewInterface viewInterface) { viewInterface.setModel(modelInterface); viewInterface.setController(this); viewList.add(viewInterface); } RHS – 2009
Car Rental DB Explained • Adding a Car – dialog part • Extract the data for Car from the fields in the dialog (standard GUI code) • Call addCar on the controller interface • Clear the fields in the dialog, to make it ready for the next input RHS – 2009
Car Rental DB Explained // Code from CarRentalAddCarDialog.java public void jButtonAddActionPerformed(…) { Car aCar = extractCarInfo(); controllerInterface.addCar(aCar); clearDialog(); } RHS – 2009
Car Rental DB Explained • Adding a Car – controller part • Just forwards the call of addCar to the model • Also notifies other views that the model is to be changed – this may prompt the other views to get new data from the model RHS – 2009
Car Rental DB Explained • Deviations from ”pure” MVC design • The Car object is created in the view class, meaning the view class knows about a domain model object • Alternatively, the addCar method should be replaced with a method containing the data itself as parameters, not the Car object • The Car object can then be created in the controller or model layer instead RHS – 2009
Car Rental DB Explained • Adding a Car – model part • The new Car object is added to the car catalog, which is stored in memory • Note that the Car object is not put into the database immediately • Data is only saved to the database when the user wants it, by choosing ”save” RHS – 2009