3

Is there a way to separate abstract class and it's concrete branches at a sequence diagram?

My use case is that some transactions must hit methods implemented in abstract class and some from its concrete implementations.

Or it is a concern of a class diagram and not a sequence diagram?

user487772
  • 8,800
  • 5
  • 47
  • 72
  • 1
    The way to do it is add a separate life line for it and show the messaging to it. As far as a standard notation like you see in class and modern entity relationship diagrams I'm not so sure. SDs should be simple, based on the diagrams I've seen, I think it'd be more appropriate for a class diagram. – ChiefTwoPencils Dec 27 '14 at 10:31
  • I would like not to add new lifeline unless I would be able to show the relationship between abstract and concrete classes. However I do agree that it is more likely a concern of class diagram. – user487772 Dec 27 '14 at 12:22
  • 1
    If you want to model two different classes then in the sequence diagram it would mean two different lifelines. That is it. You can show any `<>` (including `<>`) in the lifeline head as shown in this example: http://www.uml-diagrams.org/sequence-diagrams-examples.html#pluck-comments – xmojmr Dec 27 '14 at 15:58
  • Why not post it as an answer? ;-) – user487772 Dec 27 '14 at 16:21
  • 1
    As far as the stereotypes go in that example, I'm not sure that's standard UML. Models are great in that they can reflect the level of detail you choose, but the issue that I see is the tooling. FE, Visual Studio UML SDs don't allow you to add stereotypes to life lines. That'd be something, then, that would need to be added outside of VS and therefore wouldn't add detail to the model as VS sees it and each time your model changes you'd have to add it back after rendering it to an image or similar. That may not be a concern for you; just something to consider. – ChiefTwoPencils Dec 27 '14 at 20:37
  • Also a valid answer. – user487772 Dec 27 '14 at 21:38
  • 1
    @ChiefTwoPencils: the UML specification allows itself to be expanded through the use of the `<>` indicator. When a tool does not allow you to add stereotypes to an artifact that it exposes, to that extent that tool does not support the UML specification. So yes, xmojmr is absolutely correct as far as UML is concerned in saying that you can add stereotypes to the lifeline head of a sequence diagram. The diagram he links is a good example of stereotype use. – BobRodes Jan 01 '15 at 07:39
  • @BobRodes, all I can say is you're probably wrong; at least in the context of distinguishing between an abstract vs. concrete- specifically when it comes to "abstract". That isn't even a "stereotype"; if it's represented as such it's *not* per what *not only* the VS standard which follows UML pretty precisely, but also by a well respected book I won't link due to laziness. "abstract" isn't represented as a stereotype (<<...>>) it is represented in *standard* fashion as *SomeAbstractClass* not <>. – ChiefTwoPencils Jan 01 '15 at 10:47
  • @BobRodes, however <> is. My, still valid, point was, there are tools where you can surely do what you like, and if need be - so be it. *But* some things no matter how much you'd like to represent it a certain way, does not compute. – ChiefTwoPencils Jan 01 '15 at 10:48
  • @BobRodes, et al: [Please read](http://stackoverflow.com/questions/13107893/uml-questions-about-abstract-and-stereotypes/13118076#13118076)! – ChiefTwoPencils Jan 01 '15 at 11:10
  • @ChiefTwoPencils: I think we are talking past one another. There are numerous ways to represent an abstract class, including italicizing the class name as you mention. However, it is also true that stereotyping is an extensibility mechanism that can be applied with the restriction that you can't use an existing keyword as one. So, if you want to create a stereotype called <> (which is not a keyword) and add it to a profile, explaining that it refers to an abstract class that is not an interface, that is entirely consistent with the UML specification for extensibility mechanisms. – BobRodes Jan 03 '15 at 19:26
  • Now, I don't argue that this is necessarily the best way to do this. I'm only saying that it isn't "wrong" in terms of the UML spec to do it. (In terms of the link you post, there's nothing in the formal spec that prevents you from creating a "custom stereotype" called abstract. I've elaborated in an answer there.) In general, if I want to make a class that is partially abstract, I would italicize the class name and would italicize the methods that I wanted to be abstract. Or, if I wanted to make it really clear, I would add the {abstract} property to the methods I wanted to be abstract. – BobRodes Jan 03 '15 at 23:27
  • I'll refer you to the UML Superstructure (2.0) Spec at section 18 which deals with profiles. From this you can see that you can create a profile, in which which you can create an Abstract class which extends the Class metaclass. (Figure 18.10 does something like it.) To this class you can add a constraint requiring that some methods must be implemented and some not. This would be the formal means of defining the stereotype. – BobRodes Jan 03 '15 at 23:27

2 Answers2

5

In your sequence diagram you should only use objects of the types you know them to be at that particular point in execution.

Then you call the method on that object, even if it is a method implemented on the abstract parent class.

Sequence diagrams are very much like code int hat respect.

So suppose you have following situation: Class diagram

Then you call both the implemented as the abstract operation on an object of ConcreteSubClass, because your user class has an association to ConcreteSubClass, regardless of where the operation is implemented.

Sequence diagram

If the User class had an association to the AbstractClass then you call the operations on an object of type AbstractClass

Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50
0

If you want to model two different classes then in the Sequence Diagram it would mean two different lifelines. That is all (as far as I know).

You can show any <<stereotype>> (including <<abstract>>) in the lifeline head as in this example: uml-diagrams.org: UML Sequence Diagrams Examples → Submit Comments to Pluck

For example, let's suppose we have this (useless) C# code:

abstract class BaseClass
{
    protected abstract string Name { get; }

    public virtual void DoSomething()
    {
        Console.WriteLine("Something useful done.");
    }

    protected void SayHello(string to)
    {
        Console.WriteLine("Hi {0}, I'm {1}", to, this.Name);
    }
}

class Case1 : BaseClass
{
    protected override string Name { get { return "Case 1"; } }

    public override void DoSomething()
    {
        base.DoSomething();
    }
}

class Case2 : BaseClass
{
    protected override string Name { get { return "Case 2"; } }

    public void DoSomething(string to)
    {
        this.SayHello(to);
        base.DoSomething();
    }
}

class Program
{
    static void Main(string[] args)
    {
        var c1 = new Case1();
        var c2 = new Case2();
        c1.DoSomething();
        c2.DoSomething("Jane");
    }
}

Then UML Sequence Diagram capturing what happens withing the Program.Main might look like this:

enter image description here

I drew the abstract class as implicit friend object sharing the lifetime (and most of the memory) with the concrete class instance. It is actually how is class inheritance implemented in some languages so the scenario is not completely "made-up".

However, the level of detail maybe too much focused on the implementation leaving no place for useful abstraction. This diagram would not survive even small code re-factoring

xmojmr
  • 8,073
  • 5
  • 31
  • 54
  • 1
    Explain to me what *standard* stereotype exists for "<>"; that's not a stereotype and what you say is wrong - <> isn't a stereotype - IOW, it's not represented the way you suggest. – ChiefTwoPencils Jan 01 '15 at 10:57
  • [Please read](http://stackoverflow.com/questions/13107893/uml-questions-about-abstract-and-stereotypes/13118076#13118076)! – ChiefTwoPencils Jan 01 '15 at 11:10
  • What tool was used to make the above diagram? – ChiefTwoPencils Jan 01 '15 at 11:37
  • 2
    @ChiefTwoPencils Good point. [UML Superstructure Specification, v2.4.1](http://www.omg.org/spec/UML/2.4.1/Superstructure/PDF/) in profile `StandardProfileL3` obsoletes the `<>` stereotype applied to `CallEvent` and instead the profile `StandardProfileL2` introduces the `<>` stereotype applied to `BehavioralFeature`. The `<>` stereotype comes from [Eriksson-Penker UML profile](http://www.sparxsystems.eu/resources/uml-profiles/) and I used it intentionally to emphasize the abstract nature of the abstract class as that is what OP wants to explicitly focus on – xmojmr Jan 01 '15 at 14:35