2

First, see these Java codes:

Drawable.java

package examples.simple.model;

public interface Drawable {
    public void draw();
}

Shape.java

package examples.simple.model;

public abstract class Shape  implements Drawable {
    private Point center;

    public Point getCenter() {
        return center;
    }

    public void setCenter(Point center) {
        this.center = center;
    }
}

Rectangle.java

package examples.simple.model;

public class Rectangle extends Shape {
    public void draw() {
        System.out.println("Drawing a rectangle....");
    }
}

Circle.java

package examples.simple.model;

public class Circle extends Shape {
    public void draw() {
        System.out.println("Drawing a circle....");
    }
}

Line.java

package examples.simple.model;

public class Line implements Drawable{
    public void draw() {
        System.out.println("Drawing a line");
    }
}

Plotter.java

package examples.simple.client;

import java.util.ArrayList;
import java.util.List;

import examples.simple.model.Circle;
import examples.simple.model.Drawable;
import examples.simple.model.Rectangle;
import examples.simple.model.Shape;
import examples.simple.model.Line;

class Plotter {

    public static void main(String[] args) {
        List<Drawable> drawables = new ArrayList<Drawable>();

        Shape s = new Circle();
        drawables.add(s);

        s = new Rectangle();
        drawables.add(s);

        Line l = new Line();
        drawables.add(l);

        for (Drawable drawable : drawables) {
            drawable.draw();
        }
    }
}

The codes are a classical example of polymorphism. The class diagram for these code is

enter image description here

When I have tried to model these classes using a UML sequence diagram to show the polymorphism, using only one sequence diagram, I have needed to use four comments to represent the polymorphism.

enter image description here

So, how to visualize polymorphic invocations in a single diagram, without comments? Are there another notation or visualization (no UML sequence or communication diagram) to show polymorphism? In this example, how to show the invocation drawable.draw() in Plotter.main()?

Fabio Petrillo
  • 612
  • 8
  • 13
  • 1
    Welcome to Stack Overflow. You can upload your existing image to a dropbox-style share and share just the link, low reputation is annoying but you'll have to work on it first. Structure and behavior are different UML aspects modeled using different diagrams, there is no easy 1 diagram showing it all. Google for "activity diagram polymorphism" gave me as 6th link document http://squall.sce.carleton.ca/pubs/tech_report/TR_SCE-05-09.pdf - see "Figure 34-A sequence and a class diagram showing the effect of classes with polymorphism in CFA" for an example of what you are dealing with – xmojmr Aug 03 '14 at 19:22
  • Thank you @xmojmr, the report is very nice! Now I have enough reputation to add images and I have added 2 diagrams to illustrate the question. I read the report cited and the authors used 3 diagrams (1 for each scenario) to represent polymorphic invocations em UML. So, I understand that it is not easy, there is a diagram (maybe out of UML) to show polymorphism? – Fabio Petrillo Aug 03 '14 at 20:39
  • 1
    You can add your polymorphic classes as actors (```Line```, ```Shape```, ```Circle```, ```Rectangle```) and show how the generic abstract actor ```Drawable``` redirects the ```draw()``` message with some **guard condition** as shown in [uml-diagrams.org examples](http://www.uml-diagrams.org/sequence-diagrams-examples.html). I would also consider modeling your problem with [activity diagrams](http://www.uml-diagrams.org/activity-diagrams.html) (I guess you have something more complex on your mind) – xmojmr Aug 04 '14 at 04:55
  • 1
    Thank you @xmojmr for your comment. Using your comment, I found this article http://www.vainolo.com/2012/05/02/factory-method-design-pattern-uml-modeling/? I think that it answer my question. Thank you! – Fabio Petrillo Aug 04 '14 at 13:33
  • In that case you may/should [answer your own question](http://stackoverflow.com/help/self-answer) for future readers. If you [write a good answer](http://stackoverflow.com/help/how-to-answer) it may earn you some more points (you'll need 50 points to get the right to write comments :) – xmojmr Aug 04 '14 at 15:37

3 Answers3

3

Using a idea proposed in Factory Method Design Pattern – Sequence Diagrams, the polymorphic invocations are modeled by multiples scenarios controlled by the guard conditions. Therefore, for each polymorphic scenario, the dynamic binding (polymorphic invocation) is represented for a "scenario box". So, this is a single model (sequence diagram) to show polymorphic invocations.

Sequence Diagram to represent polymorphic scenarios

Literally, the author's post comments your modeling strategy:

"It turns out that to model the flow of operations I am actually modeling how polymorphism, late binding and all that stuff works. I guess that in this case, a sequence diagram is not only irrelevant, but actually confusing. But I had to try."

To conclude, to show polymorphic invocations is a non-trivial task, and I think that clearly visualize polymorphism actually is a open challenge for software engineers.

Fabio Petrillo
  • 612
  • 8
  • 13
  • http://www.uml-diagrams.org/uml-object-oriented-concepts.html#polymorphism says that the term ```polymorphism``` is not explicitly mentioned by the UML specification anymore. May be because it is not needed. Polymorphism and virtual methods etc. are natural part of the structural modeling using class diagrams. The challenge you mentioned is probably in finding a well balanced one diagram to show it all (which is, [in my opinion](http://stackoverflow.com/a/23301076/2626313), not possible and not needed) – xmojmr Aug 05 '14 at 06:47
  • Very interesting article! But, why is not need? :-) For you, is not necessary visualize polymorphic invocations to understand the relationship between a client (Plotter.java for example) and a infrastructure (Drawable children)? For example, to identify several GOF design pattern, is it not a strategy to search polymorphic invocations? Thank for this interest discussion! – Fabio Petrillo Aug 05 '14 at 13:33
  • I mentioned the "not needed" twice by mistake. The 1st time it is just "may be". The second time I mean it like: you don't need one single diagram to show all aspects at once as you can easily draw two or more diagrams, each one showing different aspect (structure, behavior) in different level of detail, from different point of view. Clarified. Documented. Done. That is my understanding of the UML approach. BTW: there are/were some attempts to make one size fits all, e.g. ```fUML```, ```state-machine.com```. I don't know the correct answer to the open challenge. If you find one, let us know :) – xmojmr Aug 05 '14 at 17:50
  • This solution is misleading (wrong) because each lifeline in a sequence diagram is an object. In real execution, there is no separate object for the Interface and the instance of `Circle` (or the other classes). Furthermore, `draw()` appears in your diagram to be recursive, which it's not really. It's only sent to one object. – Fuhrmanator Feb 12 '18 at 21:26
2

Sequence diagrams are strong at showing interactions, they're not very well suited to depict structural properties of a model.

A class diagram could be what you need. The below image is an example of a class diagram that shows polymorphism (namely the getArea method).

enter image description here

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
0

For the reasons I put in my comment (each object having its own lifeline, each message being a single call, etc), it's difficult (if not impossible and wrong) to try to do this in a single diagram.

Furthermore, the beauty of polymorphism is that at a certain level (the polymorphic call), we don't want to know the details. A call is made to draw() and it just works, regardless of the implementing class. The details are hidden in the abstraction. UML diagrams are useful when they show the essential information.

enter image description here

In your answer, each of the shapes just receives the message draw() (we don't see any difference in behavior from the sequence diagram). If you really need to clarify differences between implementations of Circle, etc., I suggest representing the polymorphic calls in separate diagrams so we can see which different objects are used to achieve the draw() function, e.g. :

enter image description here

Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111