Skip to content

Repository files navigation

Car Builder Pattern Implementation

Assignment Summary

This project implements the Builder Pattern in Java for creating Car objects. The implementation demonstrates Clean Code principles with small, focused methods, descriptive naming, and proper validation.

Key Features

  • Immutable Car class with final fields and private constructor
  • Builder pattern with fluent interface for object construction
  • Input validation with meaningful error messages
  • Clean Code principles applied throughout the implementation
  • Comprehensive examples demonstrating usage and error handling

Project Structure

assignment/
├── src/main/java/com/example/car/
│   ├── Car.java          # Main Car class with Builder pattern
│   └── App.java          # Example usage and demonstrations
├── uml/
│   └── class_diagram.puml # PlantUML class diagram
├── README.md             # This file
└── report.docx           # Project report (to be created)

How to Build

Prerequisites

  • Java Development Kit (JDK) 8 or higher
  • Java compiler (javac) and runtime (java)

Compilation

# Navigate to the project root
cd assignment

# Compile the Java files
javac -d . src/main/java/com/example/car/*.java

Execution

# Run the example application
java com.example.car.App

Example Usage

Basic Car Creation

Car sportsCar = new Car.Builder()
    .setSeats(2)
    .setEngineType("V8 Turbo")
    .setGPS(true)
    .setTripComputer(true)
    .build();

System.out.println(sportsCar);
// Output: Car{seats=2, engine='V8 Turbo', GPS=true, TripComputer=true}

Error Handling

try {
    Car invalidCar = new Car.Builder()
        .setSeats(0)  // Invalid: seats must be > 0
        .setEngineType("V6")
        .build();
} catch (IllegalStateException e) {
    System.out.println("Error: " + e.getMessage());
    // Output: Error: Seats must be greater than 0, got: 0
}

Different Configurations

// Family car
Car familyCar = new Car.Builder()
    .setSeats(7)
    .setEngineType("V6")
    .setGPS(true)
    .setTripComputer(false)
    .build();

// Luxury car
Car luxuryCar = new Car.Builder()
    .setSeats(4)
    .setEngineType("V12 Twin Turbo")
    .setGPS(true)
    .setTripComputer(true)
    .build();

// Basic car
Car basicCar = new Car.Builder()
    .setSeats(5)
    .setEngineType("4-Cylinder")
    .setGPS(false)
    .setTripComputer(false)
    .build();

UML Diagram

The class diagram can be viewed using PlantUML:

  • File: uml/class_diagram.puml
  • Online Viewer: PlantUML Online Server
  • VS Code Extension: PlantUML extension for VS Code

Clean Code Principles Applied

  • Small Functions: Each method is focused and under 20 lines
  • Single Responsibility: Each method has one clear purpose
  • Descriptive Names: Method and variable names clearly express intent
  • No Flag Arguments: Boolean parameters are avoided in favor of explicit methods
  • Validation: Input validation is separated into dedicated methods
  • Immutability: Car objects are immutable once created
  • Error Handling: Meaningful error messages with context

Recommended Commit Messages

  1. feat: implement Car class with Builder pattern
  2. feat: add input validation and error handling
  3. feat: create App.java with usage examples
  4. docs: add UML class diagram
  5. docs: create comprehensive README
  6. refactor: apply Clean Code principles
  7. test: add error handling demonstrations

Author

Student Name: Sergej Balakarev
Group: SE-2404
Course: Software Design Patterns
Date: 14.09.25

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages