2

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

2 Answers2

1

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.

Spooky
  • 2,966
  • 8
  • 27
  • 41
Doug Knesek
  • 6,527
  • 3
  • 21
  • 26
  • yep. that's what I meant with "adding a constraint" :-) Problem is that you can't transform this directly into code. – rdmueller Nov 18 '09 at 22:27
0

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.

rdmueller
  • 10,742
  • 10
  • 69
  • 126
  • 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 Nov 18 '09 at 17:07
  • 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 Nov 18 '09 at 17:12
  • the attributes have types. like String 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. – rdmueller Nov 18 '09 at 17:22