|
|
|
|
|
|
|
Figure 7-3
Function Interface (Visible) and Implementation (Hidden) |
|
|
|
 |
|
 |
|
|
InterfaceA connecting link at a shared boundary that permits independent systems to meet and act on or communicate with each other. Also, the formal description of the purpose of a subprogram and the mechanism for communicating with it. |
|
|
|
 |
|
 |
|
|
EncapsulationHiding a module implementation in a separate block with a formally specified interface. |
|
|
|
|
|
|
|
|
Encapsulation is what we do in the top-down design process when we postpone the solution of a difficult subproblem. We write down its purpose and what information it takes and returns, and then we write the rest of our design as if the subproblem already had been solved. We could hand this interface specification to someone else, and that person could develop a function for us that solves the subproblem. We needn't be concerned about how it works, as long as it conforms to the interface specification. Interfaces and encapsulation are the basis for team programming, in which a group of programmers work together to solve a large problem. |
|
|
|
|
|
|
|
|
Thus, designing a function can (and should) be divided into two tasks: designing the interface and designing the implementation. We already know how to design an implementationit is a segment of code that corresponds to an algorithm. To design the interface, we focus on the what, not the how. We must define the behavior of the function (what it does) and the mechanism for communicating with it. |
|
|
|
|
|
|
|
|
You already know how to specify formally the behavior of a function. Because a function corresponds to a module, its behavior is defined by the precondition and postcondition of the module. All that remains is to define the mechanism for communicating with the function. To do so, make a list of the following items: |
|
|
|
|
|
|
|
|
1. Incoming values that the function receives from the caller. |
|
|
|
|
|
|
|
|
2. Outgoing values that the function produces and returns to the caller. |
|
|
|
|
|