UML Representing an anonymous class in class diagr

2019-02-13 14:25发布

问题:

How would one represent a Java anonymous class in a UML class diagram?

回答1:

Inner (nested) classes are represented in UML with an association adorned with a crossed circle.

Illustration:

Source:

  • UML Class Diagrams for Java Programmers, by Robert C. Martin on InformIT.


回答2:

There are two really anonymous classes in Java First is the non-named inner class. Such as:

class BGThread<T>{...}
...
class TitleEditDlg{
    new BGThread<Props>(cont, true) {
        @Override
        public Props run() {
            ...
        }
    }
 }

A citation from UML standard 2.5 (p.149):

The standard notation for an anonymous InstanceSpecification of an unnamed Classifier is an underlined colon (‘:’).

So, as for anonymous java class, you should create a class block with only : as name and connect the container class to it twice - by container relationship and by one-direction arrow without a dot. From the other side, the : block should connect to the parent class.


According to the same source, an Anonymous Bound Class, that is the second anonymous class we meet in Java, but often do not notice it, when you use a template/generic class, as in

class BGThread<T>{...}
...
class TitleEditDlg{
   BGThread<String> newThread= new BGThread<String>();
}    

can be shown by two ways:

  • As bind dependency, with substitution on it.
  • As an intermediate class, with the name of the parent class and substitution in angle brackets. Notice, here the class is anonymous, but the attribute has a name. So, this way you are showing more information.



回答3:

I was looking for a way to represent a JavaScript object literal in a class diagram (I know it doesn't make much sense...)and I found this post.

I'd contribute with this link and image. Ciao.

http://www.uml-diagrams.org/class-diagrams.html



标签: java oop uml