Chapters & contents Lesson 6 of 18
3 min read
Object-oriented designExpanded reading

Composition and delegation

Build a clear object relationship without inheriting implementation unnecessarily.

What you’ll learn

  • Describe a has-a relationship
  • Delegate behavior through a contract
  • Compare composition with inheritance

Separate the responsibilities

Suppose a report service obtains records and formats them for a reader. Reading records and producing a presentation are related but distinct responsibilities. If one large class owns every source format and every output format, changes can become tangled. A compositional design gives the service collaborators with narrower roles. The report service has a data source and has a formatter. It coordinates their work without pretending that the service is itself a specialized database or a specialized text renderer.

Delegate through an understandable contract

Delegation means asking another object to perform part of the work. A formatter might accept a collection of records and return a representation. Its contract should specify relevant behavior such as how empty input is represented and how errors are communicated. The service can use that contract without reaching into the formatter's internal fields. Avoid introducing an interface for every trivial object by habit; use a boundary when it makes responsibilities or variability clearer.

Compare the inheritance alternative

Inheritance can express a subtype relationship, but reusing a few methods is not enough to justify it. A subtype should preserve the expectations of code written for its base type. If a report service inherits from a database adapter merely to gain a query helper, it may expose operations that do not make sense for a report service. Composition can reuse the adapter's behavior without creating that misleading public relationship. This keeps the dependency visible and easier to replace.

Make testing more focused

When a service receives its collaborators explicitly, a test can supply a deterministic data source and a simple formatter. The test can then check coordination without requiring a real network or filesystem for every case. Separate integration tests still verify that real adapters follow their contracts. Passing dependencies does not mean every implementation detail must be injectable. Choose seams around effects, changing policies, and expensive boundaries where substitution helps explain or verify behavior.

Keep the design proportional

A small program may begin with direct functions and grow into collaborating objects as responsibilities become clearer. Composition is useful because it allows local changes, not because more objects are automatically better. After introducing a collaborator, ask whether its name and contract explain a real responsibility, whether callers know too much about its internals, and whether the extra indirection helps a reader. The aim is a design that can be followed and changed safely at the size the program actually has.

Try it yourself

A notification service sends either an email or a terminal message. Describe a compositional design and a useful test.

Show a possible solution

Give the notification service a sender collaborator with an explicit send contract. Use an email sender in one environment and a terminal or recording sender in another. A unit test can use a recording sender to verify the intended destination and message; integration tests verify real delivery adapters.

Check your understanding

Try 14 questions across object-oriented design. Read each explanation and revisit anything that needs another pass.

Practise this subject

Go to the source

Use these primary references to check details and explore the topic further.

Saved in this browser. Signed-in progress syncs to your account.