So I'm currently self-teaching myself UML and I took an online quiz to help strengthen my understanding of it.
One of the questions asked:
How do you model the following situation with a UML2 class diagram:
"There are multiple different bird species, e.g. blackbird, thrush,
and starling."
And two available options were:
The diagram on the top is correct (and I understand why), however, the diagram on the bottom is incorrect. Why is this? Since the three birds inherit from the abstract class bird and conform to any abstract methods, aren't they all birds?
There's two possible answers to this I think:
The top is incorrect logically since you can't create an instance of a Bird on its own. Bird is an abstract classification of all bird species. You can only create Thrushes, Starlings and Blackbirds. I'm not sure what a Bird instance would look like, but it would be supernatural.
The bottom is incorrect syntactically because the abstract constraint should be applied after the class's name and type.
Personally I think I'd let the #2 one go (it's a mistake, who cares?) and focus on #1 which is, if I was in the business of birds, more important.
The UML 2.5 spec says on p. 98
The isAbstract property of Classifier, when true, specifies that the Classifier is abstract, i.e., has no direct instances: every instance of the abstract Classifier shall be an instance of one of its specializations.
So whether or not Bird is adorned with the keyword abstract results in the restriction of being able to not/instantiate it. If you need a Bird instance you leave it away. If you want to create an animal index you will probably have the abstract Bird which tells what you need to basically include in a bird's description. But you don't describe it by itself.
In your case, both diagrams are "correct" in general. It just depends on the context you did not explain properly. You can fulfill the requirement with both since there is no requirement that you may not instantiate Bird.
The requirement in the quiz says:
There are multiple different bird species, e.g. blackbird, thrush, and starling
But it does not says anything about what information you should hold about the bird species. So my first assumption is that the user would only be interested in the type.
The UML diagram:
is a valid solution for this requirement.
The two options in your quiz are also both valid solutions depending on how you look at the requirement. The requirement gave three examples which are all modelled explicitly in both solutions. "e.g." means there could be other birds and this fact is better described with the first solution. In the second solution you can have no instances of "Bird" that are not in the list of examples.
But this solution would only make sense if you could have some attributes and operations which are not shown - IMHO the minimum would be to keep track of the bird type - it is very awkward to retrieve this from the class name in most implementation environments.
Personally I do not think it is a good way to explain inheritance with such examples as in your quiz. Inheritance is costly in implementation (e.g. in most programming languages you end up with separate source code files per inherited class) and this cost should give you a certain benefit. This is mostly the case when the things to be depicted differ in theie attributes and behavior and you need different implementations. "Blackbird" and "Bluebird" might differ only in color (the color attribute is the discriminator). IMHO in this case it is better to have:
than
Related
I am trying to create a system for managing vaccines against covid.
The system supports 3 different vaccines but each citizen can only get one and the system has to differentiate between the citizens who are older than 65, the AstraZeneca vaccine cannot be given to people older than that age.
Below I tried to create a basic UML class diagram. However I'm pretty sure I'm missing something since the vaccine should be also connected to the AstraZeneca class?
The diagram is confusing, since it only shows associations, but regrouping them in an unexpected manner. It looks more like a decision tree than a real class diagram.
First improvements you need to consider:
Pfizer BioNTech, Moderna and AstraZeneca are each a Vaccine: you should show this with a generalization from the specific vaccine to the general vaccine.
age 65+ seems not a good candidate for a class: in most OO languanges an object of a class keeps the class during its whole life. But citizen do not change class at 65. Age is a (derived) property of Citizen. The wording "astrazeneca vaccine cannot be given to people older than 65" moreover is an expression of a constraint.
Finally, if you manage vaccines, you need to manage also shots. When you write "citizen can only get one" you probably mean "one kind": the vaccines that you mention do in principle require 2 shots. And in most countries around the world, the two shots have to be of the same vaccine, which is another contraint. The remaining question is then if 65+ constraint applies to the first shot or the second?
This would lead us to a diagram that looks as follows:
Additional thoughts:
You could manage the shots by making the association Vaccination an association class.
There is an issue in the regarding the open/closed principle: if you'd add new vaccines, you might have to add different constraints on some. Alternatives:
Make Vaccine an abstract class (or an interface), with some more operations that need to be implemented by the concrete classes: getRequiredMinAge(), getRecommendedMinAge(), getRecommendedMaxAge(), getrequiredMaxAge(), instead of hard-coding the constraint.
Use a method Vaccine::checkCompatibility(c: Citizen) transfering the constraint verification to the Vaccine class
One could wonder if subclassing the vaccines is really required.
Currently I am trying to model a UML diagram for cars. I have the problem that besides the combustion engines also electric cars exist.
When you look at the diagram, you can see that the Golf has the data type Fuel for the attribute consumes, while the e-Golf has the data type EnergyType.
How would you adapt this diagram?
Inheritance is meant differently. You already define consumes an enumeration in the abstract class. Now in the inheriting ones you do not override this attribute but just assign fixed values. Plus you use a wrong notation in that case. It would be rather consumes: Energytype = electrical energy (etc.). This type anyway is superfluous since you would have it in the class type itself. A concrete electric car is of the type you want. So that enumeration would contain the possible concrete class types (if needed at all). Now you should rather concentrate on what the different car types are. The only common thing is probably the chassis which will be defined in the abstract car.
N.B. thinking this way of cars is what the dinosaurs actually do and which is why they have so much trouble. E-cars are much more different than classic cars. Basically you need to go back to the seats for humans inside for the abstract car.
Amendment
could be a way to express a car (there are lots and lots of ways to show variants and it takes weeks and months to get to something appropriate for cars). You see that the abstract car (written in italics) has no attributes but just associations with role names. Some to abstract classes and one to a concrete class (note that this is just something meant as example). The abstract classes just have associations and contain attributes which are agreed to be common to that thing.
Now if you're building some concrete car configuration you will only have concrete classes:
The MySuperNewCar has an electric drive with 4 wheels and 2 leather seats. I repeated the abstract classes in this diagram. But that's not needed (since you probably would already guess so).
So, thats one way to describe a car. There are much more ways which need long discussions. In any way you should get a consultant aboard who's talking UML fluently (in other words who's good at modeling things).
I would advise to use different names for attributes with different types. Instead of 'consumes' you could use 'energyType' and 'fuelType'.
I have a short question:
Should I name attributes of types like a List, Arrays, Vectors or Pointers to objects (not primitive type) in the UML diagram or the only association/aggregation/composition arrows are enough?
Example: which of these diagrams is correct?
or
In UML, your second diagram would be correct if you wrote the property names at the far ends of the associations. While UML properties are allowed to be unnamed, it is not a good practice. Use association ends to indicate why the relationship exists. Sometimes more than one association must exist between one pair of classes, but for different reasons. How would you tell them apart?
The first diagram shows two properties of each type. One is named and another (at the end of each association) is unnamed. That is incorrect.
This really depends on what you're trying to convey in this architectural drawing.
The purpose of the drawing is to help reason about the structure of the software. It should not be used to represent all of the details of implementation. If you put too much detail in it, it becomes cluttered, and it is hard to keep it consistent with the source code as changes occur.
The UML drawing should be more abstract than the implementation. It should hide details on purpose, so that it conveys the external view of classes, and not how they are implemented internally. You generally don't want users of classes to assume too much about their internal implementation, therefore you don't want to expose it too much.
Also, an architecture is typically represented by several drawings - not one. Try to have each drawing focus on one level of abstraction. If you have a few high level classes that represent the main logic of the application, and many low level classes, it makes sense to have a drawing of just the high level classes separately.
Okay so I'm a bit confused as to when I should add an attribute to a class vs drawing an association to a class when trying to show a relationship in a UML diagram.
For example let's say I have a DFA class that contains 10 state objects each having a different set of paths to various states in the DFA. Should I draw the composition line from the DFA to the State class or just type all 10 states in the attribute section of the DFA class.
Basically I'm trying to figure out if when a Class A contains (or is composed of) one or more Class Bs, should I draw a line (aggregation, composition,association dependency etc) between them or just put one as an attribute to another.
This article attempts to answer the question but I get confused as soon as he starts talking about the diagrams he drew.
The article you pointed gives one guideline:
In the years I’ve been working with different modelling teams I’ve found that the rule that works best is to use Associations for Classes and Attributes for DataTypes.
A data type is a special kind of classifier, similar to a class. It differs from a class in that instances of a data type are identified only by their value.
I kind of agree with the author, adding another point: your class diagram should be understandable. If you have a diagram with many lines crisscrossing each other the diagram is useless. When this happens you must try to find relations that can be modeled using attributes without loosing too much visual information, and replace them with attributes.
Another thing that you can do with relations and not with attributes is to display bi-directional relations (you can do this with one attribute in each class of the relation, but you loose the semantics of the relation).
Are there any tools out there that let you model how a class (or a class hierarchy) can change at runtime? For example, if I have a given number of mixin classes that will be combined at runtime and I don't know which ones will be combined until the program runs, how do you go about diagramming that type of runtime behavior?
Here's a better example. Let's say that I have a base class called IceCream, and I have over 100 possible flavors that all derive from that one IceCream class. Let's also suppose that any instance of the IceCream class can be combined with another instance of the IceCream class to create a completely unique IceCream type altogether. Given this domain, how do you use a graphical model to actually say that any one of these types can be combined at runtime?
It would be inefficient to model all the possible combinations of IceCream types, given that there can be a virtually infinite number of permuations for these 100 IceCream types. So again, here's the question: Are there any graphical modeling languages that let you specify this sort of behavior?
Your design sounds a little disturbing. If two different ice creams have different behavior, then why is it wrong to model all the possibilities? Where are you loading the behaviors from? It very well could be the case, but if so I'd guess that you want to contain the behavior instead...
If they don't have different behaviors, then all you are talking is a class "IceCream" with a "Flavor" member. Never create a second class when the only difference is data--the code must actually differ in the two cases to warrant different classes.
If I totally missed something I apologize.
Edit: Let me be more specific about "Containing behavior". If each of your ice-cream flavors had a "Taste" (which is code) and the taste is different between Vanilla, Strawberry and Chocolate--then you have 3 "Taste" ice-cream classes that are contained in one "Cone" class.
The Cone class would be what I think you are trying to model as "IceCream". Since the cone contains all three, a "Lick" method can combine those three in any way possible. Either you can lick(bottom), lick(middle) or lick(top), or you can just lick() and allow the lick method to combine all three into a single call (to be more real-code, you might pass a single variable to lick() that would be forwarded to all contained flavors).
I wonder if the personal db approach of Bento or DabbleDB could be relevant for the actual modelling part. Then maybe the Django admin's model introspection for the logic part. Sounds like you want to create an interface to a scripting language. A kind of vpl library. So, a beefed up and more reflective Django admin might be a starting point.
In general, if you want to create UML class diagrams you can exploit Generic Types in UML. Also, there is the concept of Template Parameters in UML.
Have a look at this site: Defining Generics with UML Templates
They use the Eclipse Modeling Framework as a tool.