UML Class Diagram Association - How, When, and Why?

I usually got so confused with UML, and this situation is no different. Say I have an IAnimal interface, the Food and Cat class:

interface IAnimal { void Feed(Food food); } class Cat : IAnimal { void Feed(Food food) { //code } } 

I have 3 questions about drawing a UML class diagram for these three elements:

  • I suggest that I should use the connection between IAnimal and Food or Cat and Food. Should the arrow on one side of the association line, if so, which side and why?

  • If I write Feed as the IAnimal method in the diagram, should I write the Feed method inside the Cat class or write only additional Cat methods?

  • Most importantly: should there be an association between Inamenia and Food, Cat and Food, or both?

+4
source share
6 answers

UML defines the number of relationship types.

Relations have several different designations:

  • Association associations have a basic continuous path notation
  • Dependency relationships have a basic dotted arrow notation
  • Generic relationships have a basic continuous-path notation with a triangular arrow
  • Implementation relationships have a basic notation of a dashed arrow with a triangular arrow (combination of dependency and generalization)

Graphically

 +---------------------------+ | <<interface>> | | IAnimal | +---------------------------+ +--------+ | + Feed(food: Food) : void |- - - - <<use>> - - - ->| Food | +---------------------------+ +--------+ ^ /_\ | | | +-----------+ | Cat | +-----------+ 

I.e:

  • The relationship between IAnimal and Food related to use . This is shown as a dependence on the stereotype "use & raquo;
  • The relationship between IAnimal and Cat is an implementation .

Association associations are used to refer to compounds between two or more classifiers. This means that at least one of the classes has an attribute another type (or collection). In fact, the attributes and ends of the links contain the same information and can be used interchangeably.

So IMHO, the relationships you describe should not be modeled as associations.

+14
source

Class diagrams are visual representations of the static structure and composition of a particular system using conventions established by a unified modeling language (UML).

Association is a broad term that encompasses virtually any logical connection or relationship between classes. For example, a passenger and an airline may be associated


Association

Association is the relationship between two objects. In other words, an association defines the multiplicity between objects. Perhaps you know about one-to-one, one-to-many, many-to-one, many-to-many, all these words define the connection between objects. Aggregation is a special form of association. Composition is a special form of aggregation.

enter image description here

Example: A student and teacher have an association.

+1
source

Assuming a class diagram, you should have a “use” association between IAnimal and Food and an “eat” relationship between Cat and IAnimal and Dog and IAnimal:

  IAnimal ----> Food ^ ^ // \\ // \\ Cat Dog 
0
source

How much you pervert yourself about this kind of thing depends a lot on what you use UML for.

If you have some whistling UML-to-code translator, then you need to be picky (but it looks like you're more comfortable with code than with boxes and strings). Why would you use such a tool? )

If you just use UML to communicate with other people, then you can allow yourself to be somewhat less picky.

Craig Larman's “Applying UML and Templates” highlights this point by including diagrams that look as if they were sketched on a blackboard. The solid line referenced by the UML standard must be dashed. So, with arrows, etc.

It’s clear to me that the line should go from IAnimal to Food .

Whether the arrow adds clarity (for readers) is a matter of choice, but it indicates a one-way relationship:

From this introductory snippet:

"In a unidirectional relationship, two classes are connected, but only one class knows that the relationship exists."

0
source

1) There should not be any associations between the IAnimal interface and the Food type. Associations are used only to join types with their own classes. for instance

 class Animal { Food foodEaten; } class Food { //Implementation code } 

then you should write an association indicating the connection between the two types.

Instead, you should specify a dependency indicating that the IAnimal interface is dependent on the type of Food. The dependency is the same as the association in the figure above, just change the straight line to the dotted one.

2) No, do not write these methods and do not write down dependencies. Leave all designations only in the IAnimal interface

0
source

I would say that IAnimal would have food, since it was metabolized, but if you really want to designate HAS-A, I think it should be an aggregate (open diamond) or a symbol of the composition (filled with diamond), depending on the cascading ones removal characteristics.

There are two schools of thought with UML, according to Martin Fowler. There are “sketches” that use a whiteboard and odd sheets of paper to communicate their ideas to other developers.

Then there are those who view UML as engineering drawings, where every last detail of the design needs to be captured.

I am firmly in the former camp. As a former engineer, I can tell you from personal experience that UML does not have the power of real engineering drawings to completely capture the software design.

If you are fortunate enough to believe the latter, make a full desktop or web interface using UML and post it here.

0
source

All Articles