Design the core of a ride-sharing app. A rider requests a ride from a pickup to a destination, the system matches a nearby driver, the ride moves through accepted, in progress and completed, a fare is worked out, and both sides get live updates.
Model riders, drivers, places and the ride itself.
The story has riders, drivers, places and trips, but no model of any of them. `Rider` and `Driver` each have a `Location`. `Location` can measure its distance to another. `Ride` ties a rider, a driver, a pickup and a drop-off together and will hold the fare. `Location` owns the distance maths in one place, so matching and pricing both reuse it. A `Ride` is its own object because it has a life of its own (stages, a fare) that outlasts one request. Coordinates on a flat plane are enough for the design. Real systems use latitude and longitude with road distance, but only `Location` would change.
Let what you can do with a ride depend on its stage.
A ride can be requested, accepted, started, finished or cancelled, and each action is only valid in some stages (you cannot start a ride nobody accepted). A `RideState` interface with the actions, and one class per stage. `Ride` holds its current state and forwards each action. A state allows the actions that make sense and moves the ride to the next state. This is the **State** pattern: illegal moves are refused by the state itself, with no scattered `if` checks. The lifecycle is visible: Requested to Accepted to InProgress to Completed. Cancelling is allowed in a few states with different consequences (free before pickup, a fee after). Each state decides, which keeps the rules next to the stage they belong to.
Find a driver by a rule, behind one entry point.
A `Ride` exists, but nobody finds a driver for it, and callers would have to juggle riders, drivers and states themselves. A `MatchingStrategy` picks a driver for a pickup location. `NearestDriverStrategy` chooses the closest available one. `RideService` is the entry point: `requestRide()` creates the ride, asks the strategy for a driver and reserves that driver. Matching is a rule that changes (nearest, highest rated, fewest rides today), so it is a **Strategy**. `RideService` is a **Facade**, so the app talks to one class. Reserving the driver in the same step as choosing them stops two rides taking the same driver. A linear scan over all drivers is fine for a demo. At scale, keep drivers in a spatial index (grid or geohash) so the strategy only looks at nearby ones, without changing its interface.
Work out the fare by a rule that can react to demand.
The ride has a fare field but nothing fills it, and prices rise when demand outstrips drivers. A `PricingStrategy` with one method, `fare(distance, demand)`. `StandardPricing` is a base fee plus a rate per kilometre. `SurgePricing` multiplies the standard fare when demand is high. `RideService` uses one to price each completed ride. **Strategy** again: pricing varies independently of matching and of the lifecycle. Adding night pricing or a promotion is one class. The ride's fare is fixed when the ride is completed, so later rule changes never alter an old trip. Showing a fixed price up front means the rider is protected from surge changes mid-trip, but the company takes on the risk. Many apps quote a price at request time and honour it.
Notify riders and drivers of every change without coupling.
Everyone wants to know when a ride changes: the rider's phone, the driver's app, analytics, support. Calling each of them from the service would couple it to all of them. A `RideObserver` interface with `onStatusChanged()`. `RideService` keeps a list of observers and calls them after every state change (accept, start, complete, cancel). `RiderNotifier` is one observer; a driver notifier or an analytics logger is another. This is the **Observer** pattern: the service only knows the interface, so new listeners never change it. Every transition goes through the service, so it is the single place that fires the notification and no change is missed. Calling observers inside a transition can slow it down or fail. In production the notification goes onto a queue and a worker delivers it, so a slow phone never blocks a ride.