Splitting Attributes and Operations


We see developers create some classes with attributes but no operations, and other classes that have no attributes—only operations. (We don’t know about you, but every object-oriented class we ever met had both attributes and operations.)

The developers making this mistake are really thinking about data structures and the functions that act on the data. They translate that idea into the object-oriented world by using the steps much like the following:

Blunder 1:

Equate data structure only with class attributes.

Blunder 2:

Equate a function that manipulates data structures only with class operations.

Blunder 3:

Create one instance of the class with operations.

Blunder 4:

Create one instance of the class with attributes.

Blunder 5:

Use the class with the operations to change the values of the class with attributes.

 Warning   Do not follow the five steps we’ve just outlined (but you knew that). They lead to splitting up attributes and operations. Big mistake.

 Tip   Make your classes whole by putting the attributes and operations that need each other together in one class.

Figure 21-1 shows classes with attributes and classes with operations—separately (and confusingly). The Vehicle class works with the Truck class. The Tools class is similar to the ToolKit class. The Person class is another name for the Employee class. Figure 21-2 shows a better model, with the attributes and operations put together.


Figure 21-1: Example of a split-classes mistake.


Figure 21-2: Example of classes made whole.




UML 2 for Dummies
UML 2 For Dummies
ISBN: 0764526146
EAN: 2147483647
Year: 2006
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net