How do Cardinalities and Generalization work in UML? - uml

I am trying to understand how Cardinalities and Relationships work with Generalization in simple UML class diagrams. I have this simple example here:
A Player owns exactly 9 Fields, but the distribution of them is irrelevant (i.e. 5 Endfields, 4 Startfields or 1 Endfield and 8 Startfields). The Field class is abstract. If i model this problem as seen above, would a Player have 9 Fields from each type? If so, how do i model this correctly?

If i model this problem as seen above, would a Player have 9 Fields from each type?
not at the same time. In your model a Player is associated to 9 Fields, knowing each Field is a Endfield or-exclusive a Startfield (supposing there are no other classes inheriting Field)
A given instance of Field cannot be both a Endfield and a Startfield, else that means Fields inherits both Startfield and Endfield (but the inheritances are in the opposite direction)
The corresponding classes in Java from your model can be :
abstract class Field { .... }
class Startfield extends Field { ... }
class Endfield extends Field { .. }
so a Player can be in relation with 9 Endfields, or-exclusive 8 Endfields and 1 Startfield, or-exclusive 7 Endfields and 2 Startfield, ..., or-exclusive 1 Endfield and 8 Startfields, or-exclusive 9 Startfields
If you want a Player has 9 Startfields and 9 EndFields your model can be :
[edit]
However if you extend your model adding an other class inheriting Startfield and Endtfield like :
in case a Player is associated to 9 Bothfields in a way it is also associated with 9 Startfield and 9 Endtfield
The multiplicity (9) of the relation concerns Field, for the inheriting classes that depends as you can see

Your model does correctly express your intended meaning except that you need to add a disjointness constraint to your Endfield-Startfield segmentation (called "generalization set" in UML jargon) for making sure that a field cannot be both an end field and a start field.
As of UML 2.5, segmentations are overlapping (and incomplete) by default. See also the (always excellent) explanations on uml-diagrams.org, which shows how to draw the disjointness constraint.

Related

Static List of all Objects of Class

I wanted to implement a static List for my Class which contains all Objects of the Class like in the following UML approach.
Is this the right way to write the UML?
There is also a question which uses this UML if you know Kotlin i would be happy if you can help me there too ;) Static List of Objects out Class in Kotlin
This is almost correct. Instead of List<IMyClass> the regular UML notation is IMyClass[*].
The UML 2.5.1 specification, section 9.5.4 gives the following notation syntax for a property:
<property> ::= [<visibility>] [‘/’] <name> [‘:’ <prop-type>] [‘[‘ <multiplicity-range> ‘]’] [‘=’ <default>] [‘{‘<prop-modifier > [‘,’ <prop-modifier >]* ’}’]
In your case, the prop-type is IMyClass and the multiplicity-range is *.
Use the stereotype to specify you use a list, for instance :
or if you prefer :
Note : are you sure you want it public ? this is dangerous
Edit for the remark of #qwerty_so: it does not make sense to stereotype an association with <<list>> where you place a multiplicity which makes it a "list" already. Looks like an over-definition too me
The multiplicity indicates a collection, and there are four types of collection depending on isOrdered and isUnique properties (c.f. formal-17-12-05 §7.5.3.2 Multiplicities, Table 7.1 Collection types for MultiplicityElements, page 34) :
isOrdered | isUnique | Collection Type
-----------+----------+-----------------
false | true | Set
true | true | OrderedSet
false | false | Bag
true | false | Sequence
Furthermore a list is one of the subtypes of Sequence, so the multiplicity "*" is far from indicating a "list" already.

how to display subclass names which belongs to particular superclass in [python]

actually I wanted to know how to display list or names of subclass which belongs to particular superclass in python. I have 1 superclass named "company" and 2 subclasses "employees" and "customers".
class company:
def-------
----------
print('subclass of company are %d',(?))
class customers(company):
def--------
-----------
class employees(company):
def---------
------------
what to use in place of ? mark to display subclass of superclass "company"

C# Inheritance access confusion

public class ABC
{
}
public class DEF : ABC
{
}
public class Class1
{
ABC abc = new DEF(); //No error
DEF def = new ABC(); //Compile time error
}
Can anyone explain to me this scenario.
and under what circumstances we might use it.
Its because as per the OOD rule you can assign child to parent but you cannot assign parent to child.
//this possible as you re assigning child to parent
ABC abc = new DEF(); //No error
//this is illegal as you are trying to assign child to parent directly
DEF def = new ABC(); //Compile time error
Reconsider your design again or if you want to convert parent object to child than you need method for that conversion directly its not possible to do it as per OOD rules.
Consider real time example relation of Customer and RetailCustomer or Corporatecustomer of bank. Where you can easily say RetailCustomer or CorporateCustomer is Customer, but you cannot say Customer is RetailCustomer or CorporateCustomer because customer can be of any type.
Same goes for relation between Parent Shape Class and Child Rectangle,Circle etc. class.
This is called as Ploymorphism .
As explained in MSDN
At run time, objects of a derived class may be treated as objects of a
base class in places such as method parameters and collections or
arrays. When this occurs, the object's declared type is no longer
identical to its run-time type.
Base classes may define and implement virtual methods, and derived
classes can override them, which means they provide their own
definition and implementation. At run-time, when client code calls the
method, the CLR looks up the run-time type of the object, and invokes
that override of the virtual method. Thus in your source code you can
call a method on a base class, and cause a derived class's version of
the method to be executed.
The answer of for this question is best explained here MSDN Polymorphism
Let's say ABC is Person, while DEF is Student (which is a subclass of Person). You can always treat a Student as a Person, so the assignment to abc (of type Person) of a Student is correct, but you cannot treat a generic Person as Student, so the assignment to def is wrong (for instance, you cannot get the student number of a person which is not a student)

Is it possible to make more than one connection between two classes in class diagram?

Exemplary situation. listing1 and listing2 has two different purposes and can't be combined into one list. Is it legal in UML to create two separate lines between A and B like this.
class A {
List<B> listing1;
List<B> listing2;
/* omitted */
}
class B {/* omitted */}
Yes, it is a valid case. Each relationship may have different multiplicity.

I need a ObservableCollection of tree levels

i need to create a ObservableCollection<> for tree o more levels in c# like this:
Group 1
Group 2
Group 1
Group 2
Group 1
Group 2
Group 3
thanks in advance,
Timmy Leonard
make a "node" class that contains an observable collection of child nodes, like this:
class node
{
// add some data properties in here, too
public ObservableCollection<node> children;
}
// declare an instance, in the data model (or where-ever)
ObservableCollection<node> RootLevel = new ObservableCollection<node>();
I could probably give you a better answer if I knew what platform your trying to do this for. (WPF or Silverlight, etc)

Resources