Interview concept

Object-Oriented Design For Service Code

Asked of: Software Engineer

Last updated

A left-to-right hexagonal architecture diagram showing an order service, domain objects and policies, injected ports, infrastructure adapters, and test fakes.

What's being tested

Object-oriented design for service code tests whether you can turn changing business rules into cohesive, testable objects rather than one large conditional-heavy method. The interviewer is probing your ability to identify domain responsibilities, define stable interfaces, manage dependencies, and explain how the design evolves when requirements change. DoorDash services combine payments, orders, merchants, delivery logistics, and third-party integrations, so good designs must isolate failure-prone dependencies while remaining easy to extend and operate.

Core knowledge

  • Single Responsibility Principle (SRP) means a class should have one reason to change. Keep orchestration in an application service, business rules in domain objects or policies, persistence in repositories, and HTTP or RPC translation at the boundary.

  • Composition over inheritance is usually safer for service code. Inject a PricingPolicy, PaymentGateway, or NotificationSender into an orchestrator instead of creating a deep hierarchy whose subclasses inherit unwanted behavior.

  • Dependency inversion makes high-level business logic depend on interfaces, not concrete clients. Define PaymentGateway.charge(...) and inject StripePaymentGateway or a fake implementation through constructor injection for production isolation and fast unit tests.

  • Cohesion and coupling are the central design tradeoff. Group data and invariants that change together; avoid a “god service” that knows database schemas, transport objects, vendor APIs, retries, and every business rule.

  • Domain entities versus value objects should be distinguished explicitly. An Order has identity and lifecycle; Money, Address, and DeliveryWindow are immutable value objects compared by value and responsible for validating their own invariants.

  • Ports and adapters, also called hexagonal architecture, place interfaces at the domain or application boundary. Adapters translate Postgres, Kafka, REST, or vendor-specific models without leaking infrastructure types into core business logic.

  • The Strategy pattern handles interchangeable algorithms or policies, such as peak-hour fees, distance pricing, or merchant-specific cancellation rules. A registry or factory selects a strategy; callers should not contain repeated if provider == ... branches.

  • The command-orchestrator pattern separates a use case from its collaborators. An AcceptOrderService can validate state, reserve capacity, persist the transition, publish an event, and return a result without implementing every operation itself.

  • Invariants and state transitions belong near the object that owns them. Model legal transitions such as CREATED → CONFIRMED → PREPARING → READY; reject invalid transitions rather than allowing arbitrary setters to mutate status.

  • Idempotency is required when service methods can be retried. Accept an idempotency key, persist the key with the resulting outcome, and return the prior result on duplicate requests; do not rely only on an in-memory flag.

  • Transactions and side effects need an explicit boundary. Commit the order-state change atomically with an outbox record, then publish asynchronously; otherwise a database success followed by an event-publish failure can leave downstream services inconsistent.

  • Error modeling should distinguish validation errors, domain conflicts, dependency failures, and unexpected defects. Use typed errors or result objects, preserve safe context for logs, and avoid exposing vendor error details directly to API clients.

Worked example: Design a pluggable delivery-fee calculator

A strong candidate would first clarify whether the fee is calculated synchronously, whether pricing must be reproducible for an order, which inputs matter, and whether a missing map or pricing provider should fail closed or use a fallback. They would state assumptions such as: the calculator returns a Money value, all monetary arithmetic uses integer minor units, and the selected policy is recorded with the order. The answer would be organized around a DeliveryFeeService orchestrator, immutable input value objects, a FeePolicy strategy interface, and adapters for distance and surge data. The service would validate inputs, select a policy based on region or merchant configuration, calculate the fee, and return a decision containing the amount and explanation metadata. The policy interface might expose calculate(FeeContext), while concrete implementations handle flat pricing, distance bands, or peak-hour surcharges. A key tradeoff is whether policy selection belongs in a factory or configuration-driven registry: a registry improves runtime extensibility, while a simple factory is easier to validate and debug. The candidate should explicitly prevent floating-point money calculations and define behavior for negative distance, unknown regions, and unavailable surge data. If more time were available, they would discuss versioning policies so an already-created order is not repriced after configuration changes.

A second angle

Consider an order-cancellation service, where the same design principles apply but state consistency and side effects dominate the framing. The core object owns legal cancellation transitions, while injected collaborators determine refund eligibility, notify the merchant, and release delivery capacity. The candidate should distinguish a cancellation rejected because the order is already delivered from a cancellation that cannot complete because the payment provider is temporarily unavailable. They should also discuss idempotency and an outbox so a retry does not issue two refunds or send duplicate cancellation events.

Common pitfalls

Analytical mistake: Treating every noun as a class produces an anemic model with dozens of getters and setters but no ownership of rules. A stronger answer assigns each invariant to the object or policy that can enforce it and keeps orchestration focused on workflow.

Pitfall: Saying “use inheritance for each pricing type” is tempting, but a deep hierarchy becomes brittle when pricing varies independently by region, merchant, time, and customer segment; strategies and composition usually scale better.

Communication mistake: Jumping directly into class diagrams without clarifying inputs, consistency requirements, or failure behavior makes the design appear disconnected from the use case. State assumptions first, then explain responsibilities and one representative call flow.

Depth mistake: Stopping at “inject an interface for testability” misses production concerns. Explain retry boundaries, idempotency, transaction scope, timeout behavior, observability, and how an external failure affects the user-visible result.

Connections

An interviewer may pivot to SOLID principles, design patterns, distributed transactions, event-driven architecture, or contract testing. Be prepared to connect object boundaries to API versioning, persistence transactions, and concurrency control without turning the answer into infrastructure-only design.

Further reading

Related concepts