1

While reading Craig Larman's Book (Applying UML and Patterns;...) I noticed he added instances of Die class as attributes in the DiceGame class.


Image of Partial Class Diagram of DiceGame.


Is it possible? and there are many related Programming Questions where they negate to do so.

Community
  • 1
  • 1
M.D Shaw
  • 28
  • 1
  • 7
  • I have no idea why this has been nominated as unclear. The OP obviously had some misunderstanding about instances. But hey, we're here to clarify such kind of issues. – qwerty_so May 30 '16 at 20:37
  • I thought it was relatively clear as well. I don't have voting rights or I'd help overturn this. – Jim L. May 30 '16 at 22:23

5 Answers5

3

These are not instances of Die but simply two properties of type Die. The picture is not a very good use of UML (shame on Larman). The association between DiceGame and Die use a 2 multiplicity. And most likely he means die1 and die2 with that. But that would be guessing. A better notation would be

enter image description here

with appropriate association-end names.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
  • That's how I was going to answer, but you beat me to it. I would probably make the associations unidirectional, though, unless a die is asynchronous for some reason and sends signals back to the game. – Jim L. May 30 '16 at 19:56
  • @JimL. I should let you a bit more time :-) Honestly I have very much spare time, so I'm on the fast lane. However, your answers are often more thoroughly expressed than mine. Unfortunately they closed this one. It was in no way unclear to me. I'll vote for re-opening so you could answer and I'll take mine back. – qwerty_so May 30 '16 at 20:35
  • Thanks for the kind thoughts, Thomas! – Jim L. May 30 '16 at 22:21
  • @JimL. So it's open again. Feel free to post your answer :-) – qwerty_so May 31 '16 at 08:57
  • @ThomasKilian can you recommend another better book for **OOA/D** – M.D Shaw May 31 '16 at 17:01
  • Somebody else just asked and I recommended Doug Rosenberg and ICONIX which helped me some 15 years ago. That was a video tutorial along with a Sparx EA model. No idea whether that's still available. – qwerty_so May 31 '16 at 18:35
1

Short answer: yes... and it happens very often.

In OO there's a neat little trick to figure out if a class should contain a property of some other type of class."Is-A" and "Has-A".

In this case you have a DiceGame class. You ask yourself, "Is-A" DiceGame a Die, or does DiceGame "Has-A" Die. If the answer is the former, then you use inheritance, otherwise you have it set as a property(ies) as in the example you posted.

If you had another class called "GamblingRoom", having a DiceGame a property of that "GamblingRoom" class "could" be a viable solution....depending on what you need to do of course.

Here's a better explanation of it in a similar question to what you have.. but probably better explained than what I did here ;)

HAS-A, IS-A terminology in object oriented language

Community
  • 1
  • 1
dvsoukup
  • 1,586
  • 15
  • 31
  • How is this answer relevant to the question? – Jim L. May 31 '16 at 16:30
  • @JimL. "**How** was it made" is explained well, specially _HAS-A_ and _IS-A_ terminologies helped me. – M.D Shaw May 31 '16 at 16:37
  • @JimL.You asked if this is possible. I gave you the answer plus a little extra to understand "why" it's that way... and the has-a is-a terminology can help you to understand that. – dvsoukup May 31 '16 at 17:11
1

I would change the question to, "Can references to any class be treated as attributes in another class?" The answer is technically "yes", but you shouldn't model it that way in UML. You should model simple values as attributes (inside the class' rectangle), but model non-simple references as associations. Simple values are identified only by their values (e.g., the number 5), whereas non-simple references point to things with identity. In your case, each Die should have an identity other than its current face value. Therefore, you should not model attributes of type Die, you should model a property at the end of an association.

In the diagram you posted, there are three (!!) properties of type Die. One is called die1, one is called die2, and another is unnamed with a multiplicity of 2. In a programming language, that would give you four places to store references to your dice, which is obviously incorrect.

The way I would model it is as follows:

enter image description here

When you generate code (or transcribe manually), the dice property becomes a member variable within DiceGame. That member variable is usually typed by a collection in your programming language, but it could also be modeled as an array. That collection or array allows you to access both of the dice.

Note that the association is unidirectional. That is because there is probably no need for the Die to access the Dice Game. (However, if you needed each Die to be an active object running in its own thread, you may need the association to be bidirectional so each Die can send a signal when it's done rolling or something.)

Jim L.
  • 6,177
  • 3
  • 21
  • 47
0

Yes it is possible. OOP provides a container relationship, which means has a relation in which one class can contain instances of another class.

For example, there can be a chair class and a table class and a room class. Now Room can have chairs and tables so room class will have instance of chairs and tables.

TylerH
  • 20,799
  • 66
  • 75
  • 101
nirali.gandhi
  • 221
  • 1
  • 11
0

It's not only possible, it's the standard case. In fact, many programming languages make no big difference between classes and elementary data types like int.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45