Module 20 · Building Real Python Applications — topic 5 of 13
Classes and application design
Using classes to structure an application: data classes for records, service classes for behaviour, assigning responsibilities, composition and dependency injection, and keeping classes small and focused.
Learning objectives
- Model records with dataclasses
- Assign responsibilities to service classes
- Compose classes with dependency injection
Definition
In application design, classes group data with the behaviour that belongs to it. Typical roles are data classes (records), service classes (business rules), repositories (storage) and controllers (user interaction), composed together.
Introduction
As the expense tracker gains budgets, storage and notifications, classes give each concept a home: Expense, ExpenseBook, Budget, Tracker.
Why it matters
Clear class responsibilities keep growing applications understandable and let parts be swapped — for example, a fake notifier in tests.
Detailed explanation
Use dataclasses for records that are mostly data: Expense(amount, category). frozen=True makes them immutable values.
Put behaviour where the data is: an ExpenseBook class knows how to add expenses and compute totals.
Give each class one responsibility. If a class name needs 'and' (ExpenseBookAndReportPrinter), split it.
Compose larger objects from smaller ones: a Tracker has a repository, a budget and a notifier.
Dependency injection passes collaborators into __init__ instead of creating them inside, so they can be replaced (a list repository in tests, a database in production).
Keep input and output out of model and service classes; let the command-line or GUI layer handle them.
Prefer composition to inheritance unless there is a genuine is-a relationship.
Start with functions and introduce classes when state and behaviour clearly belong together.
Syntax
- Dataclasses for records.
- Services for behaviour.
- Inject collaborators.
Code examples
A record and a collection class
410 370
A class with one responsibility
['food over budget by 70']
More examples
Composition and dependency injection
50 -70 inf ['food: over budget by 70']
Step-by-step explanation
- Tracker coordinates three collaborators it receives in __init__.
- messages.append is passed as the notifier, so the test can see exactly what would have been shown or emailed.
- Categories without a budget have an infinite limit, so they never trigger warnings.
Expected output
Running “Composition and dependency injection” prints exactly:
Practical applications
- Service classes in web back ends.
- Swapping real email or SMS senders for fakes in tests.
Best practices
- Use dataclasses for records.
- One responsibility per class.
- Inject collaborators.
Common mistakes
A god class
Avoid
Prefer
One huge class is hard to understand, test and change.
Creating dependencies inside
Avoid
Prefer
Hard-wired dependencies can't be replaced in tests.
Important notes
- Protocols (typing.Protocol) can describe the interface collaborators must provide, such as an object with add() and all().
Key takeaways
- Classes give each concept a home.
- Records as dataclasses, behaviour in services.
- Compose and inject collaborators.
Try it yourself
- 1Add a CategoryReport class that takes an ExpenseBook and produces a sorted summary.
- 2Write a fake repository that records every call, and use it to test Tracker.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- AdvancedWhy pass a repository object into a service's __init__ instead of creating it inside?