Chriss
Chriss

Reputation: 995

Specifying custom type for an attribute of a UML class

i have a class with attribute called interval and i like to give this attribute a minimum and maximum value to bound this interval,

How can represent this? Does this possible?

Upvotes: 1

Views: 2608

Answers (3)

Marco Brambilla
Marco Brambilla

Reputation: 476

If you mean you want to put a limit to a value, another option is to specify an OCL constraint over your attribute that limits its possible values.

context ClassA inv: self.attributeA >= 1 and self.attributeA <= 10

Upvotes: 2

Pete Kirkham
Pete Kirkham

Reputation: 49331

Create a value type - a classifier with the «data type» stereotype - for the compound type, then use that as the type of the attribute.

For example, this says ClassA has a public attribute called interval of type Interval, and Interval is a value type which has public min and max attributes of type double:

                                +----------------+
+-----------------------+       |   «data type»  |
|        ClassA         |       |    Interval    |
+-----------------------+       +----------------+
| + interval : Interval |       | + min : double |
+-----------------------+       | + max : double |
                                +----------------+

As Interval is a value type, its identity is not important, so it would be held by value in ClassA and would probably be implemented by a struct (if the target language supports values and structs; you can't have a value type in Java for example so the stereotype would be implemented by convention, as it is for String in Java so you have to remember not to use == on strings as their identities don't matter).

You can put a «uses» dependency from ClassA to Interval, but it is implicit from the attribute's type so it is common to omit it.

Upvotes: 4

Red Beard
Red Beard

Reputation: 3506

I mostly agree with @Pete Kirkham I would use a Datatype instead of a Classifier with the <> stereotype but if I am right it is a partial response. @Pete Kirkham truly responded to how model attribute with a min and a max bounds, I guess that @Chriss still want to know how to specify the value. For this you have several ways (depending of your meaning), you can:

  1. specify a default value for your attribute.
  2. made an object diagram, instantiated your attributed and then specified the values.

Hoping it helps ...

Upvotes: 2

Related Questions