Class association (in UML)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi there,
I'm a newbie in C# and i am looking for a simple yet clearly code example
of an association between two classes (a multiciplity association for
example) to help me understand a bit more of UML.

[class1] <- 0..1 --------- 0..* -> [class2]

Thanks !
 
"Oswaldfig" <[email protected]> a écrit dans le message de
news: (e-mail address removed)...

| I'm a newbie in C# and i am looking for a simple yet clearly code
example
| of an association between two classes (a multiciplity association for
| example) to help me understand a bit more of UML.
|
| [class1] <- 0..1 --------- 0..* -> [class2]

The ordinality of this example relationship is not really very useful. It
states that 0 or 1 instances of class1 can be related to 0 or more instances
of class2.

IMO, it would be more normal to fix the class1 end at 1 and then you have a
classic one-to-many relationship; whereas, if you change class 2 to have an
ordinality of 1..*, then you have a "lookup" relationship where an optional
class1 can be related to any number of class2.

Having 0 in either end signifies optionality, 1 signifies "compulsarity".

HTH

Joanna
 
Oswaldfig said:
I'm a newbie in C# and i am looking for a simple yet clearly code example
of an association between two classes (a multiciplity association for
example) to help me understand a bit more of UML.

[class1] <- 0..1 --------- 0..* -> [class2]

Two examples:

order-(1)---------(0..*)-orderline
newsgroup-(1..*)-----------(0..*)-posts

Arne
 
Thanks Joanna, Arne for your comments, perhaps you can show me an example
written C# ? It helps me understand i guess how UML deals with this.

Arne Vajhøj said:
Oswaldfig said:
I'm a newbie in C# and i am looking for a simple yet clearly code example
of an association between two classes (a multiciplity association for
example) to help me understand a bit more of UML.

[class1] <- 0..1 --------- 0..* -> [class2]

Two examples:

order-(1)---------(0..*)-orderline
newsgroup-(1..*)-----------(0..*)-posts

Arne
 
Oswaldfig said:
Arne Vajhøj said:
Oswaldfig said:
I'm a newbie in C# and i am looking for a simple yet clearly code example
of an association between two classes (a multiciplity association for
example) to help me understand a bit more of UML.

[class1] <- 0..1 --------- 0..* -> [class2]
Two examples:

order-(1)---------(0..*)-orderline
newsgroup-(1..*)-----------(0..*)-posts
Thanks Joanna, Arne for your comments, perhaps you can show me an example
written C# ? It helps me understand i guess how UML deals with this.

Assuming all bidirectional:

public class Order
{
private List<OrderLine> orderlines;
...
}

public class OrderLine
{
private Order order;
...
}

public class NewsGroup
{
private List<Post> posts;
...
}

public class Post
{
private List<NewsGroup> newsgroups;
...
}

Arne
 
Thanks Arne,

Your example is very helpfull !

OswaldFig

Arne Vajhøj said:
Oswaldfig said:
Arne Vajhøj said:
Oswaldfig wrote:
I'm a newbie in C# and i am looking for a simple yet clearly code example
of an association between two classes (a multiciplity association for
example) to help me understand a bit more of UML.

[class1] <- 0..1 --------- 0..* -> [class2]
Two examples:

order-(1)---------(0..*)-orderline
newsgroup-(1..*)-----------(0..*)-posts
Thanks Joanna, Arne for your comments, perhaps you can show me an example
written C# ? It helps me understand i guess how UML deals with this.

Assuming all bidirectional:

public class Order
{
private List<OrderLine> orderlines;
...
}

public class OrderLine
{
private Order order;
...
}

public class NewsGroup
{
private List<Post> posts;
...
}

public class Post
{
private List<NewsGroup> newsgroups;
...
}

Arne
 
Hi,
"Oswaldfig" <[email protected]> a écrit dans le message de
news: (e-mail address removed)...

| I'm a newbie in C# and i am looking for a simple yet clearly code
example
| of an association between two classes (a multiciplity association for
| example) to help me understand a bit more of UML.
|
| [class1] <- 0..1 --------- 0..* -> [class2]

The ordinality of this example relationship is not really very useful. It
states that 0 or 1 instances of class1 can be related to 0 or more instances
of class2.

IMO, it would be more normal to fix the class1 end at 1 and then you have a
classic one-to-many relationship;

I can imagine that a 0..1 ---> 0..* relationship makes sense in certain
scenarios. For example in ASP.NET, we have custom controls which are
basically classes. Sometimes you can use a custom control for the
included functionality without including it in a Page. In that case, you
need to check if the control has a Parent. So you have the following
relationship:

[Page] 0..1 ------- 0..* [CustomControl]
whereas, if you change class 2 to have an
ordinality of 1..*, then you have a "lookup" relationship where an optional
class1 can be related to any number of class2.

Having 0 in either end signifies optionality, 1 signifies "compulsarity".

Actually having 0 on one end signifies "forbiddenity" ;-) but I can't
really think of a practical example... "0..1" specifies optionality.
HTH

Joanna

Greetings,
Laurent
 
Hi,
Assuming all bidirectional:

public class Order
{
private List<OrderLine> orderlines;
...
}

public class OrderLine
{
private Order order;
...
}

As an add-on, the "1" relationship specifies that the "order" variable
may not be null. It specifies that the class OrderLine may not have a
life of its own, but that it is always attached to an Order. Translated
in code, it means you may use the "order" variable without checking if
it's null.

If you had had a "0..1" relationship, then the "order" variable may be
null and you need to check that in the code before using it.
public class NewsGroup
{
private List<Post> posts;
...
}

public class Post
{
private List<NewsGroup> newsgroups;
...
}

Again, the "1..* relationship means that posts are always attached to at
least one newsgroup. So the "newsgroups" list is never empty.

Greetings,
Laurent
 
Back
Top