Object-Oriented Frameworks
How Object-Oriented Frameworks Run Your Code
When you build several applications in the same domain, they often share a workflow while differing in a few operations. A reporting application might always open an output destination, examine records, format selected records, and close the destination. An object-oriented framework supplies that recurring structure. Your task is to recognize which execution decisions it already makes and where your application is expected to supply behavior.
A reusable structure that controls execution
An object-oriented framework is a reusable set of cooperating classes that provides a structure and control flow for a family of applications, together with supported places for customization. It gives you more than individual operations: it connects operations into a working structure.
The central execution idea is inversion of control: framework code decides when to invoke application-supplied behavior. You may start the framework by calling a method such as run, but the framework then controls the sequence of calls within that operation.
Situation | Who normally chooses the sequence? | Role of application code |
|---|---|---|
Typical library use | The application | Calls useful operations when needed |
Framework-managed execution | The framework within its managed workflow | Supplies behavior at supported extension points |
This comparison describes typical use. A library can accept callbacks, and an application can combine library calls with framework-managed execution. One callback alone does not establish that an entire library provides an application framework; look for the reusable structure and workflow around it.
Customizing the supported extension points
A framework can call a hook method on an application subclass. The subclass inherits the framework's workflow and overrides selected operations. Through dynamic dispatch, a call made by inherited code can execute the subclass's implementation.
Consider this simplified framework method:
def run(self, items):
self.open()
for item in items:
if self.accept(item):
self.process(item)
self.close()Suppose open and close are supplied by the framework, while accept and process are documented customization points. Your subclass can choose which items qualify and what happens to qualifying items. The inherited run method still determines the order of those operations.
Another framework might accept a separate extension object through registration. It can then call that object's methods without requiring your application to inherit from a framework class. Both approaches allow application-specific behavior inside framework-controlled execution.
Use the extension mechanism the framework documents. The presence of a method does not automatically mean applications are expected to replace it. Replacing the workflow itself may skip setup, checking, or cleanup that the framework normally performs.
Reasoning about calls through the framework
To trace execution, start in the controlling method and follow each call. Distinguish the number of input items from the number that reach a conditional operation.
For the run method above, let n be the number of input items and a the number accepted. If every item is examined exactly once, all calls return normally, and execution has no retries or early termination, then
Example: If six records are examined and four are accepted, accept runs six times and process runs four times. Together, these two methods receive ten calls. That total excludes run, open, and close because it counts only the two named hooks.
These counts follow from this particular workflow. A framework that stops early, repeats failed operations, or processes items in groups needs a fresh trace. Start with its actual control flow rather than assuming every extension runs once per input.
What to remember
- A framework supplies cooperating classes and a reusable control structure.
- Inversion of control concerns who schedules application-supplied behavior.
- Subclass overrides and registered objects are two possible extension mechanisms.
- Trace the framework's conditions and loops before counting method calls.
Example: Extending a Measurement Report Framework
A laboratory uses a hypothetical reporting framework to produce a report from measurement rows. The application supplies two hook methods, methods intended for application-specific behavior: include decides whether a row belongs in the report, and render writes a qualifying row. The application inherits run unchanged.
The following excerpt shows the framework's controlling method. The framework supplies begin and end; the application overrides include and render.
def run(self, rows):
self.begin()
for row in rows:
if self.include(row):
self.render(row)
self.end()There are 10 rows, and include returns true for exactly seven. Each include call takes 0.20 ms, and each render call takes 1.50 ms. All remaining framework work, including begin and end, takes 3.00 ms in total. Calls execute sequentially and return normally, with no retries or early stopping. These costs cover the entire run without overlap.
a) Identify who controls the sequence and which behavior the application customizes.
The framework's inherited run method controls the sequence. It calls begin, visits rows in order, calls include for each row, conditionally calls render, and finally calls end.
The application customizes the inclusion decision and row rendering through its two overrides. A call such as self.include(row) reaches the application's implementation on the actual object, even though the call appears inside framework code.
The application may initiate execution by calling run, but the inherited framework method schedules the hook calls. This is inversion of control within the reporting workflow.
The framework controls the sequence; the application supplies row selection and row rendering.
b) Determine how many times include and render execute.
First, count the iterations of the framework's loop. Every one of the 10 rows reaches the inclusion test, regardless of whether the row qualifies.
Next, follow the conditional branch. Only the seven rows for which include returns true reach render.
The three rejected rows still require inclusion checks; they simply avoid rendering. Therefore, include executes 10 times and render executes seven times.
c) Calculate the total execution time in ms, to one decimal place.
First, multiply the number of inclusion checks by the duration of one check. The checks take ________ in total.
Next, multiply the number of rendered rows by the rendering duration. Rendering takes ________ in total.
Together, the two hooks take ________. Add the remaining framework work once for the whole run.
The total is greater than the hook time alone because setup, completion, and other framework operations also consume time. To one decimal place, the report takes 15.5 ms.
A Java object-oriented framework supplies ExportJob. Application code supplies LabelJob, intending to customize the formatting used by the framework's run method.
class ExportJob {
public final String run() {
return format(7);
}
protected String format(int count) {
return "base";
}
}
class LabelJob extends ExportJob {
protected String format(long count) {
return "label";
}
}Fill in the returned text without quotation marks and the Java annotation, including its initial @ character.
The expression new LabelJob().run() returns . To make the compiler reject the subclass method when it does not override a superclass method, place immediately above that subclass method.
A team is documenting an object-oriented reporting framework. Match each term to the description that best explains its role in application customization and execution.
Operation intended for application-specific behavior at a documented extension point
Reusable cooperating classes that provide a control structure for a family of applications
Execution arrangement in which reusable infrastructure decides when application-supplied operations run
Act of supplying an extension object so infrastructure can locate and invoke it
Inversion of control
Hook method
Object-oriented framework
Registration