EXERCISE 9 – IMPROVE REUSABILITY AND MAINTAINABILITY USING DESIGN
PATTERNS-9034-GANESAN.P
DESIGN PATTERNS TO IMPLEMENT:
1. Layered Architecture
2. Client-Server Pattern
3. Repository Pattern
1. LAYERED ARCHITECTURE
controller → contains business logic
dao → handles database interactions
model → entity/data classes
view → Swing UI screens
package [Link];
import [Link];
import [Link];
import [Link];
public class CarController {
private CarDAO carDAO;
public CarController() {
[Link] = new CarDAO();
}
public List<Car> getAvailableCars() {
return [Link]();
}
public boolean addCar(String brand, String model, int year, double pricePerDay) {
Car car = new Car(0, brand, model, year, pricePerDay, true); // ID is auto-generated by DB
return [Link](car);
}
2. CLIENT-SERVER PATTERN
package [Link];
import [Link];
import [Link];
import [Link].*;
import [Link].*;
import [Link];
public class AdminDashboard extends JFrame {
private CarController carController;
public AdminDashboard() {
carController = new CarController();
setTitle("Admin Dashboard - Car Rental System");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnAddCar = new JButton("Add New Car");
[Link](e -> showAddCarDialog());
JButton btnViewCars = new JButton("View Available Cars");
[Link](e -> showAvailableCars());
JPanel panel = new JPanel(new GridLayout(2, 1));
[Link](btnAddCar);
[Link](btnViewCars);
add(panel);
setVisible(true);
}
private void showAddCarDialog() {
JTextField tfBrand = new JTextField();
JTextField tfModel = new JTextField();
JTextField tfYear = new JTextField();
JTextField tfPricePerDay = new JTextField();
Object[] fields = {
"Brand:", tfBrand,
"Model:", tfModel,
"Year:", tfYear,
"Price Per Day:", tfPricePerDay,
};
int result = [Link](this, fields, "Add New Car",
JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
String brand = [Link]();
String model = [Link]();
int year = [Link]([Link]());
double pricePerDay = [Link]([Link]());
boolean success = [Link](brand, model, year, pricePerDay);
if (success) {
[Link](this, "Car added successfully!");
} else {
[Link](this, "Failed to add car!", "Error",
JOptionPane.ERROR_MESSAGE);} } }
private void showAvailableCars() {
List<Car> cars = [Link]();
StringBuilder sb = new StringBuilder("Available Cars:\n");
for (Car car : cars) {
[Link]([Link]()).append("\n");
[Link](this, [Link]()); }}
4. REPOSITORY PATTERN
package [Link];
import [Link];
import [Link];
import [Link];
public class DatabaseConnector {
private static final String DB_URL = "jdbc:mysql://localhost:3306/car_rental";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "Sql@13677";
public DatabaseConnector() { }
public static Connection connect() throws SQLException {
return [Link]("jdbc:mysql://localhost:3306/car_rental", "root",
"Sql@13677"); }
public static void main(String[] var0) {
try {
Connection var1 = connect();
[Link]("Connection successful!");
[Link]();
} catch (SQLException var2) {
[Link](); } }}
Summary Table
Design Pattern Used In ATM System Benefit
Layered Separated UI, Logic Clean code, easier
Architecture (ATMService), DB maintenance
Client-Server Java Swing UI ↔ MySQL via Decouples interface
Pattern JDBC and storage
Repository [Link] handles DB Reusable DB logic,
Pattern operations easy to test
Result:
Thus, the application of appropriate design patterns was successfully implemented to enhance
the reusability and maintainability of the software system