3

I'm not sure how to represent this scenario in a UML class diagram.

An example of what I mean:

A typical Mysql class which handles the DB connection, but delegates the statement construction and execution to another class called MysqlStatement, so that the Mysql class has one method called Mysql::prepare() which returns the Mysqlstatement class (not containing it), like so:

    class Mysql extends DB_Connection {

    public function connect($user, $pass, $dbhost, $dbname)
    {
        $this->dbh = mysql_connect($dbhost, $user, $pass);
        // ...
    }

    /**
     * Connects to the DB, then creates and returns the statement object
     * @param string $query
     * @return MysqlStatement
     */
    public function prepare($query)
    {
        if (! $this->dbh) $this->connect();
        return new MysqlStatement($this->dbh, $query);
    }

}

Note that, as far as I know, there's no composition/aggregation/association, because no class contains the other. So I'm not sure how to represent this relationship. I need to do it to put everything in order and get a glimpse of the system.

Luis Martin
  • 910
  • 3
  • 14
  • 31
  • 1
    Sounds like what you need is more on the level of UML sequence or activity diagram than strictly a class diagram. In UML you'd likely have a uni-directional usage relationship to show that you have reference to the class you're returning, and the sequence diagram would show the call sequence. – Ryan J Oct 21 '14 at 19:13
  • So a class diagram will not represent this type of relationship per se? – Luis Martin Oct 21 '14 at 19:17
  • Well, it will, but it may not show the intended usage relationship unless you specify it somewhere else. The sequence diagram will show how the usage is intended to provide the functionality, while UML simply shows that there _is a_ usage relationship between these two classes. – Ryan J Oct 21 '14 at 19:18
  • There must be a way to represent this in a class diagram anyhow. Maybe like some of the defined types of association? Or without any connected arrow whatsoever? If the sequence diagram reflects that, maybe, as you suggest, I'm forced to use it in this case, and leave either two classes unconnected in the class diagram. – Luis Martin Oct 21 '14 at 19:25

1 Answers1

6

You can represent the relationship in UML using a uni-directional usage (instantiation) relationship.

Depending on your tooling, the graphics may be drawn a little differently, but here's a really simple example showing what you want.

enter image description here

See this reference for a description of this type of relationship.

For reference, here's what a sequence diagram might show (details limited obviously)

enter image description here

Ryan J
  • 8,275
  • 3
  • 25
  • 28
  • Wonderful! You drew it from my own example. I appreciate the effort. Now I see it better. So I have to use a dashed arrow. The sequence diagram definitely helps a lot! – Luis Martin Oct 21 '14 at 19:32
  • Good luck, glad it helped you out. – Ryan J Oct 21 '14 at 19:35
  • Just one more thing: which UML modeling tool did you use, or which one would you recommend? A free one if possible. – Luis Martin Oct 21 '14 at 19:38
  • I used ArgoUML to make these. It's not the best, but it's free and open source. I like it for what it does. I don't really have enough knowledge of what else is out there to provide such a recommendation, but [this list](http://en.wikipedia.org/wiki/List_of_Unified_Modeling_Language_tools) should help you get an idea what's out there. Rational Rose is a good (paid) solution, Eclipse has some capability, even Visio... – Ryan J Oct 21 '14 at 19:51
  • I'll try that one ArgoUML. I didn't even know that Eclipse provides UML modeling. I have it installed and it's the IDE I mostly use. So I'll find out more about it. Thanks for all your help and best regards! – Luis Martin Oct 21 '14 at 20:13
  • Eclipse provides an extension that does it, so you might have to install it first. – Ryan J Oct 21 '14 at 20:15