Methods and Attributes: The Building Blocks of Objects
A zone is a self-contained unit combining code (methods) and data (attributes) with well-defined interactions with other zones and the external world.
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? |
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.
Identify the shared part: The class definition supplies one shared code zone containing the methods available to the parser objects.
Identify the separate parts: Each parser object has its own data zone containing the values stored in that instance's attributes.
Trace 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.
Fetch: A fetcher zone retrieves the web page's HTML text from the external world.
Pass: The fetcher zone passes the HTML text to a parser zone.
Parse: The parser zone processes the HTML and extracts link data using its own methods and data.
Store: 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
Treating a zone as only a collection of methods
A zone combines executable code with stored data. Methods need the object's data to carry out its responsibility.
Fix:
Whenever you identify methods, also identify the attributes those methods use or maintain.Assuming objects created from one class have identical data
Objects of the same class share a code zone but maintain separate data zones.
Fix:
Separate the shared class behavior from each instance's own attribute data.Imagining the zone model as a literal memory map
The zone model is a conceptual tool for reasoning about structure and responsibility, not a literal description of memory.
Fix:
Use zones to track behavior, data, responsibilities, and interactions.Putting every responsibility into one object
A monolithic zone makes it harder to reason about data flow and which object should handle each task.
Fix:
Separate 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
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
- An object can be understood as a zone containing methods and attributes.
- Methods provide the executable behavior, while attributes hold the object's data or state.
- Objects created from the same class share the class's code zone but maintain separate data zones.
- Zones interact through method calls, passed data, and returned results across well-defined boundaries.
- The 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.