Significant gains in reliability and maintainability can result when direct access to global data is denied in favor of indirect access to field data via method calls, a concept referred to as "encapsulation":
Traditional Object-Oriented
(direct access) (method call)
hull_1_char.lbp = 500. hull_1_char.setLBP(500.);
temp = hull_1_char.lbp temp = hull_1_char.getLBP();
The first line above sets a data value into the 'lbp' field of the variable
'hull_1_char', which is of the abstract type HullCharacteristics
(see Data Abstraction).
The second line retrieves the current 'lbp' value. The left-hand column
performs these tasks using the traditional approach of direct variable
access, while the right-hand column uses the encapsulation concept of
indirect variable access via method calls.Dot notation is used to refer to the methods setLBP() and getLBP() because these methods are defined as part of the HullCharacteristics abstract data type. Their definition might be represented in pseudo-code as follows:
type HullCharacteristics {
// Field Variables
float lbp; // length between perpendiculars
float lwl; // waterline length
float loa; // overall length
float b, h, d; // beam, draft, depth
float cb; // block coefficient
float cp; // prismatic coefficient
// Field Access Methods
method setLBP (float lbp_value) {
if (lbp_value > 0.0) {
lbp = lbp_value;
}
}
float method getLBP( ) {
return (lbp);
}
}
The setLBP() method changes the value of the 'lbp' field of the
HullCharacteristics data type. Before doing so, it ensures the new
value is greater than zero. Such validity checking is one way in which
encapsulation can improve reliability.The getLBP() method simply returns the current value of the 'lbp' field to its caller. Together, both methods encapsulate 'lbp', permitting indirect access to 'lbp' by other abstract types via the two methods. Similar 'set' and 'get' methods would be defined for each of the other field variables to encapsulate all HullCharacteristics data.
Other, similar methods would normally be defined to access the other fields as well.
type HullCharacteristics {
// Field Variables
private float lbp; // length between perpendiculars
private float lwl; // waterline length
private float loa; // overall length
private float b, h, d; // beam, draft, depth
private float cb; // block coefficient
private float cp; // prismatic coefficient
// Field Access Methods
method setLBP (float lbp_value) {
if (lbp_value > 0.0) {
lbp = lbp_value;
}
}
float method getLBP( ) {
return (lbp);
}
}
This restriction of visibility is termed "information-hiding." The net
effect is to force methods of other abstract types to use the
HullCharacteristics methods to access HullCharacteristics data.
Information-hiding forces encapsulation by restricting field visibility.Because all data fields of an abstract type are encapsulated and declared 'private,' abstract types have no global data, i.e., data that is directly accessible program-wide. Because an object-oriented program is constructed of abstract data types, OO programs as a whole contain no global data. Lack of global data is a signature trait of a properly-designed OO program.
While encapsulation and information-hiding can by themselves improve reliability and eliminate global data, when combined with stable interfaces they can greatly increase maintainability as well.