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)
Architecture Layers
Architecture Layers

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
        }
Concept flow

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.

  1. OBJECTPlayer instance
    to_dict()
  2. PLAIN DATAdictionary
    json.dump
  3. JSON FILEpersistent representation
    load + construct
  4. 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.

Source of Truth
Source of Truth

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.

1 / 3
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.