3

Consider the following java classes:

public class GenericActionService {

    public void runAction(int actionNo) {

        .... (some stuff)

        runActionCode(actionNo);

        .... (some more stuff)
    }

    protected void runActionCode(int actionNo) {
        switch (actionNo) {
            case 100:
                doSomeGenericAction();
                break;
            ....

            default:
                throw new IllegalArgumentException("Invalid Action!");
        }
    }
}

And inherited sub-class:

public class SpecificActionService extends GenericActionService {

    protected void runActionCode(int actionNo) {
        switch (actionNo) {
            case 1000:
               doSomeSpecificAction();
               break;
            ....

            default:
                super.runActionCode(actionNo);
                break;
        }
    }
}

Now I'm not concerned about whether this is particularly good design or not, and these classes are highly fictional.

What I would like to know is, how do you represent this design with some sort of diagram? Can this be represented using a UML Sequence diagram? If not, what type of diagram should be used?

In particular, I'd like to diagram the flow/sequence when something calls the runAction() method on an instance of SpecificActionService.

For example, consider the flow of execution when runAction(1000) is called on SpecificActionService. I would want the diagram to show the following:

  • Calls SpecificActionService.runAction(1000)
  • Which runs some code in GenericActionService.runAction()
  • Before it calls SpecificActionService.runActionCode(1000)
  • Which calls GenericActionService.runActionCode(1000)
  • Which performs the action
  • Before returning to SpecificActionService.runActionCode(1000)
  • Which returns to GenericActionService.runAction(1000)
  • Where more code is run before it returns to the original caller

The problem is some people consider this to be "modern spaghetti code", especially managers who don't seem to 'get it' when it comes to this kind of inheritence structure, so I'm wanting to document this flow so they understand.

Extra points to those who draw a pretty picture for me. :-)

DuncanKinnear
  • 4,563
  • 2
  • 34
  • 65

1 Answers1

3

In particular, I'd like to diagram the flow/sequence

So I would use a sequence diagram. This can show how the Objects work together. It did not show you the inheritance structure and how to write the classes but for this you can also add a Class Diagram later.

And here a fancy simple image how this could look: Sequence Diagram

And the plantuml source code if this is not good enough and you want to experiment with this :).

otherObject->SpecificActionService:runActionCode(1000)
SpecificActionService->SpecificActionService:doSomeSpecificAction()

otherObject->SpecificActionService:runActionCode(100)
SpecificActionService->GenericActionService :runActionCode(100)
GenericActionService->GenericActionService:doSomeGenericAction()
mszalbach
  • 10,612
  • 1
  • 41
  • 53
  • Yes, this is pretty close to what I want to show. But it doesn't show how both calls to `runAction()` execute some code in `GenericActionService` before calling the `runActionCode` method(s). In fact, it looks like the call to `runAction(1000)` never goes near `GenericActionService`. Please see the edit of my question for a more specific example of what I'm trying to show. – DuncanKinnear Aug 19 '15 at 20:40
  • Also, your diagram shows calls to `runActionCode()` (which is `protected`) not `runAction()`. – DuncanKinnear Aug 19 '15 at 20:54
  • Since you have better understanding of your code and the sequence flow I suggest you try to draw such a diagram and I can try to give tips if something is bothering you in the result. Its kinda easy to get some first diagrams with the tool I used (of course you can use something else capable of UML) – mszalbach Aug 19 '15 at 21:38
  • Yes, what you have shown me is good because it shows that I can use a sequence diagram to show inheritance. When I went looking on the internet (Google image search for "uml sequence diagram inheritance overridden") I couldn't see any that seemed to fit. Cheers. – DuncanKinnear Aug 19 '15 at 22:14