DEV Community

NOOB
NOOB

Posted on

LLD-1:Navigation/Route Calculation System

Navigation Route Calculation System - Strategy Pattern

This is an implementation of the Strategy design pattern for a navigation system like Google Maps.

Problem Statement

Building a backend for a mapping application where the route calculation algorithm changes based on transport mode. The system must be extensible without rewriting the core engine.

Class Diagram

        +-------------------------------------+             +----------------------------------+
        |             Context                 |             |            Interface             |
        |-------------------------------------|             |----------------------------------|
        |         NavigationService           |             |          RouteStrategy           |
        |-------------------------------------|    HAS-A    |----------------------------------|
        | - strategy: RouteStrategy           |<>---------->| + calculateRoute(src, dest)      |
        | + setStrategy(RouteStrategy)        |             +----------------------------------+
        | + buildRoute(src, dest)             |                              ^
        +-------------------------------------+                              |
                                                                             | IMPLEMENTS
                             +-----------------------------+-----------------+------------------------------+
                             |                             |                                                |
             +-------------------------------+ +-------------------------------+        +-------------------------------+
             |      Concrete Strategy 1      | |      Concrete Strategy 2      |        |      Concrete Strategy 3      |
             |-------------------------------| |-------------------------------|        |-------------------------------|
             |          CarStrategy          | |        WalkingStrategy        |        |    PublicTransportStrategy    |
             +-------------------------------+ +-------------------------------+        +-------------------------------+
Enter fullscreen mode Exit fullscreen mode

Implementation

package com.noob.lld.navigationroutecalculationsystem;

public class NavigationRouteCalculationSystem {

    /**
     * THE STRATEGY INTERFACE
     * This enforces a "Contract". Every navigation algorithm MUST have a
     * calculateRoute method.
     */
    public interface RouteStrategy {
        void calculateRoute(String source, String destination);
    }

    static class CarStrategy implements RouteStrategy {
        @Override
        public void calculateRoute(String source, String destination) {
            System.out.println("[CAR] calculation route....");
            System.out.println("-Avoiding Traffic on Highway 44");
            System.out.println("-ETA: 25 mins. Cost: $2.00 fuel");
            System.out.println("-Route" + source + " => Main Road => " + destination);
        }
    }

    static class WalkingStrategy implements RouteStrategy {
        @Override
        public void calculateRoute(String source, String destination) {
            System.out.println("[WALK] calculation route....");
            System.out.println("-Using shortcut through City Park");
            System.out.println("-ETA: 50 mins. Cost: Free");
            System.out.println("-Route" + source + " => Park Path => " + destination);
        }
    }

    static class PublicTransportStrategy implements RouteStrategy {
        @Override
        public void calculateRoute(String source, String destination) {
            System.out.println("[BUS] calculation route....");
            System.out.println("-Fetching Bus 505 schedule");
            System.out.println("-ETA: 40 mins. Cost: $1.50 ticket");
            System.out.println("-Route" + source + " => Bus Stop A => " + destination);
        }
    }

    static class StrategyFactory {
        public static RouteStrategy getStrategy(String mode) {
            return switch (mode) {
                case "CAR" -> new CarStrategy();
                case "WALK" -> new WalkingStrategy();
                case "BUS" -> new PublicTransportStrategy();
                default -> throw new IllegalArgumentException("Invalid mode: " + mode);
            };
        }
    }

    static class NavigationService {
        private RouteStrategy strategy;

        public void setStrategy(RouteStrategy strategy) {
            this.strategy = strategy;
        }

        public void buildRoute(String source, String destination) {
            if (strategy == null) {
                System.out.println("Error: No transport mode selected");
                return;
            }
            System.out.println("\n----Getting Route----");
            strategy.calculateRoute(source, destination);
            System.out.println("-----------------------");
        }
    }

    public static void main(String[] args) {
        NavigationService googleMaps = new NavigationService();
        String source = "Bangalore Airport";
        String destination = "Samsung R&D";

        // Scenario 1: User selects Car
        String selectedMode = "CAR";
        RouteStrategy strategy = StrategyFactory.getStrategy(selectedMode);
        googleMaps.setStrategy(strategy);
        googleMaps.buildRoute(source, destination);

        // Scenario 2: User switches to Walk
        selectedMode = "WALK";
        strategy = StrategyFactory.getStrategy(selectedMode);
        googleMaps.setStrategy(strategy);
        googleMaps.buildRoute(source, destination);
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)