Created June 28, 2009
Last updated July 4, 2009
Instance-scope attribute and Class-scope attribute
There are two kinds of attributes of the class in the point of how with data.
One is an instance-scope attribute, and the other is a class-scope attribute.
In this page, I will explain the feature, difference and use of the instance-scope attribute and the class-scope attribute 1
1:I explain only a class-scope attribute.
Please look at the class diagram (Figure 1).
Figure 1 - Class diagram
In the figure, the not underlined attributes are the instance-scope.
The instance-scope attributes have a different(or same) value in each instance. If you need, please look at Figure 1 and Figure 2 at "Classes and Objects 3. Example1(Telephone book)".
In Figure 1, the attributes of "Name" and "Telephone number" have a different value in each line (in other words , it can have a different value). These are the instance-scope attribute.
In the beginning, please look at Figure 2 (same content as Figure 1).
Figure 2 - Class diagram 1
The underlined one is a class-scope attribute 2.
Then, please look at Figure 3.
Figure 3 - The data of telephone number and the number of data 1
The class-scope attribute doesn't have a different value in each instance but has one value in the class.
In Figure2, the number of data (number of instances of the TelephoneBook class) is not the data that is belong to an individual instance.
First of all, the example in which the class-scope attribute is used includes the Singleton pattern that is one of the design patterns of GoF.
1:Here, the number of data of instances was defined as a class-scope attribute. However, this gave as an example, and doesn't give the number of data of instances as an class-scope attribute in an actual design. The number of instances of the class can certainly be managed as a class-scope attribute. However, like the number of telephone book data of children in Class 1 A and the number of telephone book data of children in Class 1 B, the number of data grouped by something cannot be managed.
Excel VBA has not given the number of instances as an attribute in the class scope either. Instead, the collection class of the class is made to give the number of instances (The number of instances is acquired in the Count property). For instance, the Worksheets class (* It is a collection class) has the object of the Worksheet class in the Excel library.
2:The class-scope attribute and operation are underlined. That is written in the UML Superstructure Specification v2.0 "7.3.19 Feature (from Kernel)"(p67).
When only one instance of the class is generated, the Singleton pattern is used.
I do not know the one to use the class-scope attribute in other design patterns. However, it might be good to examine the design pattern used with the Singleton pattern because in the design pattern, there is something used with other design patterns 1.
As other examples, the constant used with the system together is written in the file 2 , and it sets it in the class-scope attribute value immediately after the start of the system.
The following tables are pages that I referred to create this page.
1:For instance, Flyweight pattern
2:It doesn't read from the file but it reads from RDB (relational database), and there is a method of writing the value directly in the program without reading from the resource somewhere, too.
I doesn't assume the responsibility of any damage that occurs because of the use of this page.
Microsoft ,Windows ,Visual Basic and Excel are registered trademarks of Microsoft Corporation in the United States and other countries.
Visual Basic for Applications may represent a VBA.
Mac ,Mac OS ,Mac OS X ,AppleScript are trademarks of Apple Inc., registerd in the U.S. and other countries.
UML ,Unified Modeling Language are trademarks of Object Management Group Inc. in the U.S. and other countries.
Other brands and their products are trademarks or registered trademarks of their respective holders and should be noted as such.
The author of this page and these companies do not have any relationship.