Author Topic: Class diagram Multipicity interpret to code  (Read 1438 times)

dratengon

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Class diagram Multipicity interpret to code
« on: June 27, 2012, 07:19:09 pm »
how to interpret class diagram with Multipicity 1..* into coding?
Example :
1. class A associated 1..* Class B?
2. 1. class A associated 1,2,6..* Class B?

Makulik

  • EA User
  • **
  • Posts: 400
  • Karma: +0/-0
    • View Profile
Re: Class diagram Multipicity interpret to code
« Reply #1 on: June 27, 2012, 07:33:06 pm »
That would depend on the association type. Could you provide a picture of the class diagram in question?

KP

  • EA Administrator
  • EA Expert
  • *****
  • Posts: 2549
  • Karma: +34/-2
    • View Profile
Re: Class diagram Multipicity interpret to code
« Reply #2 on: June 28, 2012, 09:32:10 am »
Quote
how to interpret class diagram with Multipicity 1..* into coding?
Example :
1. class A associated 1..* Class B?
2. 1. class A associated 1,2,6..* Class B?
This may or may not be a homework question, but if it is, I've never seen a multiplicity of [1,2,6..*] before so it's an interesting homework question! It isn't valid UML because UML only allows a lower and upper bound for multiplicities, but hey who's complaining?

The association represents that an object of class A is associated with a set of objects of class B. The multiplicity constrains the size of the set (with '*' meaning 'unlimited').

So to code example 1, just use your favourite collection class in your chosen programming language. To code example 2, do the same but you may need to add code to handle the error cases where the size of the collection is 3, 4 or 5. In both cases you will need to consider the error case where the collection is empty.
The Sparx Team
support@sparxsystems.com

dratengon

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Class diagram Multipicity interpret to code
« Reply #3 on: July 02, 2012, 04:16:04 pm »
For Q 1, (Sorry i dont know how to attach img from pc)
_______                 __________
            |                |
class A  |----------> | Class B
            | 1       1..* |
_______|                |_________

isn't something like below? if not, mind show the correct way?
class A{
  vector<B> b;
  b.add(new B());
};

class B{

};




For Q2, i was heard from friend that he got this from interview Question.

How to translate to coding? (any language will do , e.g. Java or C++)


  
« Last Edit: July 02, 2012, 04:49:57 pm by dratengon »

KP

  • EA Administrator
  • EA Expert
  • *****
  • Posts: 2549
  • Karma: +34/-2
    • View Profile
Re: Class diagram Multipicity interpret to code
« Reply #4 on: July 03, 2012, 10:36:01 am »
In my previous answer I said "just use your favourite collection class in your chosen programming language". You chose vector, which will do fine. For question 2, the same code will do, but you also need to decide how you are going to handle the error condition if there are 3, 4 or 5 objects in the vector.
The Sparx Team
support@sparxsystems.com

dratengon

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Class diagram Multipicity interpret to code
« Reply #5 on: July 04, 2012, 06:55:55 pm »
coding wise, isn;t same like composition or agg?
_______                 __________
           |                |
class A  |<>------> | Class B
           | 1       1..* |
_______|                |_________

qwerty

  • EA Guru
  • *****
  • Posts: 9836
  • Karma: +190/-155
  • I'm no guru at all
    • View Profile
Re: Class diagram Multipicity interpret to code
« Reply #6 on: July 04, 2012, 09:11:40 pm »
Climb down your coding tree. Aggregation / composition is about semantics. The way you code it is about bits.

q.