Operations
A classifier can have any number of operations or none at all. Operations define the behavior of an instance of a classifier, typically a class, but also of other classifiers, such as use cases, or components.
You can add operations to identify the behavior of many types of classifier in your model. In classes, operations are implementations of functions that an object might be required to perform. Well-defined operations perform a single task.
Operations can have exceptions, elements that are created when the operation encounters an error.
Every operation in a classifier must have a unique signature. A signature comprises the operation's name and its ordered list of parameter types. The UML syntax for an operation name is as follows:
visibility «stereotype» name(parameter list) : return-type
For example, in an e-commerce application a Customer class has the following operation: - getBalance([in] day: Date) : MoneyType. The operation signature describes the information in the following table:
Part of syntax | Example | Description |
---|---|---|
visibility | - | The minus sign indicates that the operation has private visibility. This operation cannot be called from other classes. |
name | getBalance | The name describes the operation according to the service that it provides. |
parameter list | ([in] day: Date) | This operation has one input parameter called day followed by its type, Date. You can display the complete signature (including the parameter list) or just the operation's name. |
return-type | : MoneyType | The type returned by the operation is an instance of the MoneyType class. |
Example
In an e-commerce application, a Cart class adds and removes merchandise to a virtual shopping cart. An operation, such as addItem( ) adds merchandise to the cart and a removeItem( ) operation removes merchandise.