Methods and Attributes: The Building Blocks of Objects
In the zone model, an object is a self-contained unit that combines methods, which provide executable behavior, with attributes, which hold data.
From Monolith to Zones
An object-oriented program is easier to understand when you stop viewing it as one large block of instructions. Instead, picture it as a collection of semi-independent zones. Each zone combines executable behavior with stored data, has a responsibility, and communicates with other zones through well-defined boundaries.
In the zone model, an object is a self-contained unit that combines methods, which provide executable behavior, with attributes, which hold data.
This model gives you a practical way to ask three design questions: What data does this object need to hold? What behavior does it need to provide? How should it interact with other zones and with the external world?
Inside One Object
A single object contains two closely related parts. Its attributes represent the data or state that belongs to that particular object. Its methods represent the instructions that can use or change that state. The methods are the how: they describe what the object can do. The attributes are the what: they hold the values the object needs to do its work.
| Part of a zone | Role | Question it answers |
|---|---|---|
| Methods | Executable instructions | How does this object perform an operation? |
| Attributes | Stored data | What information or state does this object hold? |
Methods and attributes work together inside an object: methods provide behavior, while attributes provide the data that behavior operates on.
Tracing Shared Code and Separate Data
Suppose a class defines the behavior for a parser. When you create an object from that class, the object becomes a new zone with its own attribute data and access to the methods defined by the class. If you create several parser objects from the same class, they share the class's code zone, but each object maintains a separate data zone.
Two Parser Objects
Use the zone model to describe two parser objects created from the same class.
- 1Identify the shared part
The class definition supplies one shared code zone containing the methods available to the parser objects.
- 2Identify the separate parts
Each parser object has its own data zone containing the values stored in that instance's attributes.
- 3Trace an operation
When a method is called on one parser object, the shared method instructions operate on that specific object's data.
The objects reuse the same behavior while maintaining separate state, so one parser object can operate independently of another.
Crossing Zone Boundaries
Zones are useful because they are not isolated. They cooperate through boundaries. One zone can call a method in another zone, pass data to it, and receive a result. The receiving zone executes its own instructions and works with its own data before returning the result.
Extracting Links in Three Zones
Trace how a program can use separate zones to extract links from a web page.
- 1Fetch
A fetcher zone retrieves the web page's HTML text from the external world.
- 2Pass
The fetcher zone passes the HTML text to a parser zone.
- 3Parse
The parser zone processes the HTML and extracts link data using its own methods and data.
- 4Store
The parser zone passes the extracted links to a collection zone that stores and manages them.
Several focused zones cooperate through passed data and results. No single zone needs to perform fetching, parsing, and collection management.
The sequence is not a straight line inside one monolithic block. Control moves from one zone to another as methods are called. A zone may receive data, change its own data, return a result, and then enable another zone to continue the task.
Assigning Responsibilities
Thinking in zones helps you decide which object should handle a task. A well-designed zone has a clear responsibility, the data it needs, the methods that provide its behavior, and defined interactions with other zones. This makes the program easier to understand, test, and modify.
For the link-extraction example, fetching page content, parsing HTML, and managing extracted links are separate responsibilities. Keeping them in separate zones makes the flow of data visible and helps prevent one zone from becoming too large or trying to do too much.
Mistakes with the Zone Model
Common mistake Treating a zone as only a collection of methods
What it looks likeDescribing an object only by what it can do and ignoring the values stored in its attributes.
Why it failsA zone combines executable code with stored data. Methods need the object's data to carry out its responsibility.
Do this insteadWhenever you identify methods, also identify the attributes those methods use or maintain.
Common mistake Assuming objects created from one class have identical data
What it looks likeThinking that two parser objects must hold the same internal state because they use the same class.
Why it failsObjects of the same class share a code zone but maintain separate data zones.
Do this insteadSeparate the shared class behavior from each instance's own attribute data.
Common mistake Imagining the zone model as a literal memory map
What it looks likeTreating every conceptual zone as a precisely located physical region of memory.
Why it failsThe zone model is a conceptual tool for reasoning about structure and responsibility, not a literal description of memory.
Do this insteadUse zones to track behavior, data, responsibilities, and interactions.
Common mistake Putting every responsibility into one object
What it looks likeUsing one zone to fetch a page, parse its HTML, and manage extracted links.
Why it failsA monolithic zone makes it harder to reason about data flow and which object should handle each task.
Do this insteadSeparate focused responsibilities and connect the zones through well-defined interactions.
Practice the Trace
A program has three zones: one retrieves page content, one processes that content, and one manages the resulting records. Describe the methods and attributes each zone might need, then trace the data and control flow from the external world through the three zones.
Hints
- Start by assigning one focused responsibility to each zone.
- For each zone, distinguish its executable behavior from the data it stores.
- Identify what data crosses each boundary and what result is returned.
What do you think happens?
Two objects are created from the same class. Do they use the same code and the same data?
Reveal answer
They use the same code and maintain separate data.
Objects of the same class share one code zone containing the class methods, while each object maintains its own data zone.
The Object-Oriented Picture
- 1An object can be understood as a zone containing methods and attributes.
- 2Methods provide the executable behavior, while attributes hold the object's data or state.
- 3Objects created from the same class share the class's code zone but maintain separate data zones.
- 4Zones interact through method calls, passed data, and returned results across well-defined boundaries.
- 5The zone model helps you assign focused responsibilities and reason about a program without treating it as one monolithic block.
Key Takeaways
- Objects are self-contained zones that combine methods and attributes.
- Methods describe how an object behaves; attributes store what the object knows or maintains.
- Multiple objects can reuse one class's code while keeping separate data.
- Object-oriented programs coordinate zones through method calls, data exchange, and returned results.
- The zone model is a conceptual design tool for understanding responsibilities and interactions.