Home > Articles > Programming

  • Print
  • + Share This
This chapter is from the book

Responsibilities and Constraints

The class icon enables you to specify still another type of information about a class. In an area below the operations list, you can show the class's responsibility. The responsibility is a description of what the class has to do—that is, what its attributes and operations are trying to accomplish. A washing machine, for example, has the responsibility of taking dirty clothes as input and producing clean clothes as output.

In the icon, you indicate responsibilities in an area below the area that contains the operations (see Figure 3.12).

The idea here is to include enough information to describe a class in an unambiguous way. Indicating the class's responsibilities is an informal way to eliminate ambiguity.

A slightly more formal way is to add a constraint, a free-form text enclosed in curly brackets. The bracketed text specifies one or more rules the class follows. For example, suppose in the WashingMachine class you wanted to specify that the capacity of a washer can only be 16, 18, or 20 pounds (and thus "constrain" the WashingMachine class's capacity attribute). You would write {capacity = 16 or 18 or 20 lbs} near the WashingMachine class icon. Figure 3.13 shows how to do it.

Figure 3.12 In a class icon, you write the class's responsibilities in an area below the operations list area.

Figure 3.13 The rule in curly brackets constrains the capacity attribute to be one of three possible values.


The UML works with still another—and much more formal—way of adding constraints that make definitions more explicit. It's an entire language called Object Constraint Language (OCL). An advanced and sometimes useful tool, OCL has its own set of rules, terms, and operators.

  • + Share This
  • 🔖 Save To Your Account