What is appropriate UML class relationship? - uml

In my case, what is appropriate relationship between CarBuilder and SuperCar (SportCar as well)?
Explanation: CarBuilder holds an array of Car class's instance but it doesn't construct any instance of Car class. Instead, it constructs SportCar, and SuperCar classes' instances by directly call CreateInstance() method of these two classes.
Class Diagram:CarBuilder

In your question you are describing three different relationships -- a compositional one (that you already have), and then two others to the constructors of SuperCar and SportCar. So I'd just add an association to SuperCar and SportCar from CarBuilder.
Incidentally, it seems unlikely that you'd only ever have a car composed within CarBuilder -- maybe you mean a (weak) aggregate rather than a composition? Surely a car can exist outside CarBuilder? Also, this looks like a partial implementation of the strategy pattern, might be worth looking whether that's relevant.

Related

How to represent in UML that subclasses of an abstract class should be singleton

Suppose an abstract class X and its subclasses Y and Z. How do I represent in UML class diagrams that Y and Z should be singletons. Is it possible to represent that all X subclasses must be singletons?
To specify that all subclasses of X are singletons, you can write a constraint in between braces: { every subclass of X is a singleton }. This constraint should be put in a constraints compartment in the class rectangle.
The UML 2.5 specification, §7.6.4 defines the notation for constraints in general and §9.2.4 specifies how to show the constraints of a classifier:
If a Classifier owns Constraints, a conforming tool may implement a compartment to show the owned Constraints listed
within a separate compartment of the owning Classifier’s rectangle. The name of this optional compartment is
“constraints.”
Alternatively, you could give a singleton indication on each and every subclass of X. From your wording, I assume that that is not what you want. Anyway, the latest version of UML (2.5.1) does not have a standard way to indicate that a class is a singleton. Some people indicate it by writing 1 in the top right corner of the rectangle. However, that is not valid UML. You may use that for parts, but not for classes. Instead, you could invent your own stereotype ≪singleton≫.
There is another StackOverflow question about this topic.
Here's another possibility: you can adorn the class with a <<singleton>> stereotype. I always used it that way and the coder knows how to handle that. It's no UML standard, but see the last sentence.
§11.4.4 of the UML 2.5 spec says:
A usage dependency may relate an InstanceSpecification to a
constructor for a Class, describing the single value returned by the
constructor Operation. The Operation is the client, the created
instance the supplier.
If you create a GeneralizationSet that has the meta-property isComplete=true (to say that all possible subclasses are accounted for), and you connect one InstanceSpecification to each constructor by a usage dependency, the model means that every class is a singleton.

UML dependency or association in class diagram

I have class Controller in my java project, which has method like this:
As you can see, in the first line I am getting Singleton instance of ActualModes class and call method getActualModes().
So the first question is, which relatinship I should use in class diagram.
After that I am creating new instane of ModeContext class and then call method executeStrategy. In this case, which relatiship is better ?
It should be like this:
Access to the singleton (note the stereotype which is just convenient and no obligation or general standard) is anonymous and so you just have a dependency. The ModeContext in contrast uses a private (I don't know the scoping rules of the language you used, so I made it pivate) property called context. Note the dot which is saying exactly that.
Disclaimer: UML does not specify a mapping between Java and UML, so every answer to your question is open for debate.
I think both relationships are dependencies, drawn as dashed arrows from Controller to ActualModes and from Controller to ModeContext. The definition of 'dependency' according to the UML 2.5 specification (§7.8.4.1) is:
A Dependency is a Relationship that signifies that a single model Element or a set of model Elements requires other
model Elements for their specification or implementation.
An example of a type of relationship which is in my opinion less suited, is the association, although its definition (§11.5) is quite broad:
An Association classifies a set of tuples representing links between typed instances. (...) An Association specifies a semantic relationship that can occur between typed instances.
One could argue that there are links between Controller and the other two classes, in the form of variables, but these variables are local method variables, which exist only temporarily during the execution of the method. Associations represent more durable links, e.g. class members - as far as I understand UML and as far as I have seen associations used in practice.

How the N-ary association works in class diagram?

i have three classes and each one of them has an association with same forth class, is it okey to use N-ary association in this case ?
It depends on the logic of your domain. If each of the three classes has a logically separate relationship with the fourth class (i.e. they can vary independently) then they are separate associations. If they are all associated by the same relationship then this would be N-ary. For example, a Car, Driver and Route could be all associated one relationship -- that you might call Journey -- which would be three-ended (N-ary), whereas a Car and an aggregate part (e.g. Wheel) would be two-ended. So it depends.

UML definition - Generalization, aggregation and abstract classes

I'm a bit confused regarding Generalization, Aggregation and abstract classes in UML.
By Generalization, I can understand that it's a "is a"-relationship. A student is a Person - and a teacher is a Person. So Person would be the superclass, whereas student and teacher are both subclasses.
By Aggregation, this is what I understand: It's a "softer" relation compared to composition. An example could be: You can have a hand with no fingers (aggregation) but you can't have fingers without a hand (composition).
And then I am completely lost on abstract classes. What are the characteristics of abstract classes? I'd like an example on this if possible.
But am I on the right track here? This is how I understand these terms.
You understand Generalization.
Associations may be Aggregations or Compositions (or neither). This is a good example that Tom Pender used to use. Suppose you have a Car class. That Car class "has" a steering wheel, seats, two axles, four wheels, and so on. If you're creating that Car in a manufacturer context, the relationship between all of those would be Composition: the lifetime of all the car's parts (at your manufacturer) is tied to the lifetime of the car. From the standpoint of the manufacturer, the lifetime of the car and all its parts ends when you deliver it to a dealer.
Now, let's suppose you're the owner of a junkyard. In this case, a car still "has" all those parts, but they have a lifetime of their own: you can sell parts off of the car, and even make them part of some other car if you need to. The relationship between the car and those parts is Aggregation, because the lifetime of the parts isn't tied to the lifetime of the car itself.
So, you can see that the same car could actually be modeled in two different ways depending on context.
As for abstract classes: an abstract class is simply a class that defines methods and/or properties while requiring that they can only be implemented in subclasses. It's easiest to explain why with interfaces. Interfaces are abstract classes, with the added proviso that NONE of the methods or properties can be implemented directly in the class (you might say it's a definition of how to define a subclass). Here's where they come in useful.
Suppose I want to define an Animal class. What do animals do? Well, they move. They bite. (They do other things too, of course, but let's stick to Move and Bite.) If I create an Animal interface, I'm saying "here's what an animal does. If you want to be an animal, you have to also do these things. However, I'm not going to tell you how to do them." So, I create an iAnimal interface, with two methods, Move and Bite. As an interface, I don't provide any sort of implementation, just empty procedures.
Now, suppose I have two animals, a Flea and a TRex. Clearly, Fleas and TRexes don't have anything in common in the way that they move or the way that they bite, but they both do both. I'll have Flea and TRex inherit or "implement" the interface, providing implementations of Move and Bite appropriate for the type of animal.
The advantage of this is that clients of my flea and trex don't have to know which one they are dealing with. They can create an iAnimal, inject an instance of Flea or TRex as appropriate, and safely call iAnimal's Move and/or Bite methods without having to worry about whether they're supported or not. (This is what we mean by "polymorphism").
So, interface implementation is a form of Generalization (more correctly Specialization, going the other way), as you have probably already surmised.
From here, an "abstract class" is simply any class with any method or attribute defined that has to be inherited to be implemented. Therefore, as I have said, interfaces are abstract classes. However, in general usage, an abstract class is taken to mean one that is not also an interface, i. e. one that has some concrete methods or attributes. Most programming languages adhere to this definition.

Multiple compositions in UML

In a UML class diagram, is it technically correct to have two possible compistion relationships leading to one class?
I.E. I have an inventory class, which has a composition of the inventory class. I want to have the same relationship but with a container class taking the place of the inventory.
So, can I have two compositions, or do I need to turn these into aggregations?
You can have as many composite associations as you like on the class level. But each instance can only be part of one composition at a specific moment in time.
UML superstructure says:
If the whole has aggregationKind = composite then the part can be included in at most one composite at a time
This article I wrote tries to explain the difference: UML Composition vs Aggregation vs Association
Any number of composition association can lead to one class of course. If instance of composed class is composed of instances of more types for example.

Resources