This discussion is archived
5 Replies Latest reply: Jun 24, 2011 5:06 AM by gimbal2 RSS

Conversion of association in ClassDiagram to class definition?

866541 Newbie
Currently Being Moderated
I have a doubt regarding association in class diagram and actual definition of clasees. Say we have two classes customer and order. Customer can give N orders while one order belongs to one customer. So when we draw the class diagram we can represent this relationship with bidirectional arrow between customer and order with N written on order side and 1 written on customer side .correct?

Now when we convert this class diagram to actual definition of class, can we simply say customer object will have array of order objects and order object will have a customer object associated with it?

Now lets change the scenario little bit,order object does not know anything about customer objects. In that case order object will not contain customer, rest will be same as above.
Is this correct?
  • 1. Re: Conversion of association in ClassDiagram to class definition?
    796440 Guru
    Currently Being Moderated
    It depends on the particular diagram conventions you use, but in general, an arrowhead at one end, like X---->Y implies that A knows about B, and, in this case, since there's no arrow at the opposite end Y doesn't know about X.

    So X might have a Y or List <Y> variable, depending on the multiplicity.

    And I think there are some conventions where the above would mean that Y knows about X, but X does not know about Y.

    So, ultimately, it comes down to understanding what that particular diagramming convention means, and if X does know about Y, then class X will generally have a member variable of type Y, or some collection of Y, although it doesn't have to be that direct a translation.

    Edited by: jverd on Jun 21, 2011 11:17 AM
  • 2. Re: Conversion of association in ClassDiagram to class definition?
    jduprez Pro
    Currently Being Moderated
    Say we have two classes customer and order. Customer can give N orders while one order belongs to one customer. So when we draw the class diagram we can represent this relationship with bidirectional arrow between customer and order with N written on order side and 1 written on customer side .correct?
    Yes.
    Now when we convert this class diagram to actual definition of class, can we simply say customer object will have array of order objects and order object will have a customer object associated with it?
    Well, I'd warn you against three things:
    1) A UML diagram is always, and almost always purposefully, uncomplete: a class diagram represents one aspect of classes, but not necessarily all the features and associations of these classes. You may very well have a diagram that shows a unidirectional association (because for the purpose of what the diagram illustrates, only that direction is meaningful, while another diagram shows an association in the opposite direction, for another purpose.
    I know in your example you already have a bi-directional association, I just want to point out that you do not convert one diagram to code, you merely convert a model (~ a set of diagrams) to code.

    2) Depending on the diagram's intent (e.g. domain class diagram vs design class diagram), not all associations translate to references. For example, depending on how the Customer and Order instances are created, fetched, used,... you might end up with classes that don't even know eachother (and that are simply used jointly by a higher level business logic class, say DatabaseRetriever ). OK that's a bit far-fetched, but again a diagram, especially a class diagram, does not necessarily express code, merely concepts. Again convert the model (set of diagrams), not one single diagram.

    3) A class diagram says little about the business logic of the various classes. Sequence or collaboration diagrams give more insight on the business logic. You might think that the class diagrams translate to structure, and the sequence diagram, to method bodies, but that's not that simple: for example the class diagram does not say whether the number of Order instances may change for a given Customer instance: if so, you'd better use a dynamic collection of orders in that latter, such as a <tt>List<Order></tt> rather than a fixed-length array <TT>Order[]</TT> .

    Again don't convert a diagram, instead convert a model (set of diagrams, both structure diagrams and dynamic diagrams).

    J.
  • 3. Re: Conversion of association in ClassDiagram to class definition?
    796440 Guru
    Currently Being Moderated
    jduprez wrote:
    Again don't convert a diagram, instead convert a model (set of diagrams, both structure diagrams and dynamic diagrams).
    I would actually say: "Don't convert a diagram. Read a diagram (or multiple diagrams) as one part of +understanding+ a model. The write code that functions as indicated by that model, without worrying about exact, one-to-one, mechanical conversion.
  • 5. Re: Conversion of association in ClassDiagram to class definition?
    gimbal2 Guru
    Currently Being Moderated
    jverd wrote:
    I would actually say: "Don't convert a diagram. Read a diagram (or multiple diagrams) as one part of +understanding+ a model. The write code that functions as indicated by that model, without worrying about exact, one-to-one, mechanical conversion.
    Oh, I read many years of working experience in that one beautiful piece of text.

    Alas, people will read that, shake their heads and continue to believe that their tools are better.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points