views:

83

answers:

3

Hello!

Very new to UML and system design.

We're at the stage of doing a class diagram in the design phase and are struggling to figure out how to implement a requirement. Here's the problem:

We have a Person class that is composed of a Name class and a ContactDetails class. Contact details class is composed of a MobileNumber and an EmailAddress class.

How would I model my class diagram so that ContactDetails must have either MobileNumber or EmailAddress or both?

I'm sure it's simple - but it's thrown me!

Thanks

A: 

let's start simple:

Model your classes as you would write your program. Create a ContactDetails class and add two attributes "MobileNumber" and "EmailAddress", both of the corresponding type. Now create your constructor methods:

+ContactDetails(mn MobileNumber)
+ContactDetails(email EmailAddress)
+ContactDetails(mn MobileNumber, email EmailAddress)

Now: how would you add the needed constraint in code?

By setting the empty constructor as private? Do so!

-ContactDetails()

By checking that the parameters are not empty? Just add a Constraint element which basically just a text note to your class.

That's it.

Ralf
So, the attributes are there and we have three constructors:ContactDetails(MobileNumber, Email);ContactDetails(Email);ContactDetails(MobileNumber);One of the three have to be called to populate the attributes? I get it!Not quite sure what you mean about the Constraint element - this?http://publib.boulder.ibm.com/infocenter/rsmhelp/v7r0m0/index.jsp?topic=/com.ibm.xtools.modeler.doc/topics/cconstrnt.html
Lord Kinboat
Sorry, I think I've misread: I need to split MobileNumber and Email into separate classes. It's the way we need to do it - they need to be separated. How to do it via the Class Diagram?
Lord Kinboat
the attributes have types. likeString s;Integer b;So s is implemented in the class "String", b in the Class "Integer". That's how you put them in separate classes, but reference them from your ContactDetails. In Class Diagrams you have two main relationships. This one is called "has a ..." relationshipt. Inheritance (Generalisation)is called "is a ..." relationship.
Ralf
+1  A: 

alt text

Also, if an association is optional, it is not a composition. It is at most an aggregation, or even a simple association as I show in this diagram.

Doug Knesek
yep. that's what I meant with "adding a constraint" :-) Problem is that you can't transform this directly into code.
Ralf
A: 

We're using Rational Rose.

The problem is that I'm not modelling that Contact Details MUST have either a MobileNumber OR an EmailAddress OR BOTH.

How do I model that one class must comprise of A OR B OR A&B, effectively?

Lord Kinboat
The class diagram is a 1:1 mapping of your code class structure (not behaviour!) to a diagram. Thies means that you can design the structure in a class diagram with the UML notation, but not the behaviour. This can only be done - indirect: hide the standard contructor and only allow contructors which fit to your behaviour- with constraints (see othe answer with diagram example)- with commentsAlways remember: a diagram is only one (of many) view on your model. The model is not the reality. Only your code will reflect the reality you have on your mind.
Ralf