# Lecture 22: Compose domain objects with explicit policies ## Compose domain objects, collections, and algorithms **Instructor:** [Dr. Debasish Pattanayak](https://drdebmath.github.io)
## One component owns one reason to change -
Flight
: route, capacity, and availability invariant. -
Passenger
: passenger identity and contact value. -
Booking
: association between one passenger and one flight. -
BookingSystem
: collection operations and policies. High cohesion keeps each responsibility local.
## A domain model names the objects and rules that matter ~~~cpp struct Flight { std::string code; std::string from; std::string to; int seats; }; struct Passenger { std::string rollNumber; std::string name; }; struct Booking { std::size_t flightIndex; std::size_t passengerIndex; }; ~~~ Indices express associations without ambiguous ownership.
## A predicate turns a domain question into reusable selection ~~~cpp auto matchesRoute = [requestedFrom, requestedTo](const Flight& flight) { return flight.from == requestedFrom && flight.to == requestedTo; }; auto result = std::find_if(flights.begin(), flights.end(), matchesRoute); ~~~ The lambda is a predicate: a callable returning true or false. Its capture list copies the two requested city values into the callable.
## A comparator makes ordering policy explicit and testable ~~~cpp std::sort(flights.begin(), flights.end(), [](const Flight& a, const Flight& b) { return a.code < b.code; }); ~~~ The comparator defines one consistent ordering relation.
## Route planning applies graph algorithms to domain constraints Airports are vertices and direct flights are edges. Route optimization selects a path according to hops, time, distance, or cost. State the objective before choosing an algorithm.
## Patterns separate replaceable policy from stable mechanism - Factory: choose which concrete service object to create. - Strategy: supply a pricing or ranking policy through an interface. Use a design pattern only after the recurring responsibility problem is visible.
## Compose domain objects with explicit predicates, comparators, and policies - Domain classes represent entities, values, and associations. - Collections expose search and sorting applications. - Predicates select; comparators order. - Routing applies graph structure to a stated objective. - Patterns name reusable responsibility arrangements.