Class association (in UML)

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 !
 
J

Joanna Carter [TeamB]

"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
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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
 
G

Guest

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
 
L

Laurent Bugnion [MVP]

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
 
L

Laurent Bugnion [MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top