# Write Change-Resilient Code With Domain Objects

DevFeed: [Write Change-Resilient Code With Domain Objects](<https://devfeed.tech/articles/write-change-resilient-code-with-domain-objects-23860.md>)

Original publisher: [Read original article](<http://testing.googleblog.com/2024/09/write-change-resilient-code-with-domain.html>)

Author: Google Testing Bloggers (noreply@blogger.com)

Published: 2024-09-04T12:56:00Z

Content type: article

Language: en

Sources: [Google Testing Blog](<https://devfeed.tech/sources/google-testing-blog.md>)

Topics: [Object-oriented programming (OOP)](<https://devfeed.tech/topics/oop.md>), [interface](<https://devfeed.tech/topics/interface.md>), [solid principles](<https://devfeed.tech/topics/solid-principles.md>), [class](<https://devfeed.tech/topics/class.md>)

Tags: [amy-fu](<https://devfeed.tech/tags/amy-fu.md>), [avoid](<https://devfeed.tech/tags/avoid.md>), [behavior](<https://devfeed.tech/tags/behavior.md>), [blocks](<https://devfeed.tech/tags/blocks.md>), [business](<https://devfeed.tech/tags/business.md>), [cases](<https://devfeed.tech/tags/cases.md>), [change](<https://devfeed.tech/tags/change.md>), [changes](<https://devfeed.tech/tags/changes.md>), [class](<https://devfeed.tech/tags/class.md>), [classes](<https://devfeed.tech/tags/classes.md>), [code](<https://devfeed.tech/tags/code.md>), [code-health](<https://devfeed.tech/tags/code-health.md>), [color](<https://devfeed.tech/tags/color.md>), [configure](<https://devfeed.tech/tags/configure.md>), [delivery](<https://devfeed.tech/tags/delivery.md>), [demand](<https://devfeed.tech/tags/demand.md>), [display](<https://devfeed.tech/tags/display.md>), [domain](<https://devfeed.tech/tags/domain.md>), [download](<https://devfeed.tech/tags/download.md>), [tott](<https://devfeed.tech/tags/tott.md>), [write](<https://devfeed.tech/tags/write.md>)

## AI overview

The excerpt argues that code built around stable product concepts can better accommodate changing requirements. In its gPizza example, a single `deliverPizza` method may lead to a growing set of methods as drinks and other offerings are added. It recommends modeling common ideas shared across related use cases with domain objects, rather than encoding each requirement as a separate behavior. The excerpt defines a use case as a behavior serving a business requirement and begins suggesting questions for identifying a suitable domain object, but cuts off before completing that guidance.

## Source excerpt

This is another post in our Code Health series. A version of this post originally appeared in Google bathrooms worldwide as a Google Testing on the Toilet episode. You can download a printer-friendly version to display in your office. By Amy Fu Although a product's requirements can change often, its fundamental ideas usually change slowly. This leads to an interesting insight: if we write code that matches the fundamental ideas of the product, it will be more likely to survive future product changes. Domain objects are building blocks (such as classes and interfaces) in our code that match the fundamental ideas of the product. Instead of writing code to match the desired behavior for the product's requirements ("configure text to be white"), we match the underlying idea ("text color settings"). For example, imagine you're part of the gPizza team, which sells tasty, fresh pizzas to feed hungry Googlers. Due to popular demand, your team has decided to add a delivery service. Without domain objects, the quickest path to pizza delivery is to simply create a deliverPizza method: public class DeliveryService { public void deliverPizza(List<Pizza> pizzas) { ... } } Although this works well at first, what happens if gPizza expands its offerings to other foods? You could add a new method: public void deliverWithDrinks(List<Pizza> pizzas, List<Drink> drinks) { ... } But as your list of requirements grows (snacks, sweets, etc.), you'll be stuck adding more and more methods. How can you change your initial implementation to avoid this continued maintenance burden? You could add a domain object that models the product's ideas, instead of its requirements: A use case is a specific behavior that helps the product satisfy its business requirements. (In this case, "Deliver pizzas so we make more money".) A domain object represents a common idea that is shared by several similar use cases. To identify the appropriate domain object, ask yourself: What related use cases does the produc