In common usage, visibility is the ability of an object to “see” or have a reference to another object. More generally, it is related to the issue of scope: Is one resource (such as an instance) within the scope of another? There are four common ways that visibility can be achieved from object A to object B:
The motivation to consider visibility is this:
For an object A to send a message to an object B, B must be visible to A. |
For example, to create an interaction diagram in which a message is sent from a Register instance to a ProductCatalog instance, the Register must have visibility to the ProductCatalog. A typical visibility solution is that a reference to the ProductCatalog instance is maintained as an attribute of the Register.
Attribute visibility from A to B exists when B is an attribute of A. It is a relatively permanent visibility because it persists as long as A and B exist. This is a very common form of visibility in object-oriented systems.
To illustrate, in a Java class definition for Register, a Register instance may have attribute visibility to a ProductCatalog, since it is an attribute (Java instance variable) of the Register.
public class Register { ... private ProductCatalog catalog; ... }
This visibility is required because in the enterItem diagram shown in Figure 18.2, a Register needs to send the getSpecification message to a ProductCatalog:
Parameter visibility from A to B exists when B is passed as a parameter to a method of A. It is a relatively temporary visibility because it persists only within the scope of the method. After attribute visibility, it is the second most common form of visibility in object-oriented systems.
To illustrate, when the makeLineItem message is sent to a Sale instance, a ProductSpecification instance is passed as a parameter. Within the scope of the makeLineItem method, the Sale has parameter visibility to a ProductSpecification (see Figure 18.3).
It is common to transform parameter visibility into attribute visibility. For example, when the Sale creates a new SalesLineItem, it passes a ProductSpecification in to its initializing method (in C++ or Java, this would be its constructor). Within the initializing method, the parameter is assigned to an attribute, thus establishing attribute visibility (Figure 18.4).
Local visibility from A to B exists when B is declared as a local object within a method of A. It is a relatively temporary visibility because it persists only within the scope of the method. After parameter visibility, it is the third most common form of visibility in object-oriented systems.
Two common means by which local visibility is achieved are:
Create a new local instance and assign it to a local variable.
Assign the returning object from a method invocation to a local variable.
As with parameter visibility, it is common to transform locally declared visibility into attribute visibility.
An example of the second variation (assigning the returning object to a local variable) can be found in the enterItem method of class Register (Figure 18.5).
A subtle version on the second variation is when the method does not explicitly declare a variable, but one implicitly exists as the result of a returning object from a method invocation. For example:
// there is implicit local visibility to the foo object // returned via the getFoo call anObject.getFoo().doBar();
Global visibility from A to B exists when B is global to A. It is a relatively permanent visibility because it persists as long as A and B exist. It is the least common form of visibility in object-oriented systems.
One way to achieve global visibility is to assign an instance to a global variable, which is possible in some languages, such as C++, but not others, such as Java.
The preferred method to achieve global visibility is to use the Singleton pattern [GHJV95], which is discussed in a later chapter.
3.15.143.40