Book a Demo

Author Topic: UML Notation Question on Polymorphism  (Read 6518 times)

dlramsey

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
UML Notation Question on Polymorphism
« on: February 02, 2004, 11:51:08 am »
Often you attempt to exploit polymorphism to the greatest extent possible and it helps. But sometimes a class ends up with peculiarities unique to that class. So assume I have a Collection c of objects of SomeClass. Further, consider that I want to do a number of operations in sequence on the members of that collection but that I need to do something additional for one special case subclass.


Code: [Select]

 public void doSomething(Collection c)
 {
   Iterator i = c.iterator();
   while (i.hasNext())
   {
     SomeClass d = (SomeClass) i.next();
     d.doSomething();
     d.doSomethingElse();
     if (d instanceof SomeSubClass)
     {
       ((SomeSubClass) d).doSomethingSpecial();
     }
   }
 }



So above is a contrived example, I am wondering if anyone has any suggestions on the best way to show these messages and interactions in a UML diagram? Nothing really clean hits me though I can see a couple possible ways to diagram it. However, I am thinking this is something others may have had to see and deal with before?

And to be more specific, in a sequence diagram I would show the interaction early on as referring to an object of type SomeClass. Later, we discover that the object is of SomeSubClass and some additional behavior is invoked. But that object is already present in the diagram as SomeClass and typed accordingly.

One thought I had was to add another object to the sequence diagram, give it the same instance name as the first, and invoke the specialized behavior from there. That shows continuity of variable names as you flow from message to message. But there is nothing that I can find in UML to show that in a way that seems really clear to me.

Any suggestions in this regard would be much appreciated. Thanks in advance.

Javier

  • EA User
  • **
  • Posts: 67
  • Karma: +0/-0
  • Necessity is the mother of email
    • View Profile
Re: UML Notation Question on Polymorphism
« Reply #1 on: November 08, 2004, 01:17:50 pm »
IMO, you can use a condition or a {constraint} (in the form of a note attached to a message ) in the sequence diagram stating that operation doSomethingSpecial executes only if  d is an instance of SomeSubclass.  In order to remain programming-language agnostic the text can be in OCL:

[d.oclIsTypeOf(SomeSubclass) = SomeSubclass]

OR

{d.oclIsTypeOf(SomeSubclass) = SomeSubclass}

Regards.
We must become the change we want to see.
- Ghandi