Presentation is loading. Please wait.

Presentation is loading. Please wait.

Car Rental DB Explained. RHS – 2009 2 Car Rental DB Explained The application demonstrates the Model- View-Controller (MVC) pattern The user can –Add.

Similar presentations


Presentation on theme: "Car Rental DB Explained. RHS – 2009 2 Car Rental DB Explained The application demonstrates the Model- View-Controller (MVC) pattern The user can –Add."— Presentation transcript:

1 Car Rental DB Explained

2 RHS – 2009 2 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

3 RHS – 2009 3 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

4 RHS – 2009 4 Car Rental DB Explained CarRentalView CarRentalViewInterface CarRentalCarListDialogCarRentalAddCarDialog CarRentalApp CarRentalControllerInterface CarRentalController CarRentalModelInterface CarRentalDBModel Car CarCatalog Auto-generated classes (but we modify the View class a bit) Interface classes View Controller Model

5 RHS – 2009 5 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!

6 RHS – 2009 6 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

7 RHS – 2009 7 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

8 RHS – 2009 8 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

9 RHS – 2009 9 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

10 RHS – 2009 10 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

11 RHS – 2009 11 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

12 RHS – 2009 12 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

13 RHS – 2009 13 Car Rental DB Explained // Code from CarRentalController.java public void addView( CarRentalViewInterface viewInterface) { viewInterface.setModel(modelInterface); viewInterface.setController(this); viewList.add(viewInterface); }

14 RHS – 2009 14 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

15 RHS – 2009 15 Car Rental DB Explained // Code from CarRentalAddCarDialog.java public void jButtonAddActionPerformed(…) { Car aCar = extractCarInfo(); controllerInterface.addCar(aCar); clearDialog(); }

16 RHS – 2009 16 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

17 RHS – 2009 17 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

18 RHS – 2009 18 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”


Download ppt "Car Rental DB Explained. RHS – 2009 2 Car Rental DB Explained The application demonstrates the Model- View-Controller (MVC) pattern The user can –Add."

Similar presentations


Ads by Google