I am reverse engineering some Java code into a class diagram. Now I'm wondering how to model classes that are from a library that I didn't design. If I'm writing them down as classes, I should maybe also know what interfaces they implement, etc, and put that in the diagram. How far do I go with this? Is it better to write them down as attributes of my own classes?
Include whatever classes and interfaces your code uses in the diagram for your code.
Place the elements from the library into a package which represents the library's package.
Use whatever level of detail is sufficient for what the diagram represents.
If you only want to record that your code uses the library, a «uses» relationship to the package representing the library
If you have associations to elements in the library, model the elements you relate to but no detail in them.
If you or extend or implement classes or interfaces, model the attributes and operations of those elements.
If your code relies on a sequence of operations on elements in the library, you may model the operations, or may just use messages which are not directly linked to them, depending what your tool allows.
I found an interesting link which could help: http://www.ejb3.org/jar_file_reverse/jar_file_reverse.html
You can reverse your java project to show .java classes and then just drag and drop .class classes coming from the jar to your diagram. You can make a differentiation between them by adding the package name in the class name, or by changing the color of the class etc....
Related
I'm working on a project where I'm using an SDK that provides things I need via some classes. I've named these ProviderProvider and Provider, that grant access to OneThing that is an IThing.
An example is shown here
.
My question is: What is the correct (or best) way to show that the Model provides the list of OneThings to the View through those classes?
Do you show this explicitly as in my example, by drawing a dependency arrow from Model to OneThing? That doesn't seem right to me and quickly becomes visually cluttered.
Do you not explicitly define that relationship, but is it simply implicitly defined through the other relationships?
Do you define that relationship semi-explicitly through attributes, notes or some other way?
What relations and attributes should I add/remove specifically and why?
You already have the implicit relationship since you use this class as a type of data returned by Model so you do not need to add that relationship explicitly.
It may be useful though, especially for classes that are core in the system, to add a diagram with dependencies only. Then you don't care about relationships between other classes, you only show on one diagram all classes that depend on the core one (it may be even more than one diagram).
One hint - in Case tool (like EA) even if you don't intend to show the relationship on the diagram since it is indicated implicitly it is still good to create the explicit dependency and just remove the arrow. This will support the traceability through tools like traceability matrix or dependency tree.
I have a .lib file containing internal C++ classes and a few C++ classes are interfaces to the functionality that software library provides.
How should I illustrate this with UML?
Using Enterprise Architect, I tried type 'Component' (pasted to the component diagram as simple link) and then dragged and dropped the internal classes of that library from the model tree to that component rectangle as element type Composite part, and the interface classes of that library as Port. Now the weird thing is what happened the model tree contains kind of object instances of that classes called Part1 and Port1 of UML type 'class', which is nonsense since a software library don't instantiate, it just provides its classes via .lib file, like a pool. I think UML element type 'component' is not the right thing here, is it?
The same happens if I use 'Package', also here parts and ports become things like object instances although classes in a library are not.
Following my comment to the O/P, I'd do something like the below. Please note that I'd probably not depict all of this on the same diagram, the class content of the package for example I might present on a separate diagram so that this is not so cluttered. But the logic is the same.
Update: Following commentary below I've added the this diagram which illustrates an alternative way of representing this. I think in some cases I've seen the delegation represented as a dependency rather than a reference association too. But hey ho. Internally, components are represented as interacting between each other via required/provided interfaces (though this is not shown as in the model below we only have one internal component illustrated).
It's not weird. You happened to drop a class as Part (in EA terms).
That's actually a Property. Just open its properties:
See UML 2.5 on p. 147:
9.9.17 Property [Class]
9.9.17.1 Description
A Property is a StructuralFeature. A Property related by ownedAttribute to a Classifier (other than an association) represents an attribute and might also represent an association end. ...
I'd probably just drop them as link to appear as class inside the enlarged component, YMMV
A class dropped as port will show as what the name said. So no confusion here. Anyway, I'd also drop it consistently as link to show just the contents. Further I'd probably stereotype the component itself with <<lib>> or the like. That's because you actually use a lib during compile time to extract the needed parts and it does not work as component by itself. A library though has an interface, namely to extract the compiled class modules it has inside.
Ah, and as #muszeo said: use ctrl-drag to show the above dialog.
Given I have two Java classes Foo and Bar, and Bar has a getFoos() method that returns a List<Foo>.
In Java, all classes extend the parent class Object. List is an interface that extends the interface Collection, which in turn extends Iterable. All of these are provided by the Java core and do not require programming. However, they have methods that can be used or overridden. Interface hierarchy may be necessary to be known, for example a method that takes an Iterable<Foo> will accept a List<Foo>.
When drawing a class diagram from these in UML, how do I do with the well-known classes (Object) and interfaces (List, and its super interfaces Collection and Iterable)? Do I have to draw them into the diagram as well? Can I skip them completely? Is there a special symbol for these (a cloud would feel great to me)? What about not-that-common classes that should be qualified (i.e. if it would be an java.awt.List and not a java.util.List)?
UML is not linked to any language, there is nothing in the norm about JDK classes.
A lot's of tools provide a profile with JDK classes already defined.
So you have to look in your tool if you get menu like "import profile" and look which profiles are available.
Two others solutions could be:
Define in your models the minimum set of JDK classes you need
Trying to write a java program to generate the XMI of JDK classes. Theoretically not so complicated, but the JDK includes JNI classes on which the introspection can not be used.
Your choice for how to deal with well-known classes will depend on how you plan on using your UML class diagram.
If the main purpose for creating a UML diagram is to communicate design details to a team, I would opt for not including well-know classes (not just from the JDK but also classes that are well-known in your context). The reason being that you want your message to be focused. Adding these well-known classes can quickly clutter your diagram while adding nothing to your message. For the same reason I typically exclude getters/setters and any information that is not essential to understanding the design. For classes that are not well-known I will include them because they communicate information that is not easily accessible to the team.
If your class diagram is going to be used in MDA, i.e. it will be used to generate a system implementation, then you have to include well-known classes.
What is the best way to represent a "mixin" using UML?
As documented in this article:
In object-oriented programming languages, a mixin refers to a defined
amount of functionality which can be added to a class. An important
aspect of this is that it makes it possible to concentrate more on the
properties of a particular behaviour than on the inheritance
structures during development.
I will give more details about my particular use case.
I have a collection of classes that model different types of objects. Because all of them can be stored on a storage, I want to use a mixin to implement all the functionality related to "being stored".
Of course, I can use abstract classes but I do not like it because these classes should be part of a different hierarchy of classes and the fact that they can be stored is only a secondary property.
Another option can be to use composition and add the "storage node" as a field of this classes. I do not like this option either for the same reason: I do not want to create any dependency between the classes and the storage.
I have already implemented the solution in Java using a mixin based on dynamic proxies and I would like to document the solution with a clear UML class diagram. Is there a standard way to represent this mixin?
I am also wondering whether it is a good idea to model also how the mixin has been implemented (using proxies) or it is better to use a more abstract representation.
Thanks
Actually there are many ways to model this in UML:
One approach could be to stereotype the operations and properties with <<mixin>> or the like and then use tagged values to describe where you got them from.
Another (I'd prefer) is to actually use a <<mixin>> stereotyped Generalization and attach a note to that telling which operations/properties should be mixed. That would give the implementer a guide to just "lean implementation of the general class".
Eventually you could create <<mixin>> sub-classes with subsets of the ops/props you want to mix in the final class and then Generalize from those.
Probably one could come up with more solutions. Use an approach which suits you best. There is not generic mixin pattern in UML (to my knowledge).
Some classes in my class diagram need to reference specific instances of other classes or themselves. Should I just model those the same way i would in an object diagram or is there a better option? In general: Can one combine class and object diagrams, since object diagrams are themselves class diagrams?
thanks!
UML2 introduced Composite Structure Diagram exactly to solve this issue.
In a Composite Structure Diagram you can show classifiers (e.g. Classes) together with theirs internal composition in terms of instances.
This way you can specify exactly which instances are linked to your objects.
See this article for a good explanation.
I agree with #Thomas Killian : you want to mix two different representations.
On the class diagram you will be able to show cardinalities, but not instances relationships. It seems your List class is not a simple list but a chain / linked list.
A ListElement is in fact part of a LinkedList. Two predecessor and successor attributes themselves of type ListElement (or how you wish to call this class, the node role could be noted) will be enough to suggest the behavior. As an attribute has a 1:1 cardinality with the encapsulating class, this respects what you wish to model. Renaming the list class would be a hint for the diagram reader.
Take a look at the Java Linked List source if you want some ideas: this could help you to design a clear class structure, the initial author is not so bad at classes design. In this case, he pushes encapsulation to the excess but the idea is right.