Java Reference
In-Depth Information
So, we can use higher-order functions in order to help us easily implement the single re-
sponsibility principle.
The Open/Closed Principle
Software entities should be open for extension, but closed for modification.
— Bertrand Meyer
The overarching goal of the open/closed principle is similar to that of the single responsibil-
ity principle: to make your software less brittle to change. Again, the problem is that a single
feature request or change to your software can ripple through the code base in a way that is
likely to introduce new bugs. The open/closed principle is an effort to avoid that problem by
ensuring that existing classes can be extended without their internal implementation being
modified.
When you first hear about the open/closed principle, it sounds like a bit of a pipe dream.
How can you extend the functionality of a class without having to change its implementa-
tion? The actual answer is that you rely on an abstraction and can plug in new functionality
that fits into this abstraction. Let's think through a concrete example.
We're writing a software program that measures information about system performance and
graphs the results of these measurements. For example, we might have a graph that plots
how much time the computer spends in user space, kernel space, and performing I/O. I'll call
the class that has the responsibility for displaying these metrics MetricDataGraph .
One way of designing the MetricDataGraph class would be to have each of the new metric
points pushed into it from the agent that gathers the data. So, its public API would look
something like Example 8-38 .
Example 8-38. The MetricDataGraph public API
class
class MetricDataGraph
MetricDataGraph {
public
public void
void updateUserTime ( int
int value );
public
public void
void updateSystemTime ( int
int value );
public
public void
void updateIoTime ( int
int value );
}
Search WWH ::




Custom Search