tags:

views:

40

answers:

3

I have a Game class and in that class exists a KeyInputHandler class. I believe this is a "has a" relationship but I am not 100% sure. If this is indeed a "has a" relationship it would be composition and I know what symbols to use, but if it is something else then I could use some advice.

EDIT:

This is the structure of the two classes I am concerned with.

//Import stuff

public class Game extends Canvas { //The Game class

    public Game (){ //Game constructor

    }

    //Other methods and stuff will go here

    private class KeyInputHandler extends KeyAdapter {
        //This is the only other class in my Game, and
        //you can see it is enclosed in the Game class
    }

    public static void main(String[] args) {
        Game game = new Game(); // Creates a new game
    }

}
+1  A: 

From your description, it looks like a Composition relationship.

  • cardinality is 1:1 (degenerate case of 1:many)
  • lifecycle of composed class (KeyInputHandler) is controlled by composing class (Game)

Assuming those are correct then Composition is appropriate.

A related question worth asking is how you'd describe the relationship among them. What role does KeyInputHandler play with respect to Game? That will help the reader understand the "why" of the relationship, not just the "what".

sfinnie
A: 

The Game class here serves like the top-level container, very much like a package I'd say. I guess KeyInputHandler is not a static class, and directly calls "back" into Game?

In that case, either you directly have a field of type KeyInputHandler, or you pass an instance to an environment like SWT that will call it back.

So an association with containment-type composite as @sfinnie suggest would be appropriate. However, there's another possible view in my opinion: you could also treat it from a component-point of view. In that perspective, your Game class becomes a component that requires an environment for input (SWT, keyboard, ...). So in this more structural view, you are combining Game, KeyInputHandler, and e.g. SWT.

Usually in the components-world of Software Engineering, these are exactly those things that have a stable 1:1 relation (stable in the sense that you will be talking to the same object for your entire lifetime).

Maybe you could elaborate how tightly KeyInputHandler and Game are coupled, i.e. how difficult it would be to put into a separate class, outside of Game. That way, you can also gain inside of how they are related (but always remember: "You ain't gonna need it", so if your design works fine, keep it ---we're only talking about the conceptual level here).

ShiDoiSi
A: 

I have reversed your java code in my UML tool and got the following result. Hope this help !!

alt text

Hummm, that is different. I have never seen a class actually drawn inside another class in UML.
typoknig
You can display the class inside or outside the other class but the structural class diagram view should be a class displayed inside another class. This is what I use with Java but could be different with other language