Architecture & LLD

State vs. Behavior

CORE CONCEPT

State & Behavior

01

State — What the Object Knows

Fields (also called attributes or properties)

State is the data an object holds right now. A lightbulb's state at this moment might be: isOn = false, brightness = 75%. State can change over time as the object is used.

Every object of the same class has its own copy. Bulb A can be on at 90% while Bulb B is off — they share the same class, but each has independent state.

02

Behavior — What the Object Does

Methods (also called functions)

Behavior is what an object can do — and most methods do their work by reading or changing the object's state. turnOn() changes isOn to true. brighten() increases the brightness value.

Think of state as the noun and behavior as the verb. The object is the complete sentence.

03

See It Live — The LightBulb Object

Click each method button on the left. Watch how calling a method changes the state fields — and how those state values then drive what the bulb looks like.

OBJECT: LightBulb
State (fields)
isOn: false
brightness: 50%
Behavior (methods)
Click a method to see how it updates state...
Visual Result
💡
Off
Driven entirely by state
How it connects:
Method called → state value changes → visual updates.
The method itself is just logic — the state is what persists.
04

Reading the Code

Notice how the fields at the top are just data — no logic. And the methods below only exist to read or update those fields. That separation is the pattern.

LightBulb.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class LightBulb {
// STATE — data the object holds
isOn: boolean = false;
brightness: number = 50;
// BEHAVIOR — actions the object can do
turnOn(): void {
this.isOn = true;
}
turnOff(): void {
this.isOn = false;
}
brighten(): void {
this.brightness = Math.min(100, this.brightness + 25);
}
dim(): void {
this.brightness = Math.max(0, this.brightness - 25);
}
}
UTF-8Spaces: 4TypeScript
💡

The One-Line Takeaway

Fields = the nouns (what it knows). Methods = the verbs (what it does). Together they make a complete, living object.

Up next: Encapsulation