How to show Data transfer object on a UML class diagram? - uml

How do I show Data transfer object in a UML class diagram?
So can someone give an example of a UML class diagram with a DTO.

A data transfer object is in principle an object like any other. As a consequence, you would probably want to represent a DTO class, with all the attributes needed and some operations to convert to and from a data transfer representation (e.g. a data format).
You may have the following additional needs specific to the DTO:
to highlight that it's a DTO object and avoid confusion with domain objects, you could use an UML profile with a «DTO» stereotype that you could show above the class name.
you could highlight the dependency to the class(es) that the DTO is supposed to transfer.
If the DTO contains nested objects, you might have to define classes for them as well, with the related associations.
If a DTO just combines one or several objects of a couple of related classes, you could as well just show the related objects as attributes of the DTO with their original type, and if needed, with multiplicity.
In some designs however, the DTO object is not really of a different class: it may just be a different representation of the same objects grouped using some special format/encoding (JSON, XML). In this case, from the design perspective it's not necessary, nor even desirable to create distinct classes: it'd be the same classes, except that the domain objects are "dehydrated" for storage/transport purpose (i.e. attributes/state without behaviors, until objects are properly extracted from the DTO) .

Related

Normalization versus multiple inheritance

I have to model a situation where I would like to use specializations to ensure classes are somewhat normalized, but:
Risk multiple inheritance problems, especially in the long run
Will need to derive an XML-compliant UML model from it (a.o., only one superclass allowed)
The simplified situation is as follows (see also diagram below): we have Parts, like doors, bolts, wheels, etc., and Tools, like drills, ladders, and bigger machinery. All of these may be used in generic processes, like Orders, Shipments, etc. As such, I would like to have one superclass (Powertype, maybe?) that represents them, say Item.
Both Tools and Parts have specialized classes that carry a serial number. As such, I figured that a SerializedItem class with a SerialNumber, which both SerializedPart and SerializedTool inherit, would ensure that all serialized 'things' we have carry at least the same information. However, I also need these Serialized items to carry at least the same information as their more generic parts, and hence I would introduce multiple inheritance.
I have considered making the Item classes interfaces. This would at least mitigate some (many, all?) multiple inheritance problems. This is where another however comes in: aside from an attribute SerialNumber, I would also like to enforce that all Serialized specializations have an aggregation relation with a Manufacturer. Aggregation to an interface is not allowed, so I feel like I cannot with one relation to the superclass enforce this relation.
As such, I have the following considerations/problems:
Have two disjoint 'branches' of Item, with little to no technical governance on content of Serialized specializations
Item classes as Interfaces, but then little governance w.r.t. use of Manufacturer by Serialized specializations
All concrete classes, but then there exist multiple inheritance issues which must be solved when trying to derive XML classes from the model
Which option would you prefer, and why? Did I miss any considerations?
If you want to have a (platform-independent) information design model (similar in spirit to a conceptual model), then you should use multiple-inheritance if this reflects the concepts of your problem domain.
According to such a model-based engineering approach, your model is a pretty good design model that can be used as a basis for making (platform-specific) implementation models such as, e.g., a Java class model or an XML Schema model.
For making an XML Schema model, you would have to choose a certain mapping. In particular, you need to choose a mapping for resolving the multiple inheritance pattern, see also https://stackoverflow.com/a/27102169/2795909.
I just would not make SerializedItem a superclass. Nothing is a serialized thing which generalization would mean. Things can conform to a serialization protocol which is the same as implementing an interface (maybe called Serializable). If you happen to deal with serializable things without bothering about their content you would just deal with Serializable and only know the number.
Basically you should make your SerializedItem an interface (eventually renaming it to Serializable), remove the generalization upwards and make the two horizontal ones realizations.
This is probably not an ultima ratio. But to me this approach sounds more reasonable.

UML Class Diagrams - Understanding Which Fields are Necessary and When To Have Public Fields

I'm currently working on a UML class diagram for an application which is supposed to be like 'Duolingo'.
I am struggling on how to model a many to many relationship.
So, I imagine that you have many users which can take many courses (different languages that they wish to learn). For this reason I have decided to create a courseProgress class to model this many to many relationship.
What I was wondering is, do I need to store the userID and courseID in my courseProgress class? I think I'm getting mixed up here with how keys may be used in a database.
See below diagram:
Am I along the right tracks?
Also, I was wondering when exactly you would use private and public fields. Because to me it seems that you would always want all fields to be private and just use getters and setters to always access these fields?
N.B in the above diagram the fields are public as I have not yet changed them to private
In the diagram above, should I have the userID field and courseID field or should I have a user field of type User and course field of type Course?
You are indeed on the right track. The additional class CourseProgress helps you to better represent the many-to-many association between User and Course. An alternative could have been the use of an association class.
The choice between public, protected or private properties depends on your class design and how you want to expose this information in the object model. This is far too broad to be explained here. To simplify, if the properties are data that could be changed by other objects without any consequence, then you could let it public. If however some properties can only be changed according to some rules with pre-conditions, invariants or post-conditions to be guaranteed, you'd better control the change via a method and thus make the property proteted or private.
Whether or not to indicate the identifiers of the associated classes (i.e. courseId, UserId) depend on the purpose of your diagram.
Typically, for a domain model or a design model, you wouldn't add the properties for representing the classes you are associated with. This is an implementation detail of the association. Usually, you'd rather use the association end to indicate how the instance of the related class would be called.
For an implementation model (example for one-to-many or many-to-many), you may want to show this detail to allow an unambiguous mapping with database tables.

UML Class - Using the base class when there are no derived classes

Hello I'm trying to prepare an ER diagram + a class diagram.
The system is very simple. It involves a simple flight reservation system with two roles (actors).
In the ERD, we can distinguish between the two roles using the "IsAdmin" attribute("true" for admin, and "false" for customer).
In the class diagram, I have a base class named "User" (containing all the attributes and methods of the customer role and all the attributes and some of the methods of the admin role).
Should I use the User class when dealing with the customer entity (since the attributes and methods are the same), or should I create an empty class derived from the User class for clarity?
It depends...
Basically when dealing with business objects I would not use a class named "User" which says all or nothing. You have a "Customer" and an "Admin" and likely many other "User"s. Don't make the "User" a "Customer".
Setting that aside, if you are using a serialization framework that often requires table and class to have the same name.
Also you shouldn't start a system with optimization. Give clarity an advantage in favor of that. It will pay out more sooner than later. And in any case: you can optimize at later stages as well.
ER Diagrams (ERDs) and UML Class Diagrams (CDs) are both information modeling languages that essentially cover the same concepts, but using a different visual syntax. Both allow modeling classes with properties/attributes (and operations/methods in the case of CDs) defining entity types, and associations (or relationship types).
Since CDs have been defined more recently, they are more expressive than, and subsume, ERDs. Consequently, there is no need to duplicate an information model both as an ERD and a CD. Rather, you should make a logical class design diagram from which you can obtain both OOP class models (defining, e.g., Java classes) and RDB table models.
If needed, you can derive an ERD from your CD.

Is it Good to have Hibernate and Jaxb annotation both on the same class

I m trying to build a rest easy service with hibernate. Is it Good to have Hibernate and Jaxb be annotation both on the same class. OR there should be two different classes one for hibernate data object with annotation and another similar class for rest request and response with jaxb annotation.
The question is, basically if you need extra transfer objects next to your entities.
If you don't, the structure of your tranfer data (JSON, XML, whatever) will be more or less dictated of how your entities are structured. You can achieve a lot with annotations but you'll still be somewhat bound. As a consequence of this, changes in the entities may need to be propagated to your outer interfaces. Basically, if you change your entities and/or your database schema, you may also need to change the structure of the JSON returned by your REST interface.
Having separate DTOs is safer in cases when you need to provide stability of your interfaces. The downside is that you'll need mapping code to convert between DTOs and entities.
From my experience, you can get away with just entities most of the time.

UML class diagrams: References to instances?

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.

Resources