Unit 12 · lesson
Program Architecture
Core path: 25 minutes
How do objects fit into a larger application without creating another giant mess?
Creating a class does not magically create good architecture. We must connect objects with responsibilities.
The Model
In a multi-file architecture, player.py will define the Player class. This class represents a core concept from our application domain—often called a model.
Architectural Boundaries
player.py: What is a Player? What rules belong to it? (The Domain)storage.py: How is data saved and loaded? (Persistence)display.py: How is information shown? (Presentation)main.py: What should happen and in what order? (Coordination)
Diagrams open at a readable shape-aware scale. Zoom or expand when you need more detail.
Objects do not need to control the entire world. Player should not read input or save files. It should just be a player.
Composition over inheritance
Objects can contain other objects. A Team class might have a list of Player objects.
class Team:
def __init__(self, name):
self.name = name
self.players = []
def add_player(self, player):
self.players.append(player)
This is composition.
Objects and JSON
json.dump() doesn't know how to save a custom Player object. We must define a conversion boundary.
def to_dict(self):
return {
"name": self.name,
"score": self.score
}
Objects need a serializable representation before they become files
A custom object is converted to plain Python data, encoded as JSON, saved, and reconstructed later.
- OBJECTPlayer instanceto_dict()
- PLAIN DATAdictionaryjson.dump
- JSON FILEpersistent representationload + construct
- OBJECT AGAINrestored Player
One Source of Truth
Notice we didn't save rank to the JSON file. If rank is purely calculated from score, saving both creates a risk that they disagree if manually edited.
Do not store information that can be reliably derived unless necessary. Calculate it when you load the object.
Diagrams open at a readable shape-aware scale. Zoom or expand when you need more detail.
Vocabulary lab
Flip the idea, not just the card
Explain the term before you reveal the back. Then compare your explanation with the definition, example, and warning.
Read all terms without animation
- Architecture
- The organization and relationships between major parts of a software system. Example: Separating storage logic from display logic. Do not confuse it with: The syntax of a specific class.
- Model
- A program representation of a meaningful concept in the application's domain. Example: The Player class. Do not confuse it with: The UI or file system.
- Source of Truth
- The authoritative value from which related information should be derived. Example: Using score as the source for rank. Do not confuse it with: Duplicated, conflicting data.