How would I properly illustrate the relationship a class has with an enclosing class in UML?
I have a Game
class and in that class exists a KeyInputHandler
class. I believe this is a "has a" relationship but I a开发者_StackOverflow社区m 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
}
}
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).
I have reversed your java code in my UML tool and got the following result. Hope this help !!
alt text http://www.forum-omondo.com/inner_class_description.png
The UML specification 2.5b1 (the latest) you can find at http://www.omg.org/spec/UML/2.5/Beta1/ says that: A Class acts as the namespace for various kinds of Classifiers defined within its scope, including Classes. Nested Classifiers are members of the namespace of the containing Class. Classifier nesting is used for reasons of information hiding. Nested Classifiers are used like any other Classifier in the containing Class. The property of the Class meta-class used to list the class contained inside a class is called nestingClass, as depicted by the meta-model shown in figure 11.15 pag. 202
Please note (very carefully) that a containment association between two class involves their instances NOT the classes.
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".
精彩评论