0

In school we should draw an UML class diagramm for a class we plan to write (we should do the planning).

I now have 10 JLabels, which are attributes to the class Game and each showing a different picture, but all off them serving the same purpose. To keep it a bit clearer, the Labels will be called Player1Throw1 to Player1Throw5 and Player2Throw1 to Player2Throw5.

My question is, if there is a correct way in UML to contract these 10 labels, so that I don't have to write

Game
--------
-Player1Throw1: JLabel
-Player1Throw2: JLabel
-Player1Throw3: JLabel
-Player1Throw4: JLabel
-Player1Throw5: JLabel
-Player2Throw1: JLabel
-Player2Throw2: JLabel
-Player2Throw3: JLabel
-Player2Throw4: JLabel
-Player2Throw5: JLabel

Or in other words: Is there a way to contract these 10 lines in maybe 2 lines?

Thanks for any help!

3 Answers3

0

You should be able to generate code from the class diagram. How will you be able to do it when you do not define all the attributes separately? If you can answer that question, you know how to display them.

Designing in OOP is trying to find out if you can make the behavior more abstract. If you have 10 likewise labels, can you then make another set of classes based on a shared interface that can do the job as well? Dive into design patterns. There is a pattern that is made for this situation.

Loek Bergman
  • 2,192
  • 20
  • 18
0

You could use the following notation in UML:

- Player1Throw : JLabel[5]

- Player2Throw : JLabel[5]

www.admiraalit.nl
  • 5,768
  • 1
  • 17
  • 32
0

Don't use global variables for local attributes. On code level use collections (arrays, sets, bags, etc.) for multiple objects of the same role.

You should define a class Player (which as I understand is a part of Game) and class Game should contain a collection (array?) player of two objects of type Player. Player should have an attribute throw with multiplicity 5 (or maybe 0..5 - I don't know details of your project, however on code level it doesn't really impact class structure) of type JLabel.

Thus your UML class diagram will look like that

class diagram for answer

Ister
  • 5,958
  • 17
  • 26
  • I have those two classes (they are even named the same). As I understand it, you mean, I should make the JLabels attributes of the Players? I already met the dead line for the project (it's just a small 4 week project) and ended up having all GUI-related attributes in the Game class. I guess, that's not a nice solution then? – Streusselhirni Jun 10 '16 at 13:12
  • Yes. Also don't create attributes throw1, throw2, etc. Use some sort of collection (List/Set/Bag/Array/Whatever). This is extremely useful when you need to loop such list, but can also make code reuse easier. And of course no, having all GUI related attributes in the Game class is a very poor solution (you should have classes dedicated to that), however my knowledge of GUI development is limited. – Ister Jun 10 '16 at 22:27