How Objects Communicate: Calling Methods and Passing Data
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
A program that uses multiple objects is not one monolithic block of instructions. It is better understood as several semi-independent zones working together. Each zone has a responsibility, contains code and data, and communicates with other zones through defined interactions.
In the zone model, an object is a self-contained unit that combines methods, which provide executable instructions, with attributes, which hold data.
Shared Instructions, Separate State
When a class creates an object, that object becomes a new zone. The object has its own data in its attributes and access to the methods defined by the class. Multiple objects created from the same class share one code zone, but they maintain separate data zones. The instructions are written once, while each object can operate with its own internal state.
Crossing an Object Boundary
Objects communicate through well-defined boundaries. One zone can call a method provided by another zone, pass data to that method, and receive a result. The receiving zone executes its own instructions and works with its own data. The calling zone can then use the result, update its own data, or communicate with another zone.
When designing a zone, ask three questions: What data does this zone need to hold? What methods does it need to provide? How will it interact with other zones?
A Three-Zone Trace
Extracting Links from a Web Page
Trace how three object zones can cooperate to extract links from a web page.
Fetching: A fetcher zone retrieves the web page's HTML text.
Passing: The fetcher zone passes the HTML text to a parser zone.
Parsing: The parser zone processes the HTML and extracts link data.
Storing: The parser zone passes the extracted link data to a collection zone that stores and manages it.
The zones accomplish one larger task together while each remains responsible for its own code and data.
This trace shows why a multi-object program does not have to follow one uninterrupted path inside one zone. Control moves from the fetcher to the parser and then to the collection zone as methods are called and data is exchanged.
External Inputs and Outputs
A zone can also interact with the external world through a defined boundary. In the web-page example, the fetching zone obtains HTML text from outside the program. That information crosses into the program, moves between object zones, and becomes extracted link data managed by another zone. The important idea is that each zone handles its own responsibility while coordinating through exchanged data.
Mistakes Beginners Make
Treating the program as one monolithic block
This makes it difficult to reason about where data flows and which object should handle a task.
Fix:
Separate the program into zones and identify each zone's responsibility, methods, and data.Assuming objects from the same class have identical data
Objects of the same class share code but maintain separate data zones.
Fix:
Track the methods as shared class code and the attributes as data belonging to each individual object.Treating the zone model as a literal memory diagram
The zone model is a conceptual tool for design and reasoning, not a literal description of memory.
Fix:
Use zones to discuss responsibilities, code, data, boundaries, and interactions.Giving one zone too many responsibilities
A zone that tries to do too much becomes harder to understand, test, and modify.
Fix:
Use the zone model to identify opportunities for multiple, more focused zones.
Practice the Trace
Imagine a program with three zones: an input zone receives information from the external world, a processing zone transforms that information, and a storage zone keeps the result. Describe the order of method calls and identify what data crosses each boundary.
Hints
- Begin with the zone that interacts with the external world.
- Name the data passed from the input zone to the processing zone.
- Explain what result the processing zone passes to the storage zone.
- An object is a zone that combines methods and attributes.
- Objects communicate by calling methods, passing data, and returning results.
- Objects from the same class can share code while keeping separate data.
- External information can enter through one zone and move through other zones via defined interactions.
- The zone model helps reveal responsibilities and suggests when a large zone should be split into more focused zones.
Key Takeaways
- Object-oriented programs can be understood as cooperating zones rather than one monolithic instruction block.
- Each object combines executable methods with stored attribute data.
- A method call moves control across a boundary, may carry data into the receiving zone, and can return a result.
- Objects created from the same class share code while maintaining separate data.
- The zone model is a design and reasoning tool for assigning clear responsibilities and tracing data flow.